2.37k likes | 2.55k Views
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.
E N D
<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 “(adjective, ) (noum, ) (verb, ) (adjective, ) (noum, )” ???? <sentence><Subject><Predicate> <adjective><noun><Predicate> <adjective><noun><verb><object> <adjective><noun><verb><adjective><noun> Leftmost Derivation Rightmost Reduction
<adjective><noun><verb><adjective><noun> • <Subject ><verb>< adjective><noun> • <Subject >< verb>< object> • <Subject><Predicate> <sentence> Leftmost Reduction
How can I design and code the “derivation” or “reduction”?
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!
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
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
3、Approached to implement a syntax analyzer Manual construction Construction by tools
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
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
3、Parsing methods (1)Top-Down (2)Bottom-Up
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
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
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
Top-Down: (1)Left-most derivation SxAy 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?
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 *,
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
x * y # x * y # controller rules controller rules # S # output output E.G.
x * y # x * y # x A y # controller rules controller rules x # output 1 E.G.
E.G. x * y # x * y # A y # controller rules * x # controller rules output 1
E.G. x * y # x * y # ** y # controller rules A x # controller rules 3 1,2
E.G. x * y # x * y # yA x # * y # controller rules controller rules 3 1,2
E.G. x * y # x * y # A y # controller rules controller rules S # 3,1 1
E.G. x * y # x * y # * y # controller rules S # controller rules 3,1 1,3
E.G. x * y # x * y # y # controller rules S # controller rules 3,1 1,3
E.G. x * y # x * y # # controller rules S # controller rules 3,1 1,3
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
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.
2、Main methods Predictive parsing (no backtracking) Recursive descent (involve backtracking)
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.
Grammar for Parsing Example StartExpr ExprExpr+Term ExprExpr-Term ExprTerm TermTerm* Int TermTerm/ Int Term Int • Set of tokens is { +, -, *, /, Int }, where Int = [0-9][0-9]*
Parsing Example Parse Tree Remaining Input Start <int,><-,><int, ><*,><int, > Sentential form Start Applied Production Current Position in Parse Tree
Parsing Example Parse Tree Remaining Input Start <int,><-,><int, ><*,><int, > Expr Sentential Form Expr StartExpr Current Position in Parse Tree
Parsing Example Parse Tree Remaining Input Start <int,><-,><int, ><*,><int, > Expr Sentential Form Expr - Term Expr - Term Applied Production ExprExpr+Term ExprExpr-Term ExprTerm ExprExpr-Term
Parsing Example Parse Tree Remaining Input Start <int,><-,><int, ><*,><int, > Expr Sentential Form Expr - Term Term - Term Term Applied Production ExprExpr+Term ExprExpr-Term ExprTerm ExprTerm
Parsing Example Parse Tree Remaining Input Start <int,><-,><int, ><*,><int, > Expr Sentential Form Expr - Term Int - Term Term Applied Production Int Term Int
Parsing Example Parse Tree Remaining Input Match Input Token! Start <int,><-,><int, ><*,><int, > Expr Sentential Form Expr - Term Int - Term Term Int
Parsing Example Parse Tree Remaining Input Match Input Token! Start <-,><int, ><*,><int, > Expr Sentential Form Expr - Term - Term Term Int
Parsing Example Parse Tree Remaining Input Match Input Token! Start <int, ><*,><int, > Expr Sentential Form Expr - Term Term Term Int
Parsing Example Parse Tree Remaining Input Start <int, ><*,><int, > Expr Sentential Form Expr - Term Term*Int Term Term * Int Applied Production Int TermTerm* Int
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
Parsing Example Parse Tree Remaining Input Match Input Token! Start <int, ><*,><int, > Expr Sentential Form Expr - Term Int* Int Term Term * Int Int Int
Parsing Example Parse Tree Remaining Input Match Input Token! Start <*,><int, > Expr Sentential Form Expr - Term * Int Term Term * Int Int Int
Parsing Example Parse Tree Remaining Input Match Input Token! Start <int, > Expr Sentential Form Expr - Term Int Term Term * Int Int Int
Parsing Example Parse Tree Remaining Input Parse Complete! Start <int, > Expr Sentential Form Expr - Term Term Term * Int Int Int
Backtracking Example Start <int,><-,><int, ><*,><int, > Start
Backtracking Example Start <int,><-,><int, ><*,><int, > Expr StartExpr
Backtracking Example Parse Tree Start <int,><-,><int, ><*,><int, > Expr Sentential Form Expr + Term Expr + Term ExprExpr+Term
Backtracking Example Parse Tree Remaining Input Start <int,><-,><int, ><*,><int, > Expr Sentential Form Expr + Term Term + Term Term Applied Production ExprTerm