Download presentation

Presentation is loading. Please wait.

Published byPriscila Tanney Modified over 2 years ago

1
Chapter 2-2 A Simple One-Pass Compiler

2
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

3
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

4
4 PRODUCTION SEMANTIC RULE expr expr 1 + term expr expr 1 - term expr term term 0 term 1... term 9 expr.t := expr 1.t || term.t || + expr.t := expr 1.t || term.t || - expr.t := term.t term.t := 0 term.t := 1... term.t := 9 Syntax-directed definition for infix to postfix translation.

5
5 Attribute values at nodes in a parse tree.

6
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

7
7 Annotated parse tree for begin west south.

8
8 Syntax-directed definition of the robots position. PRODUCTIONSEMANTIC RULES seq begin seq.x := 0 seq.y := 0 seq seq 1 instr seq.x := seq.x 1 + instr.dx seq.y := seq.y 1 + instr.dy instr east instr.dx := 1 instr.dy := 0 instr north instr.dx := 0 instr.dy := 1 instr west instr.dx := -1 instr.dy := 0 instr south instr.dx := 0 instr.dy := -1

9
9 expr expr 1 + term expr expr 1 - term expr term term 0 term 1... term 9 { print (+) } { print ( - ) } { print (0) } { print (1) } { print (9) } Actions translating expressions into postfix notation.

10
10 Actions translating into 95-2+

11
11 Top-down parsing Lookahead … the current token being scanned Array[ num dotdot num ] of integer –

12
12 (2.8) type simple | id | array [ simple ] of type Simple integer | char | num dotdot num

13
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

14
14 Predictive parsing First – – -production Designing a Predictive Parser Left Recursion – expr expr + term –

15
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)

16
16 Translation of 9 – into 95 – 2 +. expr term rest 9 {print( 9 )} - term {print( - )} rest 5 {print( 5 )} + term {print( + )} rest 2 {print( 2 )}

17
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 Functions for the nonterminals expr, rest, and term.

18
18 Replacement for functions expr and rest of Fig expr() { term(); while(1) if(lookahead == + ) { match( + ); term(); putchar( + ); } else if (lookahead == - ) { match( - ); term(); putchar( - ); } else break; }

19
19 Implementing the interactions in Fig uses getchar()return token to read character to caller pushes back c using ungetc(c, stdin) sets global variable to attribute value lexan() lexical analyzer tokenval

20
20 Symbol table and array for storing strings.

21
21 Code layout for conditional and while statements. label test code for expr gofalse out code for stmt 1 goto test label out WHILE IF stmt if expr then stmt 1 { out := newlable; stmt.t := expr.t || gofalse out || stmt 1.t || label out code for expr gofalse out code for stmt 1 label out (2.18)

22
22 Fig Parsing table M for grammar (4.11) NONTER- MINAL INPUT SYMBOL id+*()$ EE'TT'FEE'TT'F E TE' T FT' F id E' +TE' T' T' *FT' E TE' T FT' F (E) E' T' E' T'

Similar presentations

© 2016 SlidePlayer.com Inc.

All rights reserved.

Ads by Google