Compiler Design Important Questions — 2
- Define parser.
Hierarchical analysis is one in which the tokens are grouped hierarchically into nested collections with collective meaning.
Also termed as Parsing.
- Mention the basic issues in parsing.
There are two important issues in parsing.
• Specification of syntax
• Representation of input after parsing.
- Why lexical and syntax analyzers are separated out?
Reasons for separating the analysis phase into lexical and syntax analyzers:
* Simpler design.
* Compiler efficiency is improved.
* Compiler portability is enhanced.
- Define a context free grammar.
A context free grammar G is a collection of the following
• V is a set of non terminals
• T is a set of terminals
• S is a start symbol
• P is a set of production rulesG can be represented as G = (V,T,S,P)
Production rules are given in the following form
Non terminal → (V U T)*
- Briefly explain the concept of derivation.
Derivation from S means generation of string w from S. For constructing derivation two things are important.i) Choice of non terminal from several others.
ii) Choice of rule from production rules for corresponding non terminal.
Instead of choosing the arbitrary non terminal one can choose
i) either leftmost derivation — leftmost non terminal in a sentinel form
ii) or rightmost derivation — rightmost non terminal in a sentinel form
- Define 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.
- What is a operator precedence parser?
A grammar is said to be operator precedence if it possess the following properties:
- No production on the right side is ε.
- There should not be any production rule possessing two adjacent non terminals at the right hand side.
- List the properties of LR parser.
* LR parsers can be constructed to recognize most of the programming languages for which the context free grammar can be written.
* The class of grammar that can be parsed by LR parser is a superset of class of grammars that can be parsed using predictive parsers.
* LR parsers work using non backtracking shift reduce technique yet it is efficient one. - Mention the types of LR parser.
• SLR parser- simple LR parser
• LALR parser- lookahead LR parser
• Canonical LR parser - What are the problems with top down parsing?
The following are the problems associated with top down parsing:
• Backtracking
• Left recursion
• Left factoring
• Ambiguity
- Write the algorithm for FIRST and FOLLOW.
FIRST
- If X is terminal, then FIRST(X) IS {X}.
- If X → ε is a production, then add ε to FIRST(X).
- If X is non terminal and X → Y1,Y2..Yk is a production, then place a in FIRST(X) if for some i , a is in FIRST(Yi) , and ε is in all of FIRST(Y1),…FIRST(Yi-1);
FOLLOW
- Place $ in FOLLOW(S),where S is the start symbol and $ is the input right endmarker.
- If there is a production A → αBβ, then everything in FIRST(β) except for ε is placed in FOLLOW(B).
- If there is a production A → αB, or a production A→ αBβ where FIRST(β) contains ε , then everything in FOLLOW(A) is in FOLLOW(B).
- List the advantages and disadvantages of operator precedence parsing.
Advantages
This typeof parsing is simple to implement.
Disadvantages
- The operator like minus has two different precedence(unary and binary).Hence it is hard to handle tokens like minus sign.
- This kind of parsing is applicable to only small class of grammars.
- What is dangling else problem?
Ambiguity can be eliminated by means of dangling-else grammar which is show below:
stmt → if expr then stmt
| if expr then stmt else stmt
| other
- Write short notes on YACC.
YACC is an automatic tool for generating the parser program.
YACC stands for Yet Another Compiler Compiler which is basically the utility available from UNIX.
Basically YACC is LALR parser generator.
It can report conflict or ambiguities in the form of error messages.
- What is meant by handle pruning?
A rightmost derivation in reverse can be obtained by handle pruning.
If w is a sentence of the grammar at hand, then w = γn, where γn is the nth right-sentential form of some as yet unknown rightmost derivation
S = γ0 => γ1…=> γn-1 => γn = w
- Define LR(0) items.
An LR(0) item of a grammar G is a production of G with a dot at some position of the right side. Thus, production A → XYZ yields the four items
A→.XYZ
A→X.YZ
A→XY.Z
A→XYZ.
- What is meant by viable prefixes?
The set of prefixes of right sentential forms that can appear on the stack of a shift-reduce parser are called viable prefixes. An equivalent definition of a viable prefix is that it is a prefix of a right sentential form that does not continue past the right end of the rightmost handle of that sentential form.
- Define handle.
A handle of a string is a substring that matches the right side of a production, and whose reduction to the nonterminal on the left side of the production represents one step along the reverse of a rightmost derivation.
A handle of a right — sentential form γ is a production A→β and a position of γ where the string β may be found and replaced by A to produce the previous right-sentential form in a rightmost derivation of γ. That is , if S =>αAw =>αβw,then A→β in the position following α is a handle of αβw.
- What are kernel & non-kernel items?
Kernel items, whish include the initial item, S’→ .S, and all items whose dots are not at the left end.
Non-kernel items, which have their dots at the left end.
- What is phrase level error recovery?
Phrase level error recovery is implemented by filling in the blank entries in the predictive parsing table with pointers to error routines. These routines may change, insert, or delete symbols on the input and issue appropriate error messages. They may also pop from the stack.