1 / 234

Part 4 Syntax Analysis

Part 4 Syntax Analysis. < sentence >  <Subject><Predicate> <Subject>  <adjective><noun> <Subject>  <noun> <Predicate>  <verb><Object> <Object>  <adjective><noun> <Object>  <noun>. E.g. “young men like pop music”. Lexical Analyzer.

masako
Download Presentation

Part 4 Syntax Analysis

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. Part 4 Syntax Analysis

  2. <sentence><Subject><Predicate><Subject> <adjective><noun> <Subject> <noun> <Predicate> <verb><Object> <Object> <adjective><noun> <Object> <noun> E.g.

  3. “young men like pop music” Lexical Analyzer “(adjective, ) (noum, ) (verb, ) (adjective, ) (noum, )” ???? <sentence><Subject><Predicate>  <adjective><noun><Predicate>  <adjective><noun><verb><object>  <adjective><noun><verb><adjective><noun> Leftmost Derivation Rightmost Reduction

  4. <adjective><noun><verb><adjective><noun> • <Subject ><verb>< adjective><noun> • <Subject >< verb>< object> • <Subject><Predicate>  <sentence> Leftmost Reduction

  5. How can I design and code the “derivation” or “reduction”?

  6. 1、The syntax description of programming language constructs Context-free grammars 0 Approaches to implement a Syntax analyzer What is the definition of Context-free grammars? Please recall it!

  7. 2、Why a grammar is usually used to describe the syntax of a programming language? A grammar gives a precise ,yet easy-to-understand, syntactic specification of a programming language From certain classes of grammar we can automatically construct an efficient parser that determines if a source program is syntactically well formed

  8. A properly designed grammar imparts a structure to a programming language that is useful for the translation of source programs into correct object code and for the detection of errors The evolved constructs can be added to a language more easily

  9. 3、Approached to implement a syntax analyzer Manual construction Construction by tools

  10. 1、 Main task Obtain a string of tokens from the lexical analyzer Verify that the string can be generated by the grammar of related programming language Report any syntax errors in an intelligible fashion Recover from commonly occurring errors so that it can continue processing the remainder of its input 4.1 The Role of the Parser

  11. 2、Position of parser in compiler model token Rest of front end Intermediate representation Parse tree Lexical analyzer Source program Parser Get next token Symbol table

  12. 3、Parsing methods (1)Top-Down (2)Bottom-Up

  13. 4、Syntax Error handling 1) Error levels Lexical, such as misspelling an identifier, keyword, or operator Syntactic, such as an arithmetic expression with unbalanced parentheses Semantic, such as an operator applied to an incompatible operand Logical, such as an infinitely recursive call

  14. 2) Simple-to-state goals of the error handler It should report the presence of errors clearly and accurately It should recover from each error quickly enough to be able to detect subsequent errors It should not significantly slow down the processing of correct programs

  15. 3) Error-recovery strategies Panic mode Discard input symbols one at a time until one of a designated set of synchronizing tokens is found Phrase level Replace a prefix of the remaining input by some string that allows the parser to continue

  16. Top-Down: (1)Left-most derivation SxAy x*y Parser Tree Bottom-Up: (1)Left-most reduction x*y xAy Parser Tree Simple Instruction of Top-Down and Bottom-Up E.g. 1) S  xAy 2) A ** 3)A  *, and Verify“x*y” S x A y * How codes?

  17. x * y # x * y # controller rules controller rules # S # output output PDA Model Top-Down Bottom-Up 1,3 String Sentential form 3,1 ? ? 1) S  xAy 2) A ** 3)A  *,

  18. T-D PDA Controller IF “x” is the top symbol of the stack and is non-terminal,then find a production rule as “x……” randomly,replace “x” with the right of the rule,and output the No of the rule——derivation。 IF “x” is the top symbol of the stack and is same to that under the reading point,then… ——Matching。 IF (2) fail, then make a backtracking action to the scene before the last derivation and select a new rule—backtracking IF there no new rule, fail IF there is only “#” in the stack,and “#” is under the reading point ,success B-U PDA Controller IF the several top symbols in stack is a Handling, then reduction, elseif“#” is under the reading point then fail, elseMove the symbol under reading point into stack. IFthere is only #S in the stack,and “#” is under the reading point ,success Controller

  19. x * y # x * y # controller rules controller rules # S # output output E.G.

  20. x * y # x * y # x A y # controller rules controller rules x # output 1 E.G.

  21. E.G. x * y # x * y # A y # controller rules * x # controller rules output 1

  22. E.G. x * y # x * y # ** y # controller rules A x # controller rules 3 1,2

  23. E.G. x * y # x * y # yA x # * y # controller rules controller rules 3 1,2

  24. E.G. x * y # x * y # A y # controller rules controller rules S # 3,1 1

  25. E.G. x * y # x * y # * y # controller rules S # controller rules 3,1 1,3

  26. E.G. x * y # x * y # y # controller rules S # controller rules 3,1 1,3

  27. E.G. x * y # x * y # # controller rules S # controller rules 3,1 1,3

  28. Flaw of T-D Left Recursion Infinite loop Eliminating Left Recursion Backtracking inefficient Methods: Predictive and Eliminating Ambiguity Left common factor Flaw of B-U Next Discussion

  29. 1、Ideas E ( E ) E + E E * E i i i 4. 2 TOP-DOWN PARSING • Find a leftmost derivation for an input string E (E) (E+E) (E*E+E) ( i*E+E) ( i*i+E) ( i* i+ i) • Construct a parse tree for the input starting from the root and creating the nodes of the parse tree in preorder.

  30. 2、Main methods Predictive parsing (no backtracking) Recursive descent (involve backtracking)

  31. 3、Recursive descent A deducing procedure, which construct a parse tree for the string top-down from S. When there is any mismatch, the program go back to the nearest non-terminal, select another production to construct the parse tree If you produce a parse tree at last, then the parsing is success, otherwise, fail.

  32. Grammar for Parsing Example StartExpr ExprExpr+Term ExprExpr-Term ExprTerm TermTerm* Int TermTerm/ Int Term Int • Set of tokens is { +, -, *, /, Int }, where Int = [0-9][0-9]*

  33. Parsing Example Parse Tree Remaining Input Start <int,><-,><int, ><*,><int, > Sentential form Start Applied Production Current Position in Parse Tree

  34. Parsing Example Parse Tree Remaining Input Start <int,><-,><int,  ><*,><int,  > Expr Sentential Form Expr StartExpr Current Position in Parse Tree

  35. Parsing Example Parse Tree Remaining Input Start <int,><-,><int,  ><*,><int,  > Expr Sentential Form Expr - Term Expr - Term Applied Production ExprExpr+Term ExprExpr-Term ExprTerm ExprExpr-Term

  36. Parsing Example Parse Tree Remaining Input Start <int,><-,><int,  ><*,><int,  > Expr Sentential Form Expr - Term Term - Term Term Applied Production ExprExpr+Term ExprExpr-Term ExprTerm ExprTerm

  37. Parsing Example Parse Tree Remaining Input Start <int,><-,><int,  ><*,><int,  > Expr Sentential Form Expr - Term Int - Term Term Applied Production Int Term Int

  38. Parsing Example Parse Tree Remaining Input Match Input Token! Start <int,><-,><int,  ><*,><int,  > Expr Sentential Form Expr - Term Int - Term Term Int

  39. Parsing Example Parse Tree Remaining Input Match Input Token! Start <-,><int,  ><*,><int,  > Expr Sentential Form Expr - Term - Term Term Int

  40. Parsing Example Parse Tree Remaining Input Match Input Token! Start <int,  ><*,><int,  > Expr Sentential Form Expr - Term Term Term Int

  41. Parsing Example Parse Tree Remaining Input Start <int,  ><*,><int,  > Expr Sentential Form Expr - Term Term*Int Term Term * Int Applied Production Int TermTerm* Int

  42. Parsing Example Parse Tree Remaining Input Start <int,  ><*,><int,  > Expr Sentential Form Expr - Term Int * Int Term Term * Int Applied Production Int Term Int Int

  43. Parsing Example Parse Tree Remaining Input Match Input Token! Start <int,  ><*,><int,  > Expr Sentential Form Expr - Term Int* Int Term Term * Int Int Int

  44. Parsing Example Parse Tree Remaining Input Match Input Token! Start <*,><int,  > Expr Sentential Form Expr - Term * Int Term Term * Int Int Int

  45. Parsing Example Parse Tree Remaining Input Match Input Token! Start <int,  > Expr Sentential Form Expr - Term Int Term Term * Int Int Int

  46. Parsing Example Parse Tree Remaining Input Parse Complete! Start <int,  > Expr Sentential Form Expr - Term Term Term * Int Int Int

  47. Backtracking Example Start <int,><-,><int,  ><*,><int,  > Start

  48. Backtracking Example Start <int,><-,><int,  ><*,><int,  > Expr StartExpr

  49. Backtracking Example Parse Tree Start <int,><-,><int,  ><*,><int,  > Expr Sentential Form Expr + Term Expr + Term ExprExpr+Term

  50. Backtracking Example Parse Tree Remaining Input Start <int,><-,><int,  ><*,><int,  > Expr Sentential Form Expr + Term Term + Term Term Applied Production ExprTerm

More Related