1 / 13

CPSC 388 – Compiler Design and Construction

CPSC 388 – Compiler Design and Construction. Parsers – Syntax Directed Translation. Syntax Directed Translation. Translating from a sequence of tokens to some other form, based on the underlying syntax.

adonis
Download Presentation

CPSC 388 – Compiler Design and Construction

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CPSC 388 – Compiler Design and Construction Parsers – Syntax Directed Translation

  2. Syntax Directed Translation • Translating from a sequence of tokens to some other form, based on the underlying syntax. • augment the CFG: a translation rule is defined for each production. A translation rule defines the translation of the left-hand side nonterminal as a function of: • constants • the right-hand-side nonterminals' translations • the right-hand-side tokens' values (e.g., the integer value associated with an INTLIT token, or the String value associated with an ID token)

  3. Translating Strings • Build the parse tree. • Use the translation rules to compute the translation of each nonterminal in the tree, working bottom up (since a nonterminal's value may depend on the value of the symbols on the right-hand side, you need to work bottom-up so that those values are available). • The translation of the root node is the translation of the string

  4. Build a Parse Tree exp → exp PLUS term exp → exp MINUS term exp → term term → term TIMES factor term → term DIVIDE factor term → factor factor → LPAREN exp RPAREN factor → INT_LIT exp1.trans = exp2.trans + term.trans exp1.trans = exp2.trans - term.trans exp.trans = term.trans term1.trans = term2.trans * factor.trans term1.trans = term2.trans / factor.trans term.trans = factor.trans factor.trans = exp.trans factor.trans = INT_LIT.value Build a parse tree for the input: 2*4+5

  5. Annotated Parse Tree • Parse Tree can be annotated using the translation rules associated with each production • Use translation rule for root node and recurse into sub-nodes as the translation rule dictates

  6. Example Annotated Parse Tree Consider the following CFG, which defines expressions that use the three operators: +, &&, ==. Let's define a syntax-directed translation that type checks these expressions; i.e., for type-correct expressions, the translation will be the type of the expression (either INT or BOOL), and for expressions that involve type errors, the translation will be the special value ERROR. We'll use the following type rules: • Both operands of the + operator must be of type INT. • Both operands of the && operator must be of type BOOL. • Both operands of the == operator have the same (non-ERROR) type.

  7. Example Annotated Parse Tree CFG Translation rules === ================= exp -> exp + term if ((exp2.trans == INT) and (term.trans == INT) then exp1.trans = INT else exp1.trans = ERROR exp -> exp && term if ((exp2.trans == BOOL) and (term.trans == BOOL) then exp1.trans = BOOL else exp1.trans = ERROR exp -> exp == term if ((exp2.trans == term.trans) and (exp2.trans != ERROR)) then exp1.trans = BOOL else exp1.trans = ERROR exp -> term exp.trans = term.trans term -> true term.trans = BOOL term -> false term.trans = BOOL term -> intliteral term.trans = INT term -> ( exp ) term.trans = exp.trans Try Input: ( 2 + 2 ) == 4 Construct Annotated Parse Tree

  8. Create Translation Rules • The following grammar defines the language of base-2 numbers: b -> 0 b -> 1 b -> b 0 b -> b 1 • Define a syntax-directed translation so that the translation of a binary number is its base 10 value. Illustrate your translation scheme by drawing the parse tree for 1001 and annotating each nonterminal in the tree with its translation.

  9. Building AST from Parse Trees • So far, our example syntax-directed translations have produced simple values (an int or a type) as the translation of an input. In practice however, we want the parser to build an abstract-syntax tree as the translation of an input program. • But that is not really so different from what we've seen so far; we just need to use tree-building operations in the translation rules instead of, e.g., arithmetic operations.

  10. Diffferences between AST and Parse Tree • Operators appear at internal nodes instead of at leaves. • "Chains" of single productions are collapsed. • Lists are "flattened". • Syntactic details (e.g., parentheses, commas, semi-colons) are omitted. • In General ASTs omit details having to do with the source language, and just contains information about the essential structure of the program.

  11. Example Abstract Syntax Tree Construct Parse Tree for 3*(4+2) * 3 + 4 2 For constructs other than expressions, the compiler writer has some choices when defining the AST -- but remember that lists (e.g., lists of declarations lists of statements, lists of parameters) should be flattened, that operators (e.g., "assign", "while", "if") go at internal nodes, not at leaves, and that syntactic details are omitted.

  12. Translation Rules for ASTs First need some java classes for nodes in AST class ExpNode { } class IntLitNode extends ExpNode { public IntLitNode(int val) {...} } class PlusNode extends ExpNode { public PlusNode( ExpNode e1, ExpNode e2 ) { ... } } class TimesNode extends ExpNode { public TimesNode( ExpNode e1, ExpNode e2 ) { ... } }

  13. Translation Rules for ASTs CFG Translation rules === ================= exp -> exp + term exp1.trans = new PlusNode(exp2.trans, term.trans) exp -> term exp.trans = term.trans term -> term * factor term1.trans = new TimesNode(term2.trans, factor.trans) term -> factor term.trans = factor.trans factor -> INTLITERAL factor.trans = new IntLitNode(INTLITERAL.value) factor -> ( exp ) factor.trans = exp.trans Add to this CFG and Translation Rules for minus and divide Draw the Parse Tree for 2+3*4 and annotate with translation

More Related