40 likes | 215 Views
Fct Call Memory Model + AST & ES + Stack Machine. Function Call Memory Model : execution stack and function frame AST ([extended] Abstract Syntax Tree) : a representation of C expressions as a tree ES (Execution Stack) : a model for evaluating ASTs on a machine
E N D
Fct Call Memory Model + AST & ES + Stack Machine • Function Call Memory Model : execution stack and function frame • AST ([extended] Abstract Syntax Tree) : a representation of C expressions as a tree • ES (Execution Stack) : a model for evaluating ASTs on a machine • First Large Coding Example: calculator (kr76-79) – Example of a Stack Machine (SM) • Description of the problem and approach • Pseudo code • Flat implementation • Modular implementation Imperative and System Programming, B. Hirsbrunner, diuf.unifr.ch/pai/ip, Lecture 5 – 16 October 2013 Morning: Fct Call Mem Model + AST & ES + calculator (flat code) : ~30’ + ~30’ + ~30’; Afternoon: calculator (modular code): ~ 45’; Exercises: ~45’
* 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 + * - + 1 2 3 4 First Large Coding Example : a calculator (1/3)problem + flat specification and pseudo code Problem: Realize a calculator that provides the operators +, -, *, and /. • Pseudo C code • while (next operator or operand is not end-of-file indicator) • if (number) • push it • elseif (operator) • pop operands • do operation • push result • elseif (newline) • pop and print top of stack • else • error Skeleton Program #include … #define … function declarations main() {…} external variables for push and pop void push(double f) {…} double pop(void) {…} int getop(char s[]) {…}
calculator.c #include <stdio.h> #include <stdlib.h> #include "calc.h" #define MAXOP 100 main() {…} calculator.c stack.c getop.c getch.c Dependency graph(all files share calc.h) getch.c stack.c getop.c #include <stdio.h> #include "calc.h" #define MAXVAL 100 int sp = 0; double val[MAXVAL]; void push(double) {…} double pop(void) {…} #include <stdio.h> #include <ctype.h> #include "calc.h" int getop(char []) {…} #include <stdio.h> #include "calc.h" #define BUFSIZE ‘100’ char bufsize[BUFSIZE ]; int bufp = 0; int getch(void) {…} void ungetch(int) {…} First Large Coding Example : a calculator (2/3)modular specification and pseudo code calc.h void push(double); double pop(void); #define NUMBER ‘0’ int getop(char []); int getch(void); void ungetch(int); calc.h calc.h calc.h
calculator.c my_stack.h calculator.c #include <stdio.h> #include <stdlib.h> #include "my_stack.h" #include "my_special_io.h" #define MAXOP 100 main() {…} void push(double); double pop(void); my_stack.h my_special_io.h my_special_io.h my_stack.c my_special_io.c #define NUMBER ‘0’ int getop(char []); my_io.h my_io.h my_io.c int getch(void); void ungetch(int); Dependency graph my_io.c my_stack.c my_special_io.c #include <stdio.h> #include "my_stack.h" #define MAXVAL 100 int sp = 0; double val[MAXVAL]; void push(double) {…} double pop(void) {…} #include <stdio.h> #include <ctype.h> #include "my_io.h" #include "my_special_io.h" int getop(char []) {…} #include <stdio.h> #include "my_io.h" #define BUFSIZE ‘100’ char bufsize[BUFSIZE ]; int bufp = 0; int getch(void) {…} void ungetch(int) {…} First Large Coding Example : a calculator (3/3)modular specification and pseudo code