1 / 18

Compiler Construction

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.

iola-reeves
Télécharger la présentation

Compiler Construction

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. Compiler Construction Sohail Aslam Lecture 33

  2. Implementing Ad-Hoc Scheme • Parser needs a mechanism to pass values of attributes from definitions in one snippet to uses in another

  3. Implementing Ad-Hoc Scheme • We will adopt notation used by YACC for snippets and passing values

  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

  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

  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

  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 pushesvalue along with the symbol

  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 ; %%

  9. %{ #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 %%

  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 = ‘$’;

  11. 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 = ‘$’;

  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 = ‘$’;

  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

  14. 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

  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

  16. x (4 bytes) y (8 bytes) c (1 byte) struct v struct rec { int x; double y; char c; } v;

  17. x (4 bytes) y (8 bytes) 8 bytes c (1 byte) union v union urec { int x; double y; char c; } v;

  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;

More Related