1 / 46

Modern Compiler Design

2. Administrative. PA1 grades are now availablequestions to compiler@post.tau.ac.ilPA2 do not use %lineTA1 published today due 14/12/05PA3 published today due 21/12/05. 3. Today. Lexical Analysis. Syntax AnalysisParsing. AST. Symbol Table etc.. Inter. Rep.(IR). CodeGen.. Goals:Bottom-up parsing - solutions for conflictsJavaCuplearn how to write a spec for JavaCupJLex/JavaCup integrationPA3 guidelinesCool AST designconstruction.

bly
Download Presentation

Modern Compiler Design

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. 1 Mooly Sagiv and Greta Yorsh School of Computer Science Tel-Aviv University gretay@post.tau.ac.il http://www.cs.tau.ac.il/~gretay Modern Compiler Design

    2. 2 Administrative PA1 grades are now available questions to compiler@post.tau.ac.il PA2 do not use %line TA1 published today due 14/12/05 PA3 published today due 21/12/05

    3. 3 Today Goals: Bottom-up parsing - solutions for conflicts JavaCup learn how to write a spec for JavaCup JLex/JavaCup integration PA3 guidelines Cool AST design construction

    4. 4 Recap What is the role of parser ? not all strings of tokens are programs… distinguish between valid and invalid sequences of tokens semantic actions for valid sequences of tokens What is the language for describing valid strings of tokens ?

    5. 5 Solutions for Conflicts Stronger parser algorithm Patch parsing table to reduce Develop equivalent non-ambiguous grammar Precedence and associativity of tokens

    6. 6 Simplified If-Then-Else Grammar terminals: if then else c c1 c2 a a1 a2 non-terminals: S C Which state of the deterministic automaton is reached on the input, which dotted items can be reached ?Which state of the deterministic automaton is reached on the input, which dotted items can be reached ?

    7. 7 Patch parsing table ? Reduce rejects some correct inputs, which ? Shift gives intended meaning default choice of Cup solves all shift/reduce conflict in the same state precedence of tokens: solve ambiguous grammar without rewriting it solve shift-reduce or reduce/reduce conflicts in non-ambiguous grammar precedence of tokens: solve ambiguous grammar without rewriting it solve shift-reduce or reduce/reduce conflicts in non-ambiguous grammar

    8. 8 Dangling Else/If-Else Ambiguity

    9. 9 Dangling Else/If-Else Ambiguity Describes the same set of stirngsDescribes the same set of stirngs

    10. 10 Dangling Else/If-Else Ambiguity

    11. 11 Dangling Else/If-Else Ambiguity

    12. 12 Dangling Else/If-Else Ambiguity

    13. 13 Precedence and Associativity Precedence E?E+E?*E E?E+E? Reduce + preceeds * Shift * preceeds +

    14. 14 Example

    15. 15 Precedence and Associativity Associativity E?E+E?+E E?E+E? Shift + right-associative Reduce + left-associative Precedence E?E+E?*E E?E+E? Reduce + preceeds * Shift * preceeds +

    16. 16 JavaCup Parser generator Generates an LALR Parser Input: spec file Output: parser/syntax analyzer

    17. 17 Syntax Analysis with JavaCup Constructor for Userful ParsersConstructor for Userful Parsers

    18. 18 JavaCup Spec File Package and import specifications User code components Terminal and non-terminal lists define building-blocks of the grammar types of semantic values Precedence declarations may help resolve conflicts The grammar may introduce conflicts that have to be resolved semantic actions Java code e.g. AST construction

    19. 19 import java_cup.runtime.*; terminal Integer NUMBER; terminal PLUS,MINUS,MULT,DIV; terminal LPAREN, RPAREN; terminal UMINUS; non terminal Integer expr; precedence left PLUS, MINUS; precedence left DIV, MULT; Precedence left UMINUS; expr ::= expr PLUS expr | expr MINUS expr | expr MULT expr | expr DIV expr | MINUS expr %prec UMINUS | LPAREN expr RPAREN | NUMBER ; Expression Calculator

    20. 20 Assigning Meaning Labels – refer to semantic values of the tokens Actions – executed on reduce RESULT

    21. 21 Lexer Integration Parser gets terminals from the Lexer

    22. 22 PA3 Guidelines RTFM

    23. 23 PA3 Guidelines PA3.ps PA3.tar.gz – install and read the files cool.cup AST package, esp. TreeNode.java, cool-tree.java mycoolc Cool Manual in particular Figure 1 T5.ppt (this presentation) JavaCup Manual esp. Errror Recovery Relevant discussions in our forum

    24. 24 PA3 – Step 1 Understand Cool grammar in the manual Don’t touch the keyboard before understanding spec Write a debug javaCup spec for Cool grammar a spec with “debug actions” : print-out debug messages to understand what’s going on Try “debug grammar” on a number of test cases Keep a copy of “debug grammar” spec around

    25. 25 PA3 – Step 2 Understand the class hierarchy for the AST Don’t touch the keyboard before you understand the hierarchy Keep in mind that this is the basis for later stages Change your javaCup actions to construct the AST Perform error recovery use javaCup error token

    26. 26 cool.cup - symbol definitions ... terminal PLUS, DIV, MINUS, MULT, EQ, LT, DOT, NEG, COMMA, SEMI, COLON; terminal LPAREN, RPAREN, AT, LBRACE, RBRACE; terminal AbstractSymbol STR_CONST, INT_CONST; terminal Boolean BOOL_CONST; terminal AbstractSymbol TYPEID, OBJECTID; nonterminal program program; nonterminal Classes class_list; nonterminal Class_ class; nonterminal Expressions stmt_list; nonterminal Expression expr;

    27. 27 cool.cup – grammar and actions

    28. 28 User code parser code {: <code1> :} copied as is into the parser class error handling action code {: <code2> :} copied as is into the action class functions and variables used in semantic actions public class CoolParser { < code1 > … } class Action { < code2 > … }

    29. 29 Error Handling parser code {: // called when syntax error detected, before recovery public void syntax_error(Symbol cur_token) { … } // called if unable to recover public void unrecovered_syntax_error(Symbol cur_token) { … } :}

    30. 30 Error Recovery ERROR terminal returned by the lexer but never used in the parser parse error when lexer returns ERROR error nonterminal discarding part of the semantic context and part of the input and continue parsing matching erroneous tokens with error consecutive errors - suppress error message

    31. 31 stmt_list ::= expr:e SEMI {: RESULT = (new Expressions(..)).appendElement(e); :} | stmt_list:sl expr:e SEMI {: RESULT = sl.appendElement(e); :} | error SEMI {: RESULT = new Expressions(curr_lineno()); :} | stmt_list:sl error SEMI {: RESULT = sl; :} ; Error Recovery Example

    32. 32 AST Construction A more useful representation of the syntax tree Less clutter Actual level of details depends on your design Evaluate expression by AST traversal Traversal for debug printing Basis for semantic analysis Later – annotate AST Type information Computed values

    33. 33 Parse Tree vs. AST

    34. 34 AST Construction AST Nodes constructed during parsing Bottom-up parser Grammar rules annotated with actions for AST construction When node is constructed all children available (already constructed) Top-down parser More complicated

    35. 35 AST Construction

    36. 36 (Most of) Cool Syntax

    37. 37 Cool AST

    38. 38 Rules of Thumb for Designing an AST Base class abstract class TreeNode { ... } Class for AST root abstract class Program extends TreeNode {…} Class for each non-terminal

    39. 39 Rules of Thumb for Designing an AST Class for each non-terminal or group of related non-terminals with similar functionality abstract class Expression extends TreeNode abstract class Feature extends TreeNode abstract class Class_ extends TreeNode

    40. 40 Abstract class for non-terminals with alternative feature ::= method | attribute abstract class Feature extends TreeNode {…} class method extends Feature {…} class attr extends Feature {…} Rules of Thumb for Designing an AST

    41. 41 Rules of Thumb for Designing an AST Base class Class for AST root Class for each non-terminal or group of related non-terminals with similar functionality Abstract class for non-terminals with alternative Remember : AST is constructed bottom-up when constructing a node children nodes already constructed also – parent not constructed yet

    42. 42 Class Hierarchy for Cool AST

    43. 43 Class Hierarchy for Cool AST

    44. 44 Cool AST

    45. 45 Cool AST

    46. 46 Cool AST

    47. 47 Summary JavaCup LALR(1) parser generator Interfaces with JLex Your grammar may require conflict resolution Some may be resolved by operator precedence Other may require other techniques Grammar may “drive” semantic actions Assign meaning to syntactical entities Construct AST AST should be carefully designed

More Related