Sie sind auf Seite 1von 10

PSNA College of Engineering and Technology

Department of Computer Science and Engineering


Question and Answer

Course Code/ Title


Year/Semester

Course Outcomes:

CO1
CO2
CO3
CO4
CO5
CO6

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
S No Question and Answers
1 Define parser.
Hierarchical analysis is one in which the tokens are grouped hierarchically into
nested collections with collective meaning. Also termed as Parsing.
2 Mention the basic issues in parsing.
There are two important issues in parsing.
o Specification of syntax
o Representation of input after parsing.
3 Wh
at
is
the
fun
ctio
n of
a
hier
arc
hic
al
ana
lysi
s?

H
ie
ra
rc
hi
ca
l
an
al
ys
is
is
o
ne
in
w
hi
ch
th
e
to
ke
ns
ar
e
gr
o
u
pe
d
hi
er
ar
ch
ic
al
ly
in
to
ne
st
ed
co
ll
ec
ti
o
ns
w
it
h
co
ll
ec
ti
ve
m
ea
ni
n
g.
A
ls
o
te
r
m
ed
as
P
ar
si
n
g.
Hierarchical analysis is one in which the tokens are grouped hierarchically into
nested collections with collective meaning. Also termed as Parsing.
4 What does a semantic analysis do?
Semantic analysis is one in which certain checks are performed to ensure that
components of a program fit together meaningfully. Mainly performs type checking.
5 Define a context free grammar.
The context free grammar consists of terminals, non terminals, a start symbol, and productions.
G = (V, T, P, S)
V-> variables or non terminals (uppercase letters)
T-> terminals (lowercase letters, operator symbols, digits)
S-> start symbol
P-> productions.
Ex:
E->E+E / id
6 Briefly explain the concept of derivation.
Derivation from S means generation of string w from S. For constructing derivation
two things are important.
Choice of non terminal from several others.
Choice of rule from production rules for corresponding non terminal. Instead of choosing
the arbitrary non terminal one can choose
either leftmost derivation – leftmost non terminal in a sentinel form
or rightmost derivation – rightmost non terminal in a sentinel form
7 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).
8 List out the actions involved in shift-reduce parsing.
Shift  The next input symbol is shifted onto the top of the stack.
Reduce It must locate the left end of the handle within the stack and decide with
non terminals to replace the handle.
Accept The parser announces successful completion of parsing.
Error The parser discovers that a syntax error has occurred and calls an error
recovery routine.
9 Define left factoring.
Left factoring is a grammar transformation that is useful for producing a grammar
suitable for predictive parsing. Two alternative productions to use to expand a non terminal
A.
A-> αβ1 / αβ2
After applying left factoring
A-> α A’
A-> β1 / β2
For example consider the following grammar: –
S  iEtS | iEtSeS | a
E  b
The left factored grammar becomes
S iEtSS’ | a
S ‘eS | ε
E b
10 Write down the rules for left recursion.
To eliminate left recursion we need to modify the grammar having a production rule with
left recursion.
A Aα / β
Then we eliminate left recursion by re-writing the production rule as:
A βA ‘
A’ αA ‘ / ε
11 . Differentiate Top down and bottom up approach with an example.
Sl. Top down parsing (LL(1), recursive Bottom up parsing (LR(k), operator precedence
No descent) and shift reduce parsing)

1 Start at the root of the parse tree from Start at the leaves and grow towards the root.
the start symbol and grow toward
leaves (similar to a derivation)
2 Pick a production and try to match the We can think of the process as reducing the input
input string to the start symbol
3 Bad “pick”  may need to backtrack At each reduction step a particular substring
matching the right-side of a production is
replaced by the symbol on the left-side of the
production
4 Some grammars are backtrack-free Bottom-up parsers handle a large class of
(predictive parsing) grammars
5. Ex: E->E+E /E * E / id E->E+E / E* E / id
Top down: Bottom up:
E=>E+E =>id+id*id
E=>E+E*E =>E+id*id
E=>id+E*E =>E+E*id
E=>id+id*E =>E+E*E
E=>id+id*id =>E+E
=>E
12 Differentiate LL parser and LR parser.
LL LR

Does a leftmost derivation. Does a rightmost derivation in reverse.

Starts with the root non terminal on the Ends with the root non terminal on the stack.
stack.

Ends when the stack is empty. Starts with an empty stack.

Uses the stack for designating what is still Uses the stack for designating what is
to be expected. already seen.

Builds the parse tree top-down. Builds the parse tree bottom-up.

Continuously pops a nonterminal off the Tries to recognize a right hand side on the
stack, and pushes the corresponding right stack, pops it, and pushes the corresponding
hand side. nonterminal.
Expands the non-terminals. Reduces the non-terminals.

Reads the terminals when it pops one off Reads the terminals while it pushes them on
the stack. the stack.

Pre-order traversal of the parse tree. Post-order traversal of the parse tree.
13 Define LR (k).
A grammar that can be parsed by an LR parser examining to up k input symbols on each
move is called LR (k) grammar. We must be able to recognize the occurrence of the right
side with k input symbols. We must be able to recognize the use of a production seeing
only the first k symbols of what its right side derives.
L-> denotes that input sequence is processed from left to right.
R-> denotes that the right most derivation is performed.
k-> denotes that at most k symbols of the sequence are used to make a decision.
14 Construct a parse tree for –(id+id).



()
/|\
id + id
15 37. Define Handle. What do you mean by Handle Pruning?
A Handle is a substring that matches the right hand side of a production and
replacing the RHS by the LHS must be a step in the reverse RMD that ultimately leads to
the start smbol.
A RMD in reverse can be obtainted by “Handle pruning” i.e we start with a string
of terminals W that we wise to parse.
PART B
1. 1(i) Construct parsing table for the grammar and find the moves made by predictive parser on the input
id+id*id and find FIRST and FOLLOW.
E→E+T
E→T
T→T*F
T→F
F → (E)/id
(ii) Explain ambiguous grammar G: E → E + E | E * E | (E) | - E | id for the sentence id+id*id.
2. 2Construct LALR parsing table for the following grammar
SCC
CcC | d
3. 3Explain LL (1) grammar for the sentence
S → iEtS | iEtSeS | a
E → b.
4. . Write in detail about the Recursive descent parser
5. . What is SRP? Why it is called so? Parse the input string id+id*id using SRP and showing the
stack content at different stages according to the following grammar.
EE+E | E*E | (E) | id
6. Show that the following grammar is LALR but not in SLR.
S->Aa / bAc / dc / bda
A->d
7. (i) Construct LR (0) parsing table for the given grammar.
S->AS / b
A->SA / a
(ii) Write an algorithm for construction of LR(1) or CLR items for grammar G’ .
PART C
1. 1(i) Check whether the following grammar is SLR or not. Explain your answer with reasons.
S->L=R/R
L->*R/id
R->L
(ii) Check whether the string num1+num2*num3 is accepted for the given grammar using Shift
reduce parser.
2. (i)
2 Construct LALR parsing table for the given grammar.
S –> XX
X –> aX
X –> b
(ii) Write an algorithm to eliminate left recursion from a grammar also Explain with an example.
3. . (i)Construct LL (1) or predictive parsing table for the given grammar.
S-> a | | | (T)
T->T, S /S
(ii) What are the conflicts that occur during shift reduce parsing.
4. Construct
. a predictive parsing table for the grammar
S → (L) | a
L → L, S | S.
and show whether the following string (a,(a,(a,a))) will be accepted or not.
5. Show that the following grammar
1. S-> AaAb | BbBa
2. A-> ε
3. B-> ε
Is LL(1) but not SLR(1)

UNIT II: Title


Unit II Syllabus
PART A
S no Question and Answers
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
PART B
1 Explain brief notes for generating code from DAG concepts. Draw the DAG for
a:=b*-c + b*-c.
2 Explain the principal sources of optimization and give example.
3
.
.
PART C
1 Generate DAG representation of the following code and list out the applications of DAG
representation
i=1;
while(i<=10)
do
sum != a[i];
2
.
.

UNIT III: Title


Unit III Syllabus
PART A
S no Question and Answers
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
PART B
1
2
3
.
.
PART C
1
2
.
.

UNIT IV: Title


Unit IV Syllabus
PART A
S no Question and Answers
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
PART B
1
2
3
.
.
PART C
1
2
.
.
UNIT V: Title
Unit V Syllabus
PART A
S no Question and Answers
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
PART B
1
2
3
.
.
PART C
1
2
.
.

Das könnte Ihnen auch gefallen