22
Chapter 2-2 A Simple One-Pass Compiler

Chapter 2-2 A Simple One-Pass Compiler. 2 Syntax-directed translation Syntax-directed definition translation of a construct in terms of attributes associated

Embed Size (px)

Citation preview

Page 1: Chapter 2-2 A Simple One-Pass Compiler. 2 Syntax-directed translation Syntax-directed definition translation of a construct in terms of attributes associated

Chapter 2-2

A Simple One-Pass Compiler

Page 2: Chapter 2-2 A Simple One-Pass Compiler. 2 Syntax-directed translation Syntax-directed definition translation of a construct in terms of attributes associated

2

Syntax-directed translation

Syntax-directed definition translation of a construct in terms of attributes associated with its syntactic components– Syntactic structure :: context-free grammar

– Grammar symbol :: a set of attributes and with each production, a set of semantic rules for computing values of the attributes associated with the symbols appearing in the production

Translation input-output mapping Annotated parse tree a parse tree showing the attribute

values at each node

Page 3: Chapter 2-2 A Simple One-Pass Compiler. 2 Syntax-directed translation Syntax-directed definition translation of a construct in terms of attributes associated

3

Synthesized Attributes

An attribute is said to be synthesized if its value at a parse-tree node is determined from attribute values at the children of node

예제 설명

Page 4: Chapter 2-2 A Simple One-Pass Compiler. 2 Syntax-directed translation Syntax-directed definition translation of a construct in terms of attributes associated

4

PRODUCTION SEMANTIC RULE

expr expr1 + term

expr expr1 - term

expr term

term 0

term 1

. . .

term 9

expr.t := expr1.t || term.t || ‘+’

expr.t := expr1.t || term.t || ‘-’

expr.t := term.t

term.t := ‘0’

term.t := ‘1’

. . .

term.t := ‘9’

Syntax-directed definition for infix to postfix translation.

Page 5: Chapter 2-2 A Simple One-Pass Compiler. 2 Syntax-directed translation Syntax-directed definition translation of a construct in terms of attributes associated

5

expr.t = 95 - 2 +

term.t = 2

+ 2-

term.t = 5expr.t = 9

term.t = 9

9 5

expr.t = 95 -

Attribute values at nodes in a parse tree.

Page 6: Chapter 2-2 A Simple One-Pass Compiler. 2 Syntax-directed translation Syntax-directed definition translation of a construct in terms of attributes associated

6

Depth first traversals

Robot positioning ::– seq seq instr | begin

instr east | north | west | south Depth-first traversals Translation Schemes

– Context-free grammar in which program fragments called semantic actions are embedded

Emitting a Translation

Page 7: Chapter 2-2 A Simple One-Pass Compiler. 2 Syntax-directed translation Syntax-directed definition translation of a construct in terms of attributes associated

7

Annotated parse tree for begin west south.

seq.x = -1seq.y = -1

instr.dx = 0instr.dy = -1

seq.x = -1seq.y = 0

seq.x = 0seq.y = 0

instr.dx = -1instr.dy = 0 south

westbegin

Page 8: Chapter 2-2 A Simple One-Pass Compiler. 2 Syntax-directed translation Syntax-directed definition translation of a construct in terms of attributes associated

8

Syntax-directed definition of the robot’s position.

PRODUCTION SEMANTIC RULES

seq begin seq.x := 0

seq.y := 0

seq seq1 instr seq.x := seq.x1 + instr.dx

seq.y := seq.y1 + instr.dy

instr eastinstr.dx := 1

instr.dy := 0

instr northinstr.dx := 0

instr.dy := 1

instr westinstr.dx := -1

instr.dy := 0

instr southinstr.dx := 0

instr.dy := -1

Page 9: Chapter 2-2 A Simple One-Pass Compiler. 2 Syntax-directed translation Syntax-directed definition translation of a construct in terms of attributes associated

9

expr expr1 + term

expr expr1 - term

expr term

term 0

term 1

. . .

term 9

{ print (‘+’) }

{ print (‘-’) }

{ print (‘0’) }

{ print (‘1’) }

{ print (‘9’) }

Actions translating expressions into postfix notation.

Page 10: Chapter 2-2 A Simple One-Pass Compiler. 2 Syntax-directed translation Syntax-directed definition translation of a construct in terms of attributes associated

10

Actions translating 9-5+2 into 95-2+

expr

{print('+')}

term

{print('2')}

+

expr

{print('- ')}

term

{print('5')}

-

expr

term

{print('9')}9

2

5

Page 11: Chapter 2-2 A Simple One-Pass Compiler. 2 Syntax-directed translation Syntax-directed definition translation of a construct in terms of attributes associated

11

Top-down parsing

Lookahead … the current token being scanned

Array[ num dotdot num ] of integer– 과정 설명

Page 12: Chapter 2-2 A Simple One-Pass Compiler. 2 Syntax-directed translation Syntax-directed definition translation of a construct in terms of attributes associated

12

(2.8)

type simple

| id

| array [ simple ] of typ

e

Simple integer

| char

| num dotdot num

Page 13: Chapter 2-2 A Simple One-Pass Compiler. 2 Syntax-directed translation Syntax-directed definition translation of a construct in terms of attributes associated

13

(a) type type

Array [ simple ] of type type

(c) Array [ simple ] of type

num dotdot num type

(d) Array [ simple ] of type

num dotdot num simple

type

(e) Array [ simple ] of type

num dotdot num simple integer

Steps in the top-down construction of a parse tree

Page 14: Chapter 2-2 A Simple One-Pass Compiler. 2 Syntax-directed translation Syntax-directed definition translation of a construct in terms of attributes associated

14

Predictive parsing

First– 예제로 설명– ∈-production

Designing a Predictive Parser Left Recursion

– expr expr + term– 일반화

Page 15: Chapter 2-2 A Simple One-Pass Compiler. 2 Syntax-directed translation Syntax-directed definition translation of a construct in terms of attributes associated

15

expr term rest

rest + term { print(‘+’) } rest | - term { print(‘-’) } rest |

term 0 { print (‘0’) }

term 1 { print (‘1’) }

. . .

term 9 { print (‘9’) }

(2.14)

Page 16: Chapter 2-2 A Simple One-Pass Compiler. 2 Syntax-directed translation Syntax-directed definition translation of a construct in terms of attributes associated

16

Translation of 9 – 5 + 2 into 95 – 2 +.

expr

term rest

9 {print(‘9’)} - term {print(‘-’)} rest

5 {print(‘5’)} + term {print(‘+’)} rest

2 {print(‘2’)}

Page 17: Chapter 2-2 A Simple One-Pass Compiler. 2 Syntax-directed translation Syntax-directed definition translation of a construct in terms of attributes associated

17

expr(){

term(); rest();}

rest(){

if(lookahead == ‘+’) {match(‘+’); term(); putchar(‘+’); rest();

}else if (lookahead == ‘-’) {

match(‘-’); term(); putchar(‘-’); rest();}else;

} term(){

if (isdigit(lookahead)) {putchar(lookahead); match(lookahead);

}else error;

}

Fig. 2. 22. Functions for the nonterminals expr, rest, and term.

Page 18: Chapter 2-2 A Simple One-Pass Compiler. 2 Syntax-directed translation Syntax-directed definition translation of a construct in terms of attributes associated

18

Replacement for functions expr and rest of Fig. 2.22.

expr(){

term();while(1)

if(lookahead == ‘+’) {match(‘+’); term(); putchar(‘+’);

}else if (lookahead == ‘-’) {

match(‘-’); term(); putchar(‘-’);}else break;

}

Page 19: Chapter 2-2 A Simple One-Pass Compiler. 2 Syntax-directed translation Syntax-directed definition translation of a construct in terms of attributes associated

19

Implementing the interactions in Fig. 2. 25.

uses getchar() return token to read character to caller

pushes back c using ungetc(c, stdin)

sets global variable to attribute val

ue

lexan() lexical analyzer

tokenval

Page 20: Chapter 2-2 A Simple One-Pass Compiler. 2 Syntax-directed translation Syntax-directed definition translation of a construct in terms of attributes associated

20

Symbol table and array for storing strings.

ARRAY symtable

attributes

0

1

2

3

4

lexptr token

ARRAY lexemes

d i v EOS m o d EOS c o u n t EOS i EOS

div

mod

id

id

Page 21: Chapter 2-2 A Simple One-Pass Compiler. 2 Syntax-directed translation Syntax-directed definition translation of a construct in terms of attributes associated

21

Code layout for conditional and while statements.

label test

code for expr

gofalse out

code for stmt1

goto test

label out

WHILEIF

stmt if expr then stmt1 { out := newlable; stmt.t := expr.t || ‘gofalse’ out || stmt1.t || ‘label’ out

code for expr

gofalse out

code for stmt1

label out

(2.18)

Page 22: Chapter 2-2 A Simple One-Pass Compiler. 2 Syntax-directed translation Syntax-directed definition translation of a construct in terms of attributes associated

22

Fig. 4.15. Parsing table M for grammar (4.11)

NONTER-MINAL

INPUT SYMBOL

id + * ( ) $

E

E'

T

T'

F

E TE'

T FT'

F id

E' +TE'

T' T' *FT'

E TE'

T FT'

F (E)

E'

T'

E'

T'