190 likes | 380 Views
Compiler Construction. Sohail Aslam Lecture 33. Implementing Ad-Hoc Scheme. Parser needs a mechanism to pass values of attributes from definitions in one snippet to uses in another. Implementing Ad-Hoc Scheme. We will adopt notation used by YACC for snippets and passing values.
E N D
Compiler Construction Sohail Aslam Lecture 33
Implementing Ad-Hoc Scheme • Parser needs a mechanism to pass values of attributes from definitions in one snippet to uses in another
Implementing Ad-Hoc Scheme • We will adopt notation used by YACC for snippets and passing values
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
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
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
Implementing Ad-Hoc Scheme • On a reduction by A →b, the parser pops 3|b| items from the stack rather than 2|b| • It pushesvalue along with the symbol
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 ; %%
%{ #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 %%
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 = ‘$’;
struct and union struct rec { int x; // 4 bytes double y; // 8 bytes char c; // 1 byte} v, w; sizeofv: 13 bytes, sizeofw: 13 bytes v.x = 10; v.y = 0.345; v.c = ‘#’;w.x = 20; w.y = 24.05; w.c = ‘$’;
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 = ‘$’;
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
struct and union union urec { int x; // 4 bytes double y; // 8 bytes char c; // 1 byte} v, w; sizeofv: 8 bytes, sizeofw: 8 bytes assign to one of the 3 fields v.x, v.y, or v.c
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
x (4 bytes) y (8 bytes) c (1 byte) struct v struct rec { int x; double y; char c; } v;
x (4 bytes) y (8 bytes) 8 bytes c (1 byte) union v union urec { int x; double y; char c; } v;
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;