0% found this document useful (0 votes)
47 views7 pages

Unit Ii Syntax Analysis

compiler design
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views7 pages

Unit Ii Syntax Analysis

compiler design
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

UNIT II SYNTAX ANALYSIS

Role of Parser — Grammars — Error Handling — Context-free grammars —


Writing a grammar — Top Down Parsing — General Strategies Recursive
Descent Parser Predictive Parser-LL(1) Parser-Shift Reduce Parser-LR Parser-
LR (0)Item Construction of SLR Parsing Table -Introduction to LALR Parser —
Error Handling and Recovery in Syntax Analyzer-YACC.

PART-A

1. Differentiate Top Down Parser And Bottom Up Parser? Give


Example for each. APRIL/MAY 2010
Top down Parser are the parsers which constructs the parse
tree from the root to the leaves in pre- order for the given input string.
Predictive Parser, Recursive Descendent arser.
Bottom Up Parser are the parsers which constructs the parse tree
from the leaves to the root for the given input string. LR Parser, SLR
Parser.
2. Compare syntax tree and parse tree. NOV/DEC 2017
Syntax tree is a variant of a parse tree in which each leaf represents an
operand and each interior node represents an operator.
A parse tree may be viewed as a graphical representation for a
derivation that filters out the choice regarding replacement order. Each
interior node of a parse tree is labeled by some nonterminal A and that the
children of the node are labeled from left to right by symbols in the right
side of the production by which this A was replaced in the derivation.
The leaves of the parse tree are terminal symbols.
3. Define Handles.MAY/JUNE 2007
A handle of a string is a substring that matches the right side of a
production. This reduction helps in constructing the parse tree or right
most derivation.
4. Define ambiguous grammar with an example, and specify it
demerits. MAY/JUNE 2016 MAY/JUNE 2012, APRIL/MAY 2012
If a grammar produces more than one parse tree for the given input
string then it is called ambiguous grammar. Its demerit is It is difficult
to select or determine which parse tree is suitable for an input string.
Ex: E E+E / E*E / id
5. Mention the properties of parse tree.NOV/DEC 2012
 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, xβ,
xγ .xn are the labels of the children.
6. What do you mean by a syntax tree?NOV/DEC 2012
Syntax tree is a variant of a parse tree in which each leaf represents
an operand and each interior node represents an operator.

7. Define Handle pruning.NOV/DEC2011, APRIL/MAY 2011,


NOV/DEC 2016 ,APRIL/MAY 2018
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.
8. What is left factoring? Give an example. NOV/DEC 2007
Left factoring is a grammar transformation that is useful for
producing a grammar suitable for predictive parsing.

9. Derive the string and construct a syntax tree for the input
string ceaedae using the
grammar S->SaA|A,A->AbB|B,B->cSd|e MAY/JUNE 2009
S->SaA
S->AaA
S->cSdaA
S->cSaAdaA
S->cAaAdaA
S->cBaAdaA
S->ceaBdaA
S->ceaedaB

C->ceaedae

10. List the factors to be considered for top-down parsing.


MAY/JUNE 2009
We begin with the start symbol and at each step, expand one of
the remaining non-terminals by replacing it with the right side of one
of its productions. We repeat until only terminals remain.The top-
down parse produces a leftmost derivation of the sentence.
11. Draw syntax tree for the expression a=b*– c+b*– c.
NOV/DEC 2017
12. Construct a parse tree of (a+b)*c for the grammer E-
>E+E/E*E/(E)/id. (or) grammar –(id+id APRIL/MAY 2008,
NOV/DEC 2016
13. Eliminate Left Recursion for the grammar A->Ac|Aad|bd
APRIL/MAY 2017
A->bd A'
A'->c A'|ad A'| є
14. What are the various conflicts that occur during shift
reduce parsing? APRIL/MAY 2017
 Reduce/Reduce conflict
 Shift/ Reduce conflict
15. Eliminate Left Recursion for the given grammar. MAY/JUNE
2007
E -> E + T | T T ->T * F | F F-> ( E )| id E -> TE'
16. Write the algorithm for FIRST and FOLLOW in parser.
MAY/JUNE 2016
FIRST(α) is the set of terminals that begin strings derived from α.
Rules
To compute FIRST(X), where X is a grammar symbol
 If X is a terinal, then FIRST(X)={X}
 If X-> є is a production, then add є to FIRST(X)

 If X is a non-terminal and X->Y 1 Yβ..Yk is a production. Then add


FIRST(Y1) to FIRST (X). If Y1 derives є. Then add FIRST(Yβ) to
FIRST(X)
FOLLOw→ (A) is the set of terminals α that appear immediately to the
right of A. For rightmost sentential form of A, $ will be in FOLLO→ (A).

 If $ is the input end-marker, and S is the start symbol, $ ∈


Rules

 If there is a production, A → αBβ, then (FIRST (β) – ε) ⊆ FOLLOW


FOLLOW(S).

 A → αBβ, where ε ∈ FIRST (β), then FOLLOW (A) ⊆ FOLLOW (B).


(B). If there is a production, A → αB, or a production

17.What is dangling reference?MAY/JUNE 2012,APRIL/MAY 2012


A dangling reference occurs when there is a reference to storage that has been
deallocated. It is a logical error to use dangling references, since the value of
deallocated storage is undefined according to the semantics of most languages.
18.Write the rule to eliminate left recursion in a grammar. NOV/DEC 2012
A - > Aα| : A -> A’ ; A’ -> αA’|£
19. Mention the role of semantic analysis.NOV/DEC 2012
It is used to check the type information of the syntactically verified statements.
20. What is meant by Predictive parsing?
Nov/Dec 2007
A special form of Recursive Descent parsing, in which the look-ahead
symbol unambiguously determines the procedure selected for each
nonterminal, where no backtracking is required.
21. Summarize the merits and demerits of LALR parser.
APRIL/MAY 2018
o This is the extension of δ (O) items, by introducing the one
symbol of lookahead on the input.
o It supports large class of grammars.
o The number of states is LALR parser is lesser than that of
LR( 1) parser. Hence, LAlR is preferable as it can be used
with reduced memory.
o Most syntactic constructs of programming language can be
stated conveniently.
22. Draw the activation tree for the following code.
APRIL/MAY 2018
PART-B

1. Explain Top- Down parsing and Bottom up Parsing. MAY/JUNE 2007


2. Construct an SLR parsing table for the above grammar.
E -> E + T
E -> T
T -> T * F
T -> F
F -> (E)
F-> id
MAY/JUNE 2009, APR/MAY 2011, APRIL/MAY 2008 ,MAY/JUNE 2014
NOV/DEC 2012,MAY/JUNE 2015, NOV/DEC 2016
3. Construct the predictive parser or non recursive predictive parsing
table for the following grammar: S -> (δ) | a δ -> δ, S | S
Construct the behavior of the parser on the sentence (a, a) and (a,(a,
(a,a))) using the grammar specified above. APRIL/MAY 2012 , MAY/JUNE
2007, APRIL/MAY 2005,NOV/DEC 2012, MAY/JUNE 2012 MAY/JUNE
201γ , APRIL/MAY 2017
4. Construct Parsing table for the grammar and find moves made by predictive
parser on input id + id * id and find FIRST and FOLLOw.NOV/DEC 2016,
NOV/DEC 2017
5. Give an algorithm for finding the FIRST and FOLLO→ positions for a
given non-terminal.MAY/JUNE 2009 APRIL/MAY 2008
6. Explain Context free grammars with examples MAY/J NE 2016
7. Consider the grammar,

E -> E + T E -> T

T -> T * F T -> F
F -> (E)
F-> id

8. Construct a LALR parsing table for the grammar given above. Verify whether the
input string id + id * id is accepted by the grammar or not MAY/JUNE
2009 APRIL/MAY 2008
9. Check whether the following grammar is a δδ(1) grammar.MAY/JUNE 2016
APRIL/MAY2005
X → Yz | a
Y → bZ | M
10.Consider the grammar E → E – E
E→ExE
E → id
Show the sequence of moves made by the shift-reduce parser on the input id1 + idβ *
idγand determine whether the given string is accepted by the parser or not.
MAY/JUNE2016
11.What is a shift-reduce parser? Explain in detail the conflicts that may occur during
shift-reduce parsing. MAY/JUNE 2012, APRIL/MAY 2012
12.Consider the grammar given below.

E -> E+E
E -> E*E
E -> id
Construct an LR parsing table for the above grammar. Give the moves of LR parser
on id+id+id ) MAY/JUNE 2007
13.Explain the non-recursive predictive parsing with its algorithm. MAY/JUNE
2016,APRIL/MAY 2005, NOV/DEC 2007
14.Explain the LR parsing algorithm in detail.NOV/DEC 2007, APRIL/MAY 2005
15.What is an ambiguous grammar? Is the following grammar ambiguous? Prove E -
> E + E | E * E | (E) | id. MAY/JUNE 2014
16.Construct parse tree for the input string w = cad using top-down parser.NOV/DEC
2016

You might also like