Question 60 used in parsing? Explain What is the different error recovery technique them CSVTU in bt May200g1 Or Discuss the mechanism by which each phase of the compiler can detect errors recover from them? CSVIUMay2010) and
Ans. Error Recovery in LR Parsing
An LR parser will detect an error when it consults the parsing action table and find, an error entry. Allempty entries in the action table are error entries. Errors are never detected by consulting the goto table. An LR parser will announce error as soon as there is no valid continuation for the scanned portion of the input. A canonical LR parser (LR(1) parser) will never make even a single reduction bet announcing an error. The SLR and LALR parsers may make several reductions before announcing an or But, all LR parsers (LR(1), LALR and SLR parsers) will never shift an erroneous inm symbol onto the stack. Panic Mode Error Recovery in LR Parsing Scan down the stack until a state s with a goto on a particular nonterminal A is found (Get rid of everything from the stack before this state s). Discard zero or more input symbols until a symbol a is found that can legitimately follow A. The symbol a is simply in FOLLOW(A), but this may not work for all situations. The parser stacks the nonterminal A and the state goto[s, A), and it resumes the normal parsing. PUBLICATIONS TM 2- 55 Syntax Analysis & Parsing ACADEMY Techniques eATE a This nonterminal A is normally is basic programming block (there can be more than one choice for A). expr, block, () stmt, Phrase-Level Error Recovery in LR Parsing Each empty entry in the action table is marked with a specific error routine. ormor routine reflects the error that the user most likely willmake in An error routine that case inserts the symbols into the stack or the input (or it deletes the svmbols from the stack and the input, or it can do both insertion and deletion). () missing operand. (i) unbalanced right parenthesis. Recovery from syntax error : For some examples of common syntax errors can consider punctuation errors. example, using a comma instead For of a semicolon in the argument list of a function declaration, leaving out a mandatory semicolon at the end of the line or using an extraneous semicolon. Misspelling of keywords : A common technique is to print the offending line with a pointer to the position of errors. The parser might add a diagnostic message like "semicolon missing at position" if it knows what the error is. Kecovery from semantic errors : There are errors which can be detected both at COmpile time and run time. Most common error of compile time are scope declaration errors. For example undeclared or and mismatch is another multiple declared identifiers. Type compile time error. Kecovery from an undeclared name is straight forward, it is suggested to entry of declaration in make the symbol table with appropriate attribute. Sr. No. Phase/Routine Errors 1. Lexical analysis Misspelled next token e.g, misspelled identifier, keyword, or printer. Missing parenthesis e.g., in MIN (A,2 * (3+ B), 2. Syntax analysis Erroneous arithmeticexpressions an arithmetic expression with unbalanced parentheses etc. 3 Semantic analysis Type mismnatch, Errors of declaration and scope 4 Intermediate An operator whose operands have incompatible code generation types 5. Code Not reachable statements due to wrongly written Optimization function calls me i.e. too large to fit in 6. Code generation A compiler-created constant a word of the target machine may be found. contradictory 7. Symbol table A multiply declared identifier with attributes. GATE ACADEMY PUBLICATIONS Compiler Design (CSE) 2-50 Ans. Sr. No. Canonical LR Parsers LALR Parsers SLR Parsers 1. SLR parser are easiest to CLR parsers are LALR parsers are implement. difficult to implement. difficult to implement than SLR parser but less than CLR parsers. 2. SLR parsers mnake use of CLR parsers are uses LALR parsers use LR(U) collection canonical collection of| LR(1) collection of of LR(0) items for items for constructing items with items constructing the parsingthe parsing tables. having same Core tables. merged into a single itemset. 3. In SLR parsers don't do In CLR parsers In LALR parsers any lookahead i.e., they lookahead is one lookahead is one lookahead zero. symbol. symbol. 4. SLR parsers are cost CLR parsers are The cost of effective to construct in expensive to construct constructing LALR terms of time and space. in termns of time and parsers is space. intermediate between SLR and CLR parser. 5. SLR parsers have CLR parses have LALR parsers have hundreds of states. thousands of states. hundreds of state and is Same as number states Sn SLR parsers. 6. SLR parsers uses CLR parsers uses LALR parsers uses FOLLOW information to lookahead symbol to lookahead symbol to guide reductions. guide reductions. guide reductions. 7. SLR parsers may fail to CLR parser works on LALR parser works produce a table for very large class of on very large class certain class of grammar. grammnars. grammars on which other succeed. 8 Every SLR() grammar is Every LR() grammar Every LALR(1) LR() grammar and may not be SLR( 1) grammar may not be LALR(I). grammar. SLR(1) but every LALR() grammar is LR() grammar. PUBLICATIONS TM ACADEMY 2-51 Syntax Analysis & Parsing GATE Techniques shift-reduce A shift-reduce or A shift-reduce reduce-reduce conflict reduce-reduce conflict may arise in conflict can not arise SLRparsing may arse but chances but a reduce-reduce table. are less than that in conflict may SLR parsing tables. arise. 10. SLR parser is least ACLR parsers powerful. is most]A LALR parser powerful among is the intermediate family carnonical of power between in bottom-up parsers SLR and LR parser. Question 56 Construct SLR parse table for the following grammar. E E+TIT, JCSVTID