Docsity

Implementing Ad Hoc Scheme - Compiler Construction - Lecture Slides, Slides of Compiler Construction

Partial preview of the text

Download Implementing Ad Hoc Scheme - Compiler Construction - Lecture Slides and more Slides Compiler Construction in PDF only on Docsity!

Compiler

Construction

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

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 〉

7

Implementing Ad-Hoc Scheme

 On a reduction by A → β, the

parser pops 3×|β| items from

the stack rather than 2×|β|

 It pushes value 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 ; %%

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;

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;

sizeof v: 8 bytes, sizeof w: 8 bytes

assign to one of the 3 fields v.x, v.y, or v.c

16

struct

struct rec { int x; double y; char c; } v;

c ( 1 byte )

y ( 8 bytes )

x ( 4 bytes )

v

17

8 bytes

union

union urec { int x; double y; char c; } v;

c ( 1 byte )

y ( 8 bytes )

x ( 4 bytes )

v