1 / 39

Syntax Directed Definition and Syntax directed Translation

Syntax Directed Definition and Syntax directed Translation. Compilation in a Nutshell 1. Source code (character stream). if (b == 0) a = b;. Lexical analysis. if. (. b. ==. 0. ). a. =. b. ;. Token stream. Parsing. if. ;. ==. =. Abstract syntax tree (AST). b. 0. a. b.

xannon
Download Presentation

Syntax Directed Definition and Syntax directed Translation

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. Syntax Directed Definition and Syntax directed Translation

  2. Compilation in a Nutshell 1 Source code (character stream) if (b == 0) a = b; Lexical analysis if ( b == 0 ) a = b ; Token stream Parsing if ; == = Abstract syntax tree (AST) b 0 a b Semantic Analysis if boolean int == = ; Decorated AST int b int 0 int a lvalue intb

  3. fp 4 fp 8 Compilation in a Nutshell 2 if boolean int == = ; Intermediate Code Generation int b int 0 int a lvalue intb CJUMP == MEM CONST MOVE NOP Optimization + 0 MEM MEM fp 8 + + CJUMP == Code generation CX CONST MOVE NOP CMP CX, 0 CMOVZ DX,CX 0 DX CX

  4. Outline • Syntax-Directed Definitions • Evaluation Orders for SDD’s • Applications of Syntax-Directed Definition • Syntax-Directed Translation

  5. Definitions • Syntax-directed definition (Attribute Grammar) • Productions with semantic rules • Ex: EE1+T E.code=E1.code |T.code|’+’ • More readable • Useful for specification • Syntax-directed translation (Translation Scheme) • Productions with semantic actions • Ex: EE1+T { print ’+’ } • More efficient • Useful for implementation

  6. Syntax-Directed Definitions • SDD: a context-free grammar with attributes and rules • Attributes: for grammar symbols • Rules: for productions • Attributes for nonterminals • Synthesized attribute: attributes that are passed up a parse tree, i.e., the LHS attribute is computed from the RHS attributes in the production rules • EE1+TE.val=E1.val+T.val • Inherited attribute: attributes that are passed down a parse tree, i.e., the RHS attributes are derived from its LHS attributes or other RHS attributes in the production rules • TF T’T’.inh=F.valT.val=T’.syn • Terminals can have synthesized attributes, but not inherited attributes

  7. Example

  8. Annotated Parse Tree for 3*5+4 n L Prod. Semantic Rules L→En print(E.val) E→E1+T E.val := E1.val+T.val E→T E.val :=T.val T→T1*F T.val :=T1.val* F.val T→F T.val :=F.val F→ (E) F.val :=E.val F→digit F.val :=digit.lexval E.val=19 n E.val=15 + T.val=4 T.val=15 F.val=4 T.val=3 F.val=5 digit.lexval=4 * F.val=3 digit.lexval=5 digit.lexval=3

  9. Class Exercise S (L)|a L L,S|S • Write an SDD to print the number of parenthesis pair for an input string. • Write an SDD to print the maximum parenthesis depth.

  10. Outline • Syntax-Directed Definitions • Evaluation Orders for SDD’s • Applications of Syntax-Directed Definition • Syntax-Directed Translation

  11. Example attribute grammar A grammar to evaluate signed binary numbers POS, VAL, and NEG are attributes of the non-terminal (node) they are attached to

  12. Example LIST0 pos pos pos val val val BIT LIST1 • Note: • semantic rules define a partial dependency graph • structure can be used to derive characteristics of generated • total dependency graphs

  13. Attribute grammars The attribute dependency graph nodes represent attributes edges represent the flow of values graph is specific to parse tree size is related to parse tree's size can be built alongside parse tree The dependency graph must be acyclic Evaluation order Topological sort of the dependency graph to order attributes Topological order: a linear ordering of the nodes of a directed acyclic graph such that each node comes before all nodes to which it has outbound edges using this order, evaluate the rules This order depends on both the grammar and the input string

  14. Example attribute grammar Example Parse tree for -101 NUM val pos val SIGN LIST neg pos val pos val LIST BIT pos val pos val BIT LIST pos val BIT - 1 0 1

  15. Example grammar dependency graph 0 NUM val pos val SIGN LIST0 neg pos val pos val LIST1 BIT2 • val and neg are synthesized attributes • pos is an inherited attribute • LIST0.pos is an inherited attribute with an empty dependency set. pos val pos val BIT1 LIST2 pos val BIT0 - 1 0 1

  16. Attribute grammars A topological order for the example 1. SIGN.neg 2. LIST0.pos 3. LIST1.pos 4. LIST2.pos 5. BIT0.pos 6. BIT1.pos 7. BIT2.pos 8. BIT0.val 9. LIST2.val 10. BIT1.val 11. LIST1.val 12. BIT2.val 13. LIST0.val 14. NUM.val 0 NUM val pos val SIGN LIST0 neg pos val pos val LIST1 BIT2 pos val pos val BIT1 LIST2 pos val BIT0 - 1 0 1 • Evaluate in this order • Yields NUM.val: -5

  17. Example grammar final result 0 NUM val :-5 pos: 0 val: 5 SIGN LIST0 neg : T pos: 1 val: 4 pos: 0 val: 1 LIST1 BIT2 pos: 1 val: 0 pos: 2 val: 4 BIT1 LIST2 pos: 2 val: 4 BIT0 The evaluation process is also called decorating the parse tree - 1 0 1

  18. S-Attributed SDD • An SDD is S-attributed if every attribute is synthesized • We can evaluate its attributes in any bottom-up order of the nodes of the parse tree • A postorder traversal • Postorder(N) { for (each child C of N, from the left) postorder(C); evaluate the attributes associated with node N;}

  19. L-Attributed SDD • L: dependency-graph edges can go from left to right, but not from right to left • Each attribute must be either: • Synthesized, or • Inherited, but with the rules limited as follows. Suppose production AX1X2…Xn and inherited attribute Xi.a computed by a rule which may only use: • Inherited attributes associated with head A • Either inherited or synthesized attributes associated with X1X2…Xi-1 • Either inherited or synthesized attributes associated with Xi, in a way that there’s no cycle

  20. L-Attributed example

  21. Example: Annotated parse tree for 3*5 T.val=15 F.Val=3 T’.inh=3T’.syn=15 digit.lexval=3 T1’.inh=15T1’.syn=15 * F.val=5 digit.lexval=5 

  22. L-Attributed example • Any SDD containing the following production and rules cannot be L-attributed. A B C A.s = B.b; B.i = f(C.c, A.s)

  23. Outline • Syntax-Directed Definitions • Evaluation Orders for SDD’s • Applications of Syntax-Directed Definitions • Syntax-Directed Translation

  24. Applications of Syntax-Directed Definitions • Type checking • Intermediate-code generation • Construction of abstract syntax trees

  25. Abstract Syntax Tree • An abstract syntax tree is the procedure’s parse tree with the nodes for most non-terminal symbols removed E.g., “a + 3 * b”

  26. Creating the AST (1 + 2 + (3 + 4)) + 5 S E + S ( S ) E + + 5 5 E + S 1 + 1 E + S 2 + 2 E 3 4 ( S ) E + S 3 E 4

  27. Construction of AST-- Example: S-Attributed Grammar • Ex: a-4+c

  28. num id id 4 + - a-4+cAST construction E nptr E nptr T nptr T nptr E - id T nptr To entry for c id To entry for a

  29. Class Exercise • Build the parse tree and abstract parse tree for ((a)+(b))

  30. Outline • Syntax-Directed Definitions • Evaluation Orders for SDD’s • Applications of Syntax-Directed Definition • Syntax-Directed Translation

  31. Syntax-Directed Translation • SDT: a context-free grammar with semantic actions embedded with production bodies • Complementary to SDD • Actions performed by a preorder traversal of the parse tree • Can be used to implement two classes of SDD’s • LR-parsable grammar: S-attributed SDD • LL-parsable grammar: L-attributed SDD

  32. Postfix Translation • Postfix SDT’s: SDT’s with all actions at the right ends of the production bodies • Ex: • LE n { print(E.val); }EE1+T { E.val=E1.val+T.val; }ET { E.val=T.val; }TT1*F { T.val=T1.val*F.val; }TF { T.val=F.val; }F(E) { F.val=E.val; }Fdigit { F.val=digit.lexval; }

  33. Parser-Stack Implementation of Postfix SDT’s • Postfix SDT’s can be implemented during LR parsing by executing the actions when reductions occur • To place the attributes along with the grammar symbols on the stack (Fig. 5.19)

  34. 5.4.2 Parser-Stack Implementation of Postfix SDT’s • Example 5.15: • Rewrite the actions of the desk-calculator so that they manipulate the parser stack explicitly.

  35. SDT’s with Actions inside Productions • Actions may be placed at any position in the body of productions • They are performed immediately after all symbols to its left are processed • BX {a} Y • a is done after we have recognized X • In bottom-up parse, perform a as soon as X appears on top of the stack • In top-down parse, perform a just before we expand Y

  36. SDT’s for L-Attributed SDD • Rules for turning L-attributed SDD into SDT • Embed the actions that computes the inherited attributes for nonterminal A immediately before A in the body of production • Place the actions that computes a synthesized attribute for the head of a production at the end of the body

  37. Example • Ex: grammar for boxes in typesetting language Eqn • BB1B2|B1 sub B2|(B1)|text

  38. SDD for Typesetting Boxes

  39. SDT for Typesetting Boxes

More Related