Chapter 4: Syntax Analyzer Chapter 4: Syntax Analysis 1 The role of the Parser The parser obtains a string of tokens from the lexical analyzer, and verifies that the string can be generated by the grammar of the source language. Chapter 4: Syntax Analysis 2 1
The role of the Parser Also, the parser should report any syntax errors in an intelligible fashion. It should also recover from commonly occurring errors so it can continue processing the remainder of its input. Chapter 4: Syntax Analysis 3 Syntax Error Handling A good compiler should assist the programmer in identifying and locating errors. Chapter 4: Syntax Analysis 4 2
Errors Types Lexical: such as misspelling id, kw, or opr. Syntactic: such as arith. Exp with unbalanced parentheses Semantic: such as an operator applied to an incompatible operand. Logical: such as an infinitely recursive call. Chapter 4: Syntax Analysis 5 Syntax Error Handling The error handler in a parser has the following simple goals: it should report the presence of an error 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 Chapter 4: Syntax Analysis 6 3
Syntax Error Handling How should an error handler report the presence of an error? It should report the place in the source program where an error is detected because there is good chance that actual error occurred within the previous few tokens, the error handler also give an understandable error message when applicable. Chapter 4: Syntax Analysis 7 Example If x>y then z =10 // ; missing -------------------------------------------------- { { { } } // unexpected end of source Chapter 4: Syntax Analysis 8 4
Error Recovery Strategies no one strategy has proven itself to be universally acceptable: Panic-mode Recovery Phrase level Recovery Chapter 4: Syntax Analysis 9 Panic-mode Recovery On discovering an error, the parser discards input symbols one at a time until one of a designated set of synchronizing tokens is found Synchronizing tokens: delimiter, semicolon, or end. Chapter 4: Syntax Analysis 10 5
Phrase level Recovery On discovering an error, a parser may perform local correction of the remaining input, that is, it may replace a prefix of the remaining input by some string that allows the parser to continue. Replacing, by ; Inserting missing ; Delete an extraneous ; Chapter 4: Syntax Analysis 11 Context Free Grammars RE: for defining the tokens constructs (id, num, Relop) CFG: for describing the syntax of language constructs ( exp, statement) Chapter 4: Syntax Analysis 12 6
Backtracking Problem Backtracking occurs when there are more than one alternative start with the same prefix. Chapter 4: Syntax Analysis 13 Backtracking Problem Input: if E1 then S1 else S2 Since that there are more than one alternative starts with the same prefix, the compiler selects them one alternative if this alternative does not match with the input then the compiler backtracks to the beginning of input and selects the second alternative Chapter 4: Syntax Analysis 14 7
Backtracking Problem The compiler repeats this process until match found between the input and an alternative or trying all alternatives with no match found declaring that the input is wrong. Chapter 4: Syntax Analysis 15 LEFT FACTORING A αβ1 αβ2 A α A' A' β1 β2 depending on input to chose β1 or β2 Chapter 4: Syntax Analysis 16 8
LEFT FACTORING <stmt> if <exp> then <stmt> else <stmt> if <exp> then <stmt> ============================= <stmt> <stmt> ' if <exp> then <stmt> <stmt> ' else <stmt> ε Chapter 4: Syntax Analysis 17 The Modified Grammar Chapter 4: Syntax Analysis 18 9
Example S I E+ S I E +Se S a E b ============================ S I E +S S' a S' e S ε E b Chapter 4: Syntax Analysis 19 Ambiguity Example Some times certain conditions can be set on the grammar to eliminate the ambiguity. Input: If E1 then if E2 then S1 else S2 Chapter 4: Syntax Analysis 20 10
Chapter 4: Syntax Analysis 21 Eliminating the Ambiguity Associate each else with the closest previous then Chapter 4: Syntax Analysis 22 11
LEFT RECURSION A grammar is left recursive if it has a non terminal A such that there is a derivation A A α for some string α. Top - down parsing methods cannot handle left- recursive grammar, why? Chapter 4: Syntax Analysis 23 LEFT RECURSION A A α β α, β are sequence of terminals and non terminals that do not start with A. <exp> <exp> +<term> <term> A= <exp>, α = + <term>, β =<term> Chapter 4: Syntax Analysis 24 12
Elimination of Left Recursion Rewrite the production for A in the following manner. A β A' A' α A' ε right recursive. Chapter 4: Syntax Analysis 25 Example 1 <exp> <exp> ' <term> <exp> ' + <term> <exp> ' ε Chapter 4: Syntax Analysis 26 13
Example 2 E E+T T T T*F F F (E) Id Chapter 4: Syntax Analysis 27 Example 2 Chapter 4: Syntax Analysis 28 14
TOP DOWN Parsing Top down parsing can be viewed as an attempt to find a left most derivation for an input string. It can be viewed as an attempt to construct a parse tree for the input starting from the root and creating nodes of the parse tree in preorder. Chapter 4: Syntax Analysis 29 Recursive descent parsing is a top down method of syntax analysis in which we execute a set of recursive procedures to process the input. A procedure is associate with each nonterminal of a grammar. Chapter 4: Syntax Analysis 30 15
Predictive parsing is a special form of recursive descent parsing in which the look-ahead symbol unambiguously determines the procedure selected for each nonterminal. (No backtracking is required). Chapter 4: Syntax Analysis 31 Non recursive predictive parsing The key problem during predictive parsing is that in determining the production to be applied for a nonterminal. The non-recursive parser looks up the production to be applied in a parsing table. Chapter 4: Syntax Analysis 32 16
Predictive parser By carefully writing grammar, eliminating left recursion from it and left factoring the resulting grammar, we can obtain a grammar that can be parsed by a recursive descent parser that needs no backtracking. i. e.,a predictive parser. The proper alternative must be detectable by looking at only the first symbol it derives. Chapter 4: Syntax Analysis 33 Predictive parser First (<stmt>) = {if, while, begin} The key words if, while and begin tell us which alternative is the only one that could possibly succeed if we are to find a statement. Chapter 4: Syntax Analysis 34 17
Pseudo code for A predictive Parser Consider the following grammar: <state>::=if <exp> then <state> {else <state>}; while <exp> do <state>; <id>:=<exp>; <exp>::=<id>{+ <id>} Chapter 4: Syntax Analysis 35 Pseudo code for A predictive Parser Match (t: token) { If lookahead = t then Lookahead = next token(); else error; } Chapter 4: Syntax Analysis 36 18
Pseudo code for A predictive Parser parsesate( ) { if lookahead=if then parseif(); else if lookahead=while then parsewhile(); else if lookahead=id then parseass(); else Error; } Chapter 4: Syntax Analysis 37 Pseudo code for A predictive Parser parseif() { match(if); parseexp(); match(then); parse(state); if lookahead=else then { match(else); parse(state); } match(;); } Chapter 4: Syntax Analysis 38 19
Pseudo code for A predictive Parser parsewhile() { match(while); parseexp(); match(do); parse(state); match(;); } Chapter 4: Syntax Analysis 39 Pseudo code for A predictive Parser parseass() { match(id); match(ass); parseexp(); match(;); } Chapter 4: Syntax Analysis 40 20
Pseudo code for A predictive Parser parseexp( ) { match(id); if lookahead=+ then { match(+); match(id); } } Chapter 4: Syntax Analysis 41 21