old business write mypaste.c and mycomm.c – mypaste is like paste & mycomm is like comm –...

9
Old Business Write mypaste.c and mycomm.c mypaste is like paste & mycomm is like comm Both take two files as arguments Paste reads a line from each file and outputs them to stdout on the same line Comm compares the two lines with strcmp and outputs the one that comes first in alphabetical order (with the appropriate number of tabs)

Upload: cory-butler

Post on 04-Jan-2016

213 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Old Business Write mypaste.c and mycomm.c – mypaste is like paste & mycomm is like comm – Both take two files as arguments – Paste reads a line from each

Old Business• Write mypaste.c and mycomm.c– mypaste is like paste & mycomm is like comm– Both take two files as arguments– Paste reads a line from each file and outputs them to

stdout on the same line– Comm compares the two lines with strcmp and outputs

the one that comes first in alphabetical order (with the appropriate number of tabs)

Page 2: Old Business Write mypaste.c and mycomm.c – mypaste is like paste & mycomm is like comm – Both take two files as arguments – Paste reads a line from each

New Business• Please install R for next time

– http://www.r-project.org/ • Homework for next time

– Add some more functionality to calculator: • 4 2 choose• 2 4 ^ (extra credit: need pow in math.h)

– http://www.network-theory.co.uk/docs/gccintro/gccintro_17.html

• 4 ! (extra credit: first example of a function with a single arg)– Show that your calculator produces the right answers for the

example(s) above– Show parse trees in Graphviz for

• choose(5,2)• 5/(2 * 3)• Extra credit: 5!/(2! * 3!)

Page 3: Old Business Write mypaste.c and mycomm.c – mypaste is like paste & mycomm is like comm – Both take two files as arguments – Paste reads a line from each

How Does Recursion Work?

Page 4: Old Business Write mypaste.c and mycomm.c – mypaste is like paste & mycomm is like comm – Both take two files as arguments – Paste reads a line from each

Reverse Polish Notation

• 1 2 4 * + 3 +• (1 + (2 * 4)) + 3

• Eval: substitute <arg1> <arg2> <op> with value– 1 2 4 * + 3 +– 1 8 + 3 +– 9 3 +– 12

Page 5: Old Business Write mypaste.c and mycomm.c – mypaste is like paste & mycomm is like comm – Both take two files as arguments – Paste reads a line from each

polish.c1+2*4+3

Page 6: Old Business Write mypaste.c and mycomm.c – mypaste is like paste & mycomm is like comm – Both take two files as arguments – Paste reads a line from each

A Stack

Page 7: Old Business Write mypaste.c and mycomm.c – mypaste is like paste & mycomm is like comm – Both take two files as arguments – Paste reads a line from each

polish.c1+2*4+3

Page 8: Old Business Write mypaste.c and mycomm.c – mypaste is like paste & mycomm is like comm – Both take two files as arguments – Paste reads a line from each

Parse Tree: (1 + (2 * 4)) + 3echo ‘1 2 4 * + 3 +’ | tr ‘ ‘ ‘\n’ | ./polish2 | egrep ‘^#’ | cut –c2- > G.gv

Page 9: Old Business Write mypaste.c and mycomm.c – mypaste is like paste & mycomm is like comm – Both take two files as arguments – Paste reads a line from each

Summary

• Wrote an interpreter in C– Input: expressions (e.g., 1 1 +)– Outputs: values (e.g., 2)

• 4-function calculator: +, -, *, /– Reverse Polish Notation

• Stack: push, pop (foundation behind recursion)• Interpreter: read (parse), eval, print• Used graphviz to display parse tree– Example of combining programs: C, Graphviz, grep