UNIT III SYNTAX ANALYSIS 2 Marks 1. Eliminate the left recursion from the following grammar A->Ac Aad bd c The rule to eliminate the left recursion is A Aα βâ can be converted as A βa and A αa ε. So the grammar after eliminating left recursion is A->bdA c A A ->c A ad A εå 2. What are the disadvantages of operator precedence parsing? The operator, like minus (unary and binary) has two different precedence. Hence it is hard to handle tokens like minus sign. This kind of parsing is applicable to only small class of grammars. 3. Construct a parse tree of (a+b)*c for the grammar E->E+E E*E (E) id. 4. Derive the string and construct a syntax tree for the input string ceaedbe using the grammar S->SaA A, A->AbB B, B->cSd e. Derivation: S=> A (S->A) => AbB (A->AbB) => BbB (A->B) => csdbb (B->cSd) => csaadbb (S->SaA) => caaadbb (S->A) => cbaadbb (A->B) => ceaadbb (B->e) =>ceabdbb (A->B) =>ceaedbb (B->e) =>ceaedbe (B->e)
5. List the factors to be considered for top-down parsing. Top down parsing is an attempt to find a leftmost derivation for an input string. Left recursive grammar can cause a top-down parser to go into an indefinite loop on writing procedure. Backtracking overhead may occur Due to backtracking, it may reject some valid sentences Left factoring Ambiguity The order in which alternates are tried can affect the language accepted When failure is reported, we have very little idea where the error actually occurred 6. What is an ambiguous grammar? A grammar G is said to be ambiguous if it generates more than one parse tree for some sentence of language L(G).i.e. both leftmost and rightmost derivations are same for the given sentence. 7. What is a predictive parser? It is a top down parser. It is a program based on a transition diagram attempts to match the terminal symbols against the input and makes a recursive procedure call whenever it has to follow an edge labeled by a non terminal. A non recursive predictive parser is a program that matches the terminal symbols against the input by maintaining a stack rather than using recursive calls. 8. What is CLR?
CLR stands for canonical LR parser. It is a bottom up parsing technique. L stands for left to right scanning of the input and R stands for constricting a rightmost derivation in reverse. 9. What do u meant by parser and its types? A parser for grammar G is a program that takes as input a string w and produces as output either a parse tree for w, if w is a sentence of G, or an error message indicating that w is not a sentence of G. it obtains a string of tokens from the lexical analyzer, verifies that the string generated by the grammar for the source language. a) Top down parsing b) Bottom up parsing 10. What are the different levels of syntax error handler? a) Lexical, such as misspelling an identifier, keyword, or operator. b) Syntactic, such as an arithmetic expression with unbalanced parentheses c) Semantic, such as operator applied to an incompatible operand d) Logical, such as an infinitely recursive call. 11. What are the goals of error handler in a parser? i) It should report the presence of errors clearly and accurately ii) It should recover from each error quickly enough to be able to detect subsequent errors iii) It should not significantly slow down the processing of correct programs. 12. What are error recovery strategies in parser? a) Panic mode b) Phrase level c) Error productions d) Global corrections 13. Define CFG? Many programming language has rules that prescribe the syntactic structure of well-formed programs. The syntax of programming language constructs can be described by CFG. Conditional statement defined by a rule such as ; If S1 and S2 are statements and E is an expression, then If E then S1 else S2 is a statement. 14. Mention the properties of parse tree. The root is labeled by the start symbol. Each leaf is labeled by a token or by. Each interior node is labeled by a non terminal.if A is the Non terminal, labeling some interior node and x1, x2, x3.xn are the labels of the children
15. When a grammar is said to be ambiguous? When a grammar produces more than one parse tree for a same sentence then the grammar is said to be ambiguous. 16. What do you mean by a syntax tree? Syntax tree is a variant of a parse tree in which each leaf represents an operand and each interior node represents an operator. 17. Compare production with reduction. The rules that define the ways in which the syntactic categories can be built are productions whereas the replacement of the string by an non-terminal according to a grammar production is called reduction. 18. What are the functions of parser? i. Representation of the tokens by codes (integers) ii. Grouping of tokens together into syntactic structure 19. Define handle pruning. A technique to obtain the rightmost derivation in reverse (called canonical reduction sequence) is known as handle pruning (i.e.) starting with a string of terminals w to be parsed. If w is the sentence of the grammar then = n where n is the nth right sentential form of unknown right most derivation. 20. List various types of grammars. i. Phase sensitive grammar ii. Context sensitive grammar iii. Context-free grammar iv. Regular grammar 21. What are the demerits of SLR? It will not produce uniquely defined parsing action tables for all grammars. Shift-Reduce conflict. 22. Why LR parsing is good and attractive? LR parsers can be constructed for all programming language constructs for which CFG can be written. LR parsing is Non-backtracking Shift-Reduce parsing. Grammars parsed using LR parsers are super set of the class of grammar. LR parser can detect syntactic error as soon as possible, when left-to-right scan of the input. 23. How will you change the given grammar to an augmented grammar? If G is a grammar with start symbol S then G is the augmented grammar for G, such that G is G with a new start symbol S and with production S S.
24. Find the item I0 for the following grammar using CLR parsing method. G: S AS S b A SA A a I0: S. S, $ S.AS, $ S.b, $ A.SA, a b A.a, a b S.AS, a b S.b, a b 25. Specify the advantages of LALR. Merging of states with common cores can never produce a shift/reduce conflict that was not present in any one of the original states. Because shift actions depends only one core, not the look ahead. 43. Mention the demerits of LALR parser. Merger will produce reduce / reduce conflict. On erroneous input, LALR parser may proceed to do some reductions after the LR parser has declared an error, but LALR parser never shift a symbol after the LR parser declares an error. 44. What is the syntax for YACC source specification program? Declarations %% Translation rules %% Supporting C-routines 45. How YACC resolves parsing action conflicts? A reduce/reduce conflict is resolved by choosing the conflicting production listed first in the YACC specification. A shift/reduce conflict is resolved by shifting action or introduce associativity and precedence. 46. What is the new production added to the YACC on error recovery When parser generated by YACC encounters errors, then YACC pops symbol from its stack until it finds the topmost state on its stack whose underlying set of items includes an item of the form A. error 47. Define LR parser?
LR parsers can be used to parse a large class of context free grammars. The technique is called LR (K) parsing. L denotes that input sequence is processed from left to right R denotes that the right most derivation is performed K denotes that atmost K symbols of the sequence are used to make a decision. 48. What are the drawbacks of LR parser? a) Parsing tables are too complicated to be generated by hand, need an automated parser generator. b) Cannot handle ambiguous grammar without special tricks. 49. Define Operator grammar. A grammar with no production right side is ε or no two adjacent Non Terminalsis called Operator grammar. 50. Define operator precedence grammar. Operator precedence grammar is an operator grammar having disjoint precedence relations between any pair of Terminals. 51.What are the different precedence relations in operator precedence parser? 52. What do you mean by precedence function? The precedence table can be encoded by two precedence functions f and g, that map the terminal symbols to integers. 16Marks 1. Construct predictive parsing table for the grammar S->(L) a L->L,S S Solution:
2. What are the different strategies that a parser can employ to recover from syntax errors? 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. Phrase level recovery On discovering an error, the parser may perform local correction on the remaining input; e.g. replace prefix of the remaining input by some string that allow the parser to continue Error productions Augment the grammar for the language with productions that generate the erroneous constructs. If it is being used by the parser, generate appropriate error diagnostics to indicate the erroneous construct that has been recognized in the input Global correction It does minimal changes in the incorrect input string to obtain a globally least-cost correction. 3. Construct the CLR parsing table from
4. Write Operator-precedence parsing algorithm.
5. Find the SLR parsing table for the given grammar and parse the sentence (a+b)*c. E->E+E E*E (E) id.
6. Given the following grammar S->AS b, A->SA a. Construct a SLR parsing table for the string baab.
7. Explain the role of parser in detail Parser obtains a string of tokens from the lexical analyzer and verifies that the string can be generated by the grammar for the source language. It can report any syntax error in an intelligible fashion. Errors can be of lexical, syntactic, semantic or logical. The error handler in a parser has simple-to-state goals: should report the presence of errors clearly and accurately should recover from each error quickly enough to be able to detect subsequent errors should not significantly slow down the processing of correct programs 8. Give the LALR parsing table for the grammar S-> L=R R L->*R id R->L.
1. What are the reasons for using LR parser technique? LR parsers can be constructed to recognize virtually all programming language constructs for which CFGs can be written LR parsing method is the most general non backtracking shift reduce parsing method known, yet it can be implemented as efficiently as other shift-reduce methods The class of grammars that can be parsed using LR methods is a proper superset of the class of grammars that can be parsed with predictive parsers
An LR parser can detect a syntactic error as soon as it is possible to do so on a left-to-right scan of the input 10. What is an operator grammar? Draw the precedence graph for the following table:
11. Write a note on error recovery in predictive parsing. An error is detected during predictive parsing, when the terminal on the top of the stack does not match the next input symbol or when non terminal A is on the top of the stack, a is the next input symbol, and the parsing table entry M [A, a] is empty. Panic mode error recovery is based on the idea of skipping symbols on the input until a token in a selected set of synchronizing tokens appears. Heuristics for choosing synchronizing token set: Place all the symbols in Follow(A) into synchronizing set for A Add to the synchronizing set of a lower construct the symbol that begin higher constructs Place all the symbols in First(A) into synchronizing set for A If a non terminal can generate the empty string, then the production deriving  can be used as default. If the terminal on top of stack does not match, just pop the terminal and issue a message saying that the terminal was inserted and continue parsing. Phrase-level recovery is implemented by filling in the blank entries in the predictive parsing table with pointers to error routines. These routines may insert, change or delete symbols on the input and issue appropriate error messages. 12. Write the LR parsing algorithm. Check whether the grammar is SLR (1) or not. Justify the answer with reasons. S->L=R R; L->*R id; R->L