Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 2-2 A Simple One-Pass Compiler

Similar presentations


Presentation on theme: "Chapter 2-2 A Simple One-Pass Compiler"— Presentation transcript:

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 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 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 Syntax-directed definition for infix to postfix translation.
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.

5 Attribute values at nodes in a parse tree.

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 Annotated parse tree for begin west south.

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  east instr.dx := 1 instr.dy := 0 instr  north instr.dx := 0 instr.dy := 1 instr  west instr.dx := -1 instr  south instr.dy := -1 Syntax-directed definition of the robot’s position.

9 Actions translating expressions into postfix notation.
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.

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

11 Top-down parsing Lookahead … the current token being scanned
Array[ num dotdot num ] of integer 과정 설명

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

13 Steps in the top-down construction of a parse tree
(a) type type Array [ simple ] of type (c) Array [ simple ] of type num dotdot num (d) Array [ simple ] of type num dotdot num simple (e) Array [ simple ] of type num dotdot num simple integer Steps in the top-down construction of a parse tree

14 Predictive parsing First Designing a Predictive Parser Left Recursion
예제로 설명 ∈-production Designing a Predictive Parser Left Recursion expr  expr + term 일반화

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 Translation of 9 – 5 + 2 into 95 – 2 +.
expr term rest {print(‘9’)} term {print(‘-’)} rest {print(‘5’)} term {print(‘+’)} rest {print(‘2’)} Translation of 9 – into 95 – 2 +.

17 Fig. 2. 22. Functions for the nonterminals expr, rest, and term.
{ 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 Replacement for functions expr and rest of Fig. 2.22.
{ term(); while(1) if(lookahead == ‘+’) { match(‘+’); term(); putchar(‘+’); } else if (lookahead == ‘-’) { match(‘-’); term(); putchar(‘-’); else break; Replacement for functions expr and rest of Fig

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 value lexan() lexical analyzer tokenval Implementing the interactions in Fig

20 Symbol table and array for storing strings.

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

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' F  (E) E'   Fig Parsing table M for grammar (4.11)


Download ppt "Chapter 2-2 A Simple One-Pass Compiler"

Similar presentations


Ads by Google