Presentation is loading. Please wait.

Presentation is loading. Please wait.

Compiler Construction

Similar presentations


Presentation on theme: "Compiler Construction"— Presentation transcript:

1 Compiler Construction
Sohail Aslam Lecture 33 compiler: Bottom-up Parsing

2 Implementing Ad-Hoc Scheme
Parser needs a mechanism to pass values of attributes from definitions in one snippet to uses in another This is a note compiler: Bottom-up Parsing

3 Implementing Ad-Hoc Scheme
We will adopt notation used by YACC for snippets and passing values This is a note compiler: Bottom-up Parsing

4 Implementing Ad-Hoc Scheme
Recall that the skeleton LR(1) parser stored two values on the stack symbol,state We can replace this with triples value,symbol,state This is a note compiler: Bottom-up Parsing

5 Implementing Ad-Hoc Scheme
Recall that the skeleton LR(1) parser stored two values on the stack symbol,state We can replace this with triples value,symbol,state This is a note compiler: Bottom-up Parsing

6 Implementing Ad-Hoc Scheme
On a reduction by A → b, the parser pops 3|b| items from the stack rather than 2|b| It pushes value along with the symbol This is a note compiler: Bottom-up Parsing

7 Implementing Ad-Hoc Scheme
On a reduction by A → b, the parser pops 3|b| items from the stack rather than 2|b| It pushes value along with the symbol This is a note compiler: Bottom-up Parsing

8 YACC file for a calculator
%token NUMBER LPAREN RPAREN %token PLUS MINUS TIMES DIVIDE %% expr : expr PLUS expr | expr MINUS expr | expr TIMES expr | expr DIVIDE expr | LPAREN expr RPAREN | MINUS expr | NUMBER ; This is a note compiler: Bottom-up Parsing

9 #include <iostream> %} %union {int val;}
%{ #include <iostream> %} %union {int val;} %token NUMBER LPAREN RPAREN EQUAL %token PLUS MINUS TIMES DIVIDE /* associativity and precedence: in order of increasing precedence */ %nonassoc EQUAL %left PLUS MINUS %left TIMES DIVIDE %left UMINUS /* dummy token to use as precedence marker */ %type <val> NUMBER expr %% This is a note compiler: Bottom-up Parsing

10 struct and union struct rec { int x; // 4 bytes double y; // 8 bytes char c; // 1 byte } v, w; sizeof v: 13 bytes, sizeof w: 13 bytes v.x = 10; v.y = 0.345; v.c = ‘#’; w.x = 20; w.y = 24.05; w.c = ‘$’; This is a note compiler: Bottom-up Parsing

11 struct and union struct rec { int x; // 4 bytes double y; // 8 bytes char c; // 1 byte } v, w; sizeof v: 13 bytes, sizeof w: 13 bytes v.x = 10; v.y = 0.345; v.c = ‘#’; w.x = 20; w.y = 24.05; w.c = ‘$’; This is a note compiler: Bottom-up Parsing

12 struct and union struct rec { int x; // 4 bytes double y; // 8 bytes char c; // 1 byte } v, w; sizeof v: 13 bytes, sizeof w: 13 bytes v.x = 10; v.y = 0.345; v.c = ‘#’; w.x = 20; w.y = 24.05; w.c = ‘$’; This is a note compiler: Bottom-up Parsing

13 struct and union union urec { int x; // 4 bytes double y; // 8 bytes char c; // 1 byte } v, w; sizeof v: 8 bytes, sizeof w: 8 bytes assign to one of the 3 fields v.x, v.y, or v.c This is a note compiler: Bottom-up Parsing

14 struct and union union urec { int x; // 4 bytes double y; // 8 bytes char c; // 1 byte } v, w; sizeof v: 8 bytes, sizeof w: 8 bytes assign to one of the 3 fields v.x, v.y, or v.c This is a note compiler: Bottom-up Parsing

15 struct and union union urec { int x; // 4 bytes double y; // 8 bytes char c; // 1 byte } v, w; sizeof v: 8 bytes, sizeof w: 8 bytes assign to one of the 3 fields v.x, v.y, or v.c This is a note compiler: Bottom-up Parsing

16 struct v struct rec { int x; double y; char c; } v; x (4 bytes)
y (8 bytes) This is a note c (1 byte) compiler: Bottom-up Parsing

17 union v union urec { int x; double y; char c; } v; c (1 byte)
8 bytes x (4 bytes) y (8 bytes) c (1 byte) This is a note compiler: Bottom-up Parsing

18 struct and union union urec { int x; // 4 bytes double y; // 8 bytes char c; // 1 byte } v; short int whichOne; v.x = 10; whichOne = 1; v.y = 25.4; whichOne = 2; v.c = ‘a’; whichOne = 3; This is a note compiler: Bottom-up Parsing


Download ppt "Compiler Construction"

Similar presentations


Ads by Google