1 / 4

Stack Machine + AST + ES + Fct Call Memory Model

Stack Machine + AST + ES + Fct Call Memory Model. 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

xanto
Download Presentation

Stack Machine + AST + ES + Fct Call Memory Model

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Stack Machine + AST + ES + Fct Call Memory Model • 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 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. * 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 Example: A Calculator Example: Write a calculator program 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 for main main() {…} external variables for push and pop void push(double f) {…} double pop(void) {…} int getop(char s[]) {…}

  3. 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) {…} 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); calc.h calc.h calc.h

  4. 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) {…} Calculator : Header Files (2/2)

More Related