Presentation is loading. Please wait.

Presentation is loading. Please wait.

0 First Large Example: A Calculator (kr76-79) – Example of a Stack Machine (SM) AST ([extended] Abstract Syntax Tree) : a representation of C expressions.

Similar presentations


Presentation on theme: "0 First Large Example: A Calculator (kr76-79) – Example of a Stack Machine (SM) AST ([extended] Abstract Syntax Tree) : a representation of C expressions."— Presentation transcript:

1 0 First Large Example: A Calculator (kr76-79) – Example of a Stack Machine (SM) AST ([extended] Abstract Syntax Tree) : a representation of C expressions as a tree ES (Execution Stack) : a model for evaluating ASTs on a machine Function Call Memory Model : stack frame Stack Machine + AST + ES + Fct Call Memory Model Imperative and System Programming, B. Hirsbrunner, diuf.unifr.ch/pai/ip, Lecture 5 – 17 October 2012 Morning: calculator (specif) + AST & ES + Fct Call Mem Model: ~30’ + ~30’ + ~20’; Afternoon: calculator (code) + UNIT testing (cont.): ~ 25' + ~20’; Exercises: ~45’

2 1 First Large Example: A Calculator Example : Write a calculator program that provides the operators +, -, *, and /. * - 1234 + Hint: use the reverse polish notation, i.e. an infix expression like (1-2)*(3+4) is entered as the postfix expression 1 2 - 3 4 + * Pseudo C code while (next operator or operand is not end-of-file indicator) if (number) push it else if (operator) pop operands do operation push result else if (newline) pop and print top of stack else error Skeleton Program #include … #define … function declarations for main main() {…} external variables for push and pop void push(double f) {…} double pop(void) {…} int getop(char s[]) {…}

3 2 Calculator : Header Files (1/2) calc.h void push(double); double pop(void); #define NUMBER ‘0’ int getop(char []); int getch(void); void ungetch(int); #include #include "calc.h" #define MAXOP 100 main() {…} calculator.c #include #include "calc.h" int getop(char []) {…} getop.c #include #include "calc.h" #define BUFSIZE ‘100’ char bufsize[BUFSIZE ]; int bufp = 0; int getch(void) {…} void ungetch(int) {…} getch.c stack.c #include #include "calc.h" #define MAXVAL 100 int sp = 0; double val[MAXVAL]; void push(double) {…} double pop(void) {…} calculator.c stack.cgetop.c getch.c Dependency graph (all files share calc.h) calc.h

4 3 Calculator : Header Files (2/2) #include #include "my_stack.h" #include "my_special_io.h" #define MAXOP 100 main() {…} calculator.c #include #include "my_io.h" #include "my_special_io.h" int getop(char []) {…} my_special_io.c #include #include "my_io.h" #define BUFSIZE ‘100’ char bufsize[BUFSIZE ]; int bufp = 0; int getch(void) {…} void ungetch(int) {…} my_io.c my_stack.c #include #include "my_stack.h" #define MAXVAL 100 int sp = 0; double val[MAXVAL]; void push(double) {…} double pop(void) {…} my_special_io.h #define NUMBER ‘0’ int getop(char []); my_io.h int getch(void); void ungetch(int); my_stack.h void push(double); double pop(void); calculator.c my_stack.cmy_special_io.c my_io.c Dependency graph my_stack.hmy_special_io.h my_io.h


Download ppt "0 First Large Example: A Calculator (kr76-79) – Example of a Stack Machine (SM) AST ([extended] Abstract Syntax Tree) : a representation of C expressions."

Similar presentations


Ads by Google