220 likes | 355 Vues
This section delves into the principles of syntax-directed translation within simple one-pass compilers. It defines how constructs are translated through attributes linked to their syntactic components using context-free grammar. Key concepts include synthesized attributes, annotated parse trees, and the depth-first traversal for evaluating expressions. Practical examples demonstrate translating infix expressions to postfix notation and the intricacies of designing predictive parsers. This comprehensive examination highlights the interactions and procedures involved in translating code efficiently.
E N D
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
Synthesized Attributes • An attribute is said to be synthesized if its value at a parse-tree node is determinedfrom attribute values at the children of node • 예제 설명
Syntax-directed definition for infix to postfix translation.
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
Top-down parsing • Lookahead … the current token being scanned • Array[ num dotdot num ] of integer • 과정 설명
type simple | id | array[ simple ]of type Simple integer | char | num dotdot num (2.8)
(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
Predictive parsing • First • 예제로 설명 • ∈-production • Designing a Predictive Parser • Left Recursion • expr expr + term • 일반화
expr term rest rest + term { print(‘+’) } rest | - term { print(‘-’) } rest | term 0 { print (‘0’) } term 1 { print (‘1’) } . . . term 9 { print (‘9’) } (2.14)
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’)}
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.
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; }
Implementing the interactions in Fig. 2. 25. lexan() lexical analyzer uses getchar() return token to read character to caller pushes back c using ungetc(c, stdin) sets global variable to attribute value tokenval
stmt ifexprthenstmt1 { out := newlable; stmt.t := expr.t || ‘gofalse’ out || stmt1.t || ‘label’ out (2.18) WHILE IF Code layout for conditional and while statements.