syntax directed analysis
play

Syntax Directed Analysis Chapter 5 1 Compiler Construction Syntax - PowerPoint PPT Presentation

Syntax Directed Analysis Chapter 5 1 Compiler Construction Syntax Directed Analysis Syntax-Directed Definitions Generalizes a context-free grammar Each grammar symbol has an associated set of attributes Synthesized attributes


  1. Syntax Directed Analysis Chapter 5 1 Compiler Construction Syntax Directed Analysis

  2. Syntax-Directed Definitions • Generalizes a context-free grammar • Each grammar symbol has an associated set of attributes – Synthesized attributes – Inherited attributes • The node for a grammar symbol in a parse tree holds information (attributes) 2 Compiler Construction Syntax Directed Analysis

  3. Syntax-directed Translation • Syntax-directed: Augment the productions of a CFG with semantic rules or actions These rules and/or actions define the values of attributes and how to compute them • The augmented grammar is called an attribute grammar • Often a language specification provides the CFG for a language and an English language description of the semantics A compiler writer must deduce an attribute grammar from the English description 3 Compiler Construction Syntax Directed Analysis

  4. Attributes • Value can be – string – number – type – etc. • Values in parse tree node depend on the grammar production responsible for that node 4 Compiler Construction Syntax Directed Analysis

  5. Example: Attributes of an Identifier • Name (lexeme from scanner) • Type – Basic type: int , float , etc. – Array ∗ Dimension ∗ Type – Structured type ( struct , class ): ∗ Field name ∗ Type of fi eld – Procedure (function, method, etc.): ∗ Number and types of parameters ∗ Return type ∗ Size of stack frame • Scope 5 Compiler Construction Syntax Directed Analysis

  6. Synthesized vs. Inherited Attributes • Synthesized – Computed from attributes of child nodes • Inherited – Computed from attributes of sibling nodes and parent node = position + initial * rate 60 6 Compiler Construction Syntax Directed Analysis

  7. Semantic Rules A semantic rule can be associated with a production: Production Semantic Rule E → E 1 + T E . val ← E 1 . val + T . val E → T E . val ← T . val T → T 1 ∗ F T . val ← T 1 . val × F . val T → F T . val ← F . val F → ( E ) F . val ← E . val F → number F . val ← number . val 7 Compiler Construction Syntax Directed Analysis

  8. Example: Declarations Productions: → decl decl type id list ; → id , id list | id id list type id_list ; τ 1 | ... | τ n → type τ id id_list id id_list Semantic Rules: ... id → decl type id list ; { id list.tval ← type.tval } id_list → id , id list 1 | id id list id { id . tval ← id list.tval ; id list 1 . tval ← id list.tval } 8 Compiler Construction Syntax Directed Analysis

  9. Dependencies • Semantic rules set up dependencies among attributes • These dependencies can be represented by a directed graph = position + initial * rate 60 9 Compiler Construction Syntax Directed Analysis

  10. Annotated Parse Tree • Each node of the parse tree has attributes • Such a parse tree is said to be annotated or decorated 10 Compiler Construction Syntax Directed Analysis

  11. Syntax-Directed Definition Each production A → α has an associated a set of semantic rules of the form b = f ( c 1 , c 2 , c 3 ,..., c k ) 11 Compiler Construction Syntax Directed Analysis

  12. Synthesized Attribute In a production of the form A → α an associated semantic rule has the form b = f ( c 1 , c 2 ,..., c k ) • b is a synthesized attribute of A • c 1 , c 2 ,..., c k are attributes belonging to the grammar symbols of the production 12 Compiler Construction Syntax Directed Analysis

  13. Inherited Attribute In a production of the form A → α an associated semantic rule has the form b = f ( c 1 , c 2 ,..., c k ) • b is an inherited attribute of one of the grammar symbols on the right side of the production • c 1 , c 2 ,..., c k are attributes belonging to the grammar symbols of the production 13 Compiler Construction Syntax Directed Analysis

  14. Attributed Evaluation • Unrestricted definition – No restrictions on attribute dependency – Perform a topological sort on the attribute dependency graph and evaluate in topological order – Expensive to evaluate • S-attributed definition – Synthesized attributes only – Attributes may be evaluated bottom-up – Evaluated very efficiently • L-attributed definition – Permits both synthesized and some inherited attributes Inherited attributes restricted to left siblings – Attributes may be evaluated depth-first, left to right Any L-attributed definition may be converted to an S-attributed definition 14 Compiler Construction Syntax Directed Analysis

  15. S-attributed Definition • A syntax-directed definition that uses synthesized attributes exclusively • S stands for synthesized attributes • Attributes are computed from the bottom up • An LR parser can be adapted to implement an S-attributed definition • See example on Pages 281-282 • Do a postorder traversal of the syntax tree to evaluate the attributes 15 Compiler Construction Syntax Directed Analysis

  16. S-attributed Evaluation • All semantic actions follow the right-hand side of a grammar rule • Yacc uses a value stack that parallel’s the LALR parser’s state stack – Values of synthesized attributes are stored on the stack – For a reduction: ∗ Pop attributes of children off the stack ∗ Compute synthesized attributes for this rule ∗ Push computed synthesized attributes on to the stack 16 Compiler Construction Syntax Directed Analysis

  17. S-attributed Evaluation: Yacc Example Expression: NUMBER { $$ = new NumberExpressionNode( yylval.token_info.lexeme, yylval.token_info.line_number); } | Name LPAREN ArgList RPAREN { $$ = new CallExpressionNode( (NameNode *) $1, (ArgListNode *) $3); } | Expression SumOp Expression %prec PLUS { $$ = new SumOpExpressionNode( (ExpressionNode *) $1, (SumOpNode *) $2, (ExpressionNode *) $3); } | Expression ProductOp Expression %prec TIMES { $$ = new ProductOpExpressionNode( (ExpressionNode *) $1, (ProductOpNode *) $2, (ExpressionNode *) $3); } ; 17 Compiler Construction Syntax Directed Analysis

  18. Inherited Attributes • Convenient for expressing the dependence of a programming language construct on the context in which it appears • It is always possible to rewrite a syntax-directed definition to use only synthesized attributes, but it is often more convenient to use inherited attributes • See example on Page 283 18 Compiler Construction Syntax Directed Analysis

  19. L-attributed Grammars Given a production of the form A → X 1 X 2 ... X n an inherited attribute of X j for 1 ≤ j ≤ n is a function of 1. the attributes of the grammar symbols X 1 , X 2 ,..., X j − 1 to the left of X j and 2. the inherited attributes of left side non-terminal A • S-attributed grammars are a subset of these • Semantic actions amy be placed anywhere on the right-hand side of a production • Evaluate with a depth-first traversal 19 Compiler Construction Syntax Directed Analysis

  20. Code Generation from L-attributes Grammar rule: while stmt → while ( expr ) stmt L-attributed grammar rule: while stmt → while ( { fprintf(fout, "__while_top%u:\n", label++); } ) expr { fprintf(fout, "lw $a1 %s\n", temp); fprintf(fout, "beqz $a1 __end_while%u\n", label); } stmt { fprintf(fout, "j __while_top%u\n", label); fprintf(fout, "__end_while%u:\n", label); } 20 Compiler Construction Syntax Directed Analysis

  21. Converting from L-attributed to S-attributed Definition Recall that • In an S-attributed definition the semantic action(s) must appear after all the grammar symbols on the right-hand side of a production • In an L-attributed definition the semantic action(s) may appear anywhere on the right-hand side of a production • Replace a rule such as B { print("Make A") } C → A with two rules → A B D C ǫ { print("Make A") } → D 21 Compiler Construction Syntax Directed Analysis

  22. Evaluation of Attributes When you visit a node evaluate its inherited attributes preorder and its synthesized attributes postorder AttributeEvaluate( Node v ) { for each child w of v from left to right do { Evaluate the inherited attributes of w ; AttributeEvaluate( w ); } Evaluate the synthesized attributes of v ; } In general, inherited attributes are difficult to deal with, especially for bottom-up parsers 22 Compiler Construction Syntax Directed Analysis

  23. Intermediate Representations • Useful for syntax-directed translation • Decouples translation from parsing • Grammars often do not accurately reflect natural program structure • An abstract syntax tree (AST) is an example 23 Compiler Construction Syntax Directed Analysis

  24. Abstract Syntax Tree A parse tree with non-essential information removed (a + b) * c Parse Tree AST E * + id E E * id id ( ) E id E E + id id 24 Compiler Construction Syntax Directed Analysis

  25. AST vs. Parse Tree • Operators are promoted from leaves to internal nodes • Chains of single productions are collapsed • Syntactic details like parentheses, semi-colons, and commas are omitted • Subtree lists are flattened 25 Compiler Construction Syntax Directed Analysis

  26. AST Construction: Operator Promotion Eliminate operator leaf nodes and promote them to interior nodes E * E E E E * 26 Compiler Construction Syntax Directed Analysis

  27. AST Construction: Collapse Child Chains Collapse chains of single children, replacing the chains with their leaf nodes E E E E + id + id id id 27 Compiler Construction Syntax Directed Analysis

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend