SYNTAX ANALYSIS AND
SYNTAX DIRECTED —
TRANSLATION
Qu. Discuss syntax analysis in brief.
Or
What is syntax analysis ? What are its primary functions ?
(R.GRV., Dec. 2015)
include syntax analysis as the next phase to
verifies the tokens produced as output @
sequence oftokens from lexical analyser are properly sequenced in the accordance
© with the grammar of the language. The syntax analyzer retums success, if the
© sequence is defined in the grammar, or failure in case it is not defined. In cases
‘ where the statement does not match the grammar specified for the language, the
syntax analyser detects the error, emits appropriate error message to the user
F and if possible, recovers from the error. The data structures used 10 represent
_ the syntactic structure of a language must now also be recursive rather than
linear The basic structure is usually some kind of tree called a parse tree.
Syntax analysis or parsing isthe task of determining the syntax or sinc ON,
ofa progam. Itis performed by # module in the compiler called syntax sealyar
Parser. For the parser to perform syntax analysis, the grammar of
language need to be specified. Context-free grammar (CFG) is usually used 10
|
Ans, The compilation process
the lexical analysis. Syntax analys
define the grammar of a language.
2. What do you understand by a context-free grammar?
Or
Define context grammar and explain how it is suitable or Paring.
- (R.GPV, Dec. 2013)
help of example.
(R-GPY., June 2016)
are applied in parser design. Is also useful
in programming languages-
Scanned with CamScanner44 Compiler Design (Vi-Sem., CS-Branch)
‘A grammar G = (V.TPS) is said to be context-free grammar (CFG)
‘every production in P is of the form ~
Asx
where, A € Vandx € (VUT)*
‘A language is said to be context-free iff there is context-free grammarg
such that L = L(G).
Every regular grammar is context-free, so a regular language is also contey,
free. But as we know that there are non-regular languages also. So, we cs
say tha, the family of regular languages are a proper subset of the family ¢
context-free languages.
“The name context-free grammar derived from the fact that the substitute
‘of the variable on the left of a production can be made any time such a variabk
appears in a sentential form. It does not depend on the symbols in the res!
the sentential form. It does not depend on the symbols in the rest of te
sentential form (ie., context). This property is the consequence of allowiy
only a single variable on the left side ofthe production. For example - Ti
= ({S}, {a, b}, P, S) having production
S$ aSa,
S+bSb,
SA,
is context-free. A typical derivation in this grammar is as follows ~
$= aSa = aaSaa = anbSbaa => aabbaa
From this derivation, it is clear that ~
1(G)={uu® /u efa.b}*}
This language is context-free but not regular.
Let us take another example ~The grammar G having productions
Sabb,
A- aaBb,
B- bbAa,
AoA,
is a context-free grammar. Similar to the way given in the above ¢%
‘can prove that ~
1(G)={ab (bbaa)” bba(ba)" /n2 0}
Both examples given above involve grammars that
free, but linear, Regular and linear grammars are cleat
context-free grammar is not necessarily linear.
ample
eo
x only om
are not only
iy content-free
Syntax Analysis and Syntax Directed Translat
6
Q.3. What do you mean by context-free grammar ? Give distinction
id context-free grammar and limitations of context-free
(RGPN., Nov. 2018)
benveen regular
‘grammar.
‘Ans, Refer 10 Q.2.
‘To know the differences between regular expressions and context-free
grammars, contrast the classic expression grammar against the regular
expression as follows —
((identinum) (+H}+))* (identinum)
Both the regular expression and the context-free grammar describe the
same set of expressions. Consider the notion of a regular grammar to make
the difference between regular expressions and context-free grammars clea.
Regular grammars have the same expressive power as regular expressions
that is, they can describe properly the full set of regular languages.
“Aregular grammar is a quadruple, R= (T, NT, S, P), with the components
having the same meanings as for a context-free grammar, In a regular grammar,
however, productions in Pare restricted to two forms either A—> a, or A -» aB
where A, BeNT and aeT.
Jn contrast, a context-free grammar permits productions with right-hand
sides which contain an arbitrary set of symbols from (TUNT). Hence, regular
grammars are a proper subset of context-free grammars. The same relationship
hos forthe languages described by regular grammar, called regular languages,
andthe described by context-free grammars, called context-free languages.
regular languages are a proper subset of the context-free languages.
caytttatons of Context-free Grammar ~ Contex-free languages are
ra ute and easy to understand and easy to parse with compare tothe
cam vetil grammars, but they cannot always express what we want. We
Forages english ina context-free grammar, nor we can write a CFG
COG for gming language We can work around these problems by defining
Progamming itl Ue language that looks like english, oF a CFG for a
i language plus a separate type checker.
Generally we 3
for a Machine, ‘we can say that CFGs are not perfect, but they are a great tool
24. What i
Siperens Uitt is @ context-free grammar ? I
‘ & ymmar ? Ilustrate with an example the
“Fusing Cree nen Of @ context-free grammar. What are the advantages
(R.GPV., May 2018)
ferent Cony
ponent
lOming compere Of CFG — A content-free grammar consists of
bt
Scanned with CamScanner48° Compiler Design (Vi-Sem., CS-Brench)
(@ Assetofterminal symbols, which are the characters ofthe alphaby
that appear in the strings generated by the grammar.
(DA set of nonterminal symbois, which are placeholders fo
patterns of terminal symbols that can be generated by the nontermina|
symbols.
(iA set of productions, which aré rules for replacing (o,
rewriting) nonterminal symbols (on the left side of the production) in
string with other nonterminal or terminal symbols (on the right side of the
production).
(iv)A start symbol, which i a special nonterminal symbol that appear
in the initial string generated by the grammar.
‘Advantages ~ The Advantages of using CFG to specify a language are as
follows ~
( A grammar provides a precise, yet easy to understand, syatactc
specification for the programs of a specific programming language.
(i) From a properly designed grammar, an efficient parser can be
‘ereated automatically.
iA grammar imports a structure to a program which is useful for
detection of errors and for its translation into object code.
Q.5. What is parsing ?
Ams. Parsing is the process or technique used to determine whether +
string of tokens can be generated by a grammar. A parser must be capable of
‘constructing the tree or the translation is incorrect. A parser can be constructed
for any grammar. For any context-free grammar there is a power that takes
most O(n?) time to parse a string of n tokens.
How do you classify the different parsing techniques ?
tt ea (RGPY, Dec. 2019.
Ans. Parsing technique can be classified into two classes —
(‘Top down approach (ii) Botiom up approach.
‘These terms refer to the order in which nodes in the parse tr Sf
‘constructed. In top down parsing, construction of parse trce starts atthe 1
‘and proceed towards the leaves using the lefimost derivation. Whi 10
bottom up. ‘construction starts at the leaves and proceeds 10¥#
the root using 1
case of
ee |
‘Syntax Analysis and Syntax Directed Translation 47
Operator tarne|
Precedence
[re
Arpicabic on LR Grimmer
Fig, 2.1 Classification of Parsing Technique
0.7. What is top-down parsing ? What are the difficulties encounter in
this and how are they overcome ? (R.GPV, Dec. 2007, 2010)
Or
Write steps of Brute force approach. (R.GRV, June 2017)
Ans. Top-down parsing attempts to find the lefi-most derivations for an
input string w, which is equivalent to constructing parse tree for the input
string w that starts from the root and creates the nodes of the parse tree in a
preorder. For example, consider the grammar
S— cAd
A> abla
Letthe input string be w = cad, To construct a Q
arse tree for this sentence top-down, we initially
Grate a tee consisting of a single node labeled S. S @)
‘{zinput pointer points toc, the first symbol of w.
‘hg et use the first production for S to expand Fis- 2.2 S-production to
‘tee and obtain fig. 2.2. canard ee Demers
The a leaf, labeled c, matches the first symbol of w, so we now
; esieepbe 4, the second symbol of w, and consider the next
- We can then ex sing the first alterna i
‘he ce shown n nan” CxDanA A using the ist alternate for A to obtain
The
Parser
cree ‘has fas the match for the second input symbol. We now
rd input symbol, and thi
4 |, and the next leaf, labeled b. Since b does
8t 23. Then eer auilure and go back (backtracks) to A, as shown in
Sd twill yg geccry {18° FeSet the input pointer tothe second input symbol
“scond alternative for A in order to obtain the tree.
Scanned with CamScanner48. Compiler Design (Vi-Sem., CS-Branch)
a Q
O QW 560d
yytstesstine
OO wintorratare ©
Fig, 2.3 Fig. 24
‘The leaf a matches the second symbol of w and the leaf d matches th
third symbol as shown in fig. 2.4. Since we have now produced a parse tce
for w, we halt and announce successful completion of parsing.
Difficulties with Top-down Parsing ~ There are several difficulties with
top-down parsing. The first concerns left-recursion. A grammar G is said 1
be left-recursive ifit asa nonterminal A such that there isa derivation A> Aa
for some a. A lef-recursive grammar can cause a top-down parser 10 20 into
an infinite loop. That is, when we try to expand A, we may eventually find
‘ourselves again trying o expand A without having consumed any input. This
cycling will surely occur on an erroneous input string, and it may also occur
‘on legal inputs, depending on the order in which the alternates for A are tried
“Therefore, to use top-down parsing, we must eliminate all left recursion from
the grammar
‘A second problem concerns backtracking. If we make a sequence of
erroneous expansions and subsequently discover a mismatch, we may have
to undo the semantic effects of making these erroneous expansions. For
‘example, entries made in the symbol table might have to be removed. Since
tundoing semantic actions requires a substantial overhead, itis reasonable to
consider top-down parsers that do no backtracking. The recursive decent
land predictive parsers are two types of top-down parsers that avoid
backtracking.
AA third problem with top-down backtracking parsers is thatthe order 2
which alternates are tried can affect the language accepted. For example,
and then ab as the order of the alternates
‘grammar given above we used ‘a’ :
for A, we could fail to accept cabd. As shown in fig. 24 parse tee and
already matched, the failure of the next input symbol, b, fo match, wo
tion of ¢
imply thatthe alternate cAd for S was wrong, leading reject
‘Therefore, to use top-down parsing, we do left factoring.
'Yet another problem is that when failure is reported, we have Vo
tinue idea where the error actually occurred. Inthe Form given her,
down parser with backtrack simply returns failure no matter what
error is,
lee
Syntax Analysis and Syntax Directed Translation 49
Ans. Refer to Q.7.
sired, we must know, given the current input symbol a and the nonterminal
‘One nuance concerns the empty string. If one alternate for A is €, and
pore 3 uit easy to write and fairly efficient if written in a language
Seetenn lure oe efficiently. To avoid the necessity of a recursive
arene EN oe stack is maintained by the parser, rather
DOT ort Tad
Prob,
1. Consider
Io sing banapage, USTammar GS —>SaSb, show that G is ambiguous
ste c (RGRY, June 2010)
then Tine mat G for string “bababab’ has two derivation
is ambiguous. The derivation or parse are given
Scanned with CamScanner| Fig, 2.5
Prob.2. Remove null production, unit production and useless symbols
from the following CFG —
S> PO) RS| OT
P>alaP
Q> 5160
R> Rd | Re
Terie (RGRX, Dec. 2005
‘Sol, We find the nullable variables and then construct the CFG withos
«productions inthe following manner
To eliminate Q > ¢ from this grammar, the non e-productions t
are obiained as follows - the list ofthe productions containing Q on
jght-hand side is -
right. ide is - ae
Q> bale
Replace each occurrence of @ to obiain the non-c-productions. The I
of these productions is
S>P|T
Qoob
‘Add these productions to the grammar, and eliminate Q —> € from
grammar. This gives us the following grammar -
= S— PQ{RS|QT|P/T
P—alaP
Q> bQ|b
R- Rd Re
Tole
‘Similarty o climinate T > ¢, we get
'S> PQ|RS|QT/Q|PIT
P+ alaP
Q> bQ/b
R—> Rd|Re
Toale
a
‘syntax Analysis and Syntax Directed Translation 31
sven grammar consis the productions
Thee consi
sot :
SoP
py arehe unit productions. T eli
ar at alet the unit production > Q
Sa ch Q > ©. Hone, we 88'S > 19 he
iminate these productions from the given
There exists a non-unit Q
ymmar and
liminate S -> Q-
i Seis added and eliminate the S ->T
Further, add the S >a and eliminate the S — P.
the grammar that we get that does not contain unit productions is
S— PQ|RS|QT|a/ble
Po alaP
Q+ bQ|b
R— Rd| Re
TeTje
Since P > a, Q-» b, T+ € are the productions of the form A —> w,
where w isin T*, non-terminal P, Q, T are capable of deriving to w to T*
There ae productions $ -> PQIQT and P > aP, Q > bQ. T > eT.
‘The right side of which contains, non-terminals P, Q and T, all of them which
are known to derivable to w in T*, and Sis also capable of deriving to w in T*.
There are three productions S —> RS, R -> Rd, R —> Re, the right side of
these productions contain non-terminals R, $ which are known to non-derivable
fowinT*, Hencenon-terminal R,S is not capable of deriving tow in T*. Therefore,
tyeliminating the productions containg R and S. We get useless symbols ~
S+PQ|QT|alble
Paap
Q> dQ Ib
ToeTle
Prob.3. Consider the grammar —
S>WMa
0 Pandan
(Find Pa 4
Cees ee for the following —
®) (a, (a, a)
® 2 (@, ((a, a), (a, a)))
onstruct leftmost derivative
(ii Sor each (a)
Construct rightmost derivative for each (a).
(RGPY, June 2005,
Therefore,
‘Scanned with CamScanner52 Compler Design (Vi-Sem., CS:Branch)
Sol, (i) Parse tees for each expression are given in fig. 2.6.
(B) Parse tree for (a, (a, a).
(A) Parse tree for (a, 8) ~
a
CAO
G40
© ©
©
@
(C) Parse tree for (a, ((a, 9), (a a))) ~
Syntax Analysis and Syntax Directed Translation 52
(i The leftmost derivation for each (a) as ~
Waa)
$> (D> (LS) + (8,8) > @, 8)» (a)
®) (, (2,9)
$>(L) > (L$) > (8,8) > @8)
(a, (L)) > (4, , 8) > @(S, $))
> (@, (4, 8) > ( (a, 9))
© (9), (@, a)
SoM
>)
26S)
> (a8)
>@ ©)
>@@,s)
> @@,S)
> @ (L, 8), 9)
> @ (S, 9), S))
> @ (@, 8), 8)
> (a, (@, a), 8)
> (@, (@, 9), ()
> @ @ a), 8)
> (@, (@, a), (S, 8)
> (a (@, a), (@, S))
> @ (@, 9), (@ ay)
(i) Rightmost derivation ~
(A) (a, a)
S20 36S) >a) >a) > a)
(B) (a, (a, 4)
SO -L9>L)>L9)
> (L, (La) + (L, (S, a) > (L, @, 9)
> (S,(@, 8)) > (a, (@, a)
© (, (, 4), (a, a)
S99 >a)
(LL, 8) >
> (L, dL, (L, sy)
>, a)
FLL, 6, a)
Scanned with CamScanner‘54 Comper Design (Vi-Sem. CS-ranch)
-&Lea»
>G&Se
+1, @ ay
> GS.)
> LGad@ a
>a, ay)
>Geaam
> Sa), a)
> @ a)
Prob. Let G be the grammar —
S—aB| ba
AalaS|b4d
B+>b|bS| aBB
For the string “aaabbabbba” find ~ b.
Lefimost derivation (ii) Rightmost derivation (ii) Parse tree
“ a = (RGPY., Dec. 2006)
Sol, (i) Leftmost derivation for string aaabbabbba is as follows ~
S 3B
> =BB
+ am BBB
> aaabBB
—> aaabbB
> aaabbaBB
> aaabbabB
> aaabbabbS
> aaabbabbbA
—> aaabbabbba
(ii) Rightmost derivation for string aaabbabbba is as follows
SB
a
Syntax Analysis and Syntax Directed Translation 55
(Gi) The parse tree ~
2.11, What kind of transformations done to the grammar rules to suit
apredictve parser ?
Ans. There are two types of transformations done to the grammar in
onde to suit a predictive parser. They are as follows ~
(9 Elimination of Left Recursion — & grarnmar is left recursive if
‘thasa nonterminal A such that there is a derivation A> Aq for some string
Top-down parsing methods cannot handle lefl-recursive gramma
transformation that eliminates left re
Bair of productions A> AalB. co
Productions
cursion is needed. In general left-recursive
uuld be replaced by the non-left-recursive
A> Ba’
A>aAle
strings derivable from A
(i) Left Factorin
Mts sc for rod
dea is that when itis not
fen? aNd a nonterminal A,
hoje °eision until we hay
ithout changing the set of
~ Lef-factoring is grammar transformation
grammar suitable for predictive parsing. The
clear which of two allematve productions to
wemay beable to rewrite the A-preductions ta
seen enough ofthe input to make the right
In gener
ih semen
"© Bor
ifA+ap,
pty string deri
to ap,
@.B2_are two productions, and the input begins
'ved from a. we do not know whether to expand
However, we may defer the decision by expanding
‘Scanned with CamScannera se
56 Compier Design (V-Sem, S812 oe sin
mim" Pod
become, Ra
A’ BilB2
: ing techniques ?
ify the different parsing techniques ? Dep,
wn dict agce et
‘remove left recursion. GRY, May 2
“Ans, Refer to Q.6, Q11 cand prob.S.
plain et recursion and shor hits eliminated. Descrie
scat etn te arcmin REPL, De
Ans, Refer to Q.11 (i).
{@)_Arzange the nonerminal in some oTder Nyy Noy N
{i fori: =0ton—1 do begin
for}: 0 10 1-2 do begin
replace each production ofthe form N; —> Nj
by the productions N; -> 5,7/627!....[5,y,
where Nj—8j/82h--45 are all the curren
Nj producti
end
eliminate the immediate left recursion among the N-
productions
end
Q.14, Define ~ Left recursive. State the rule to remove left recurs:
rom the grammar. Eliminate left recursive from following grammar
S+Aa\b
Ade | Sd\f
Ans, Refer to Q.11.
limiting the immediate lef recursion (productions of the form
Aya
‘0 the production A could be replaced by the non-left-recursive productions
ABA’
Aad’ e
‘without changing the set of strings derivable
aa ngs from A
A> SaA"| fa"
AeA'e
(RGPY, Dec. 2016)
‘Syntax Analysis and Syntax Directed Translat
(Q.1S. Whatispredicveparser? How aparsersconroled by «program?
(RGRY, Dec. 2003, 2007, 2010)
‘Ans. A predictive parser isan efficient way of implementing eeuvee
dgescem parsing by handling the stack of activation records expliily. A
predictive parser is shown in fi. 28
"The predictive parser hasan input stack, parsing able, and an ouput
‘The input contains the string (0 be
parsed, followed by S» the right
Pafimarker. The stack contains a
SZquence of grammar symbols, preceded
fy Sithe bottom-of-stack marker
Initially, the stack contains the start
{pmbol of the grammar preceded by S.
‘Fhe parsing table is a two dimensional
FeNM[A.a], where isanonterminal, Fig. 28 Model ofa Predictive Parser
fand ‘a” isa terminal or the symbol $
‘The parser is controlled by a program that behaves as follows, The
ram determines X, the symbol on top of the stack, and ‘a, the Curent
Fapat symbol. These two symbols determine the action ofthe parser. There
are three possibilities ~
() X=a
completion of parsing.
)) IFX =a S, the parser pops X off the stack and advances the
{input pointer to the next input symbol
(i) 1X isa nonterminal, the program consults entry M[X, a] ofthe
parsing table M. This entry will be either an X-production of the grammar Oran
error entry. IfM[X, a] = (X > UVW}, the parser replaces X on top of the stoke
by WVU. As output, the grammar does the semantic action associated with this
production, which, for the time being, we shall assume is just printing the
production used. If M[X, a] = error, the parser calls an error recovery routines
Consider the grammar y
BOTE
E35 +TEle
oer
T > ¢FTle
F Bid a
A predictive parsing table for this grammar is shown in table 2.1. Blanks
are error routines, F
§, the parser halts and announces successful
Scanned with CamScanneruse of given parsing
“janie 22 Maves Made BY
FIRST and FOLLOW To construct parsing table for predictive pars,
‘two fianctions are used which are called FIRST and FOLLOW. T:
functions allow to fill in the entries of predictive parsing table
FIRST(q) is the function applied on a string of variable and termina! 0
any size is defined as the set of the terminals that begin the strings Je"
from a. If a => € Then ¢ is also in FIRST(a).
To compute FIRST(X) the following rules are applied —
@ If X is terminal, then FIRST(X) is X.
(@) IX + € is a production, then add ¢ to FIRST(X)
(G) IfXisa nonterminal and X—> Y, Y, ... Ys any prov
place ‘a’ in FIRST(X) if ‘ais in FIRST(Y,) and ¢ is in all FIRST!
— __
Syntax Anayais and Syrtax Directed Tran
FOLLOW(A) isthe function applied on nonterminal and defined am
set of terminals that can appear immediately t the ight off ”
To compute FOLLOW(A) for all nooterminals A, apply the following ely
{Place $ in FOLLOW(S), where Sis the start symbol
{i)_Hthere isa production A -» «BB then everything in FIRSTNB)
‘except for € is placed in FOLLOW(B).
(ii) W there is a production A > aB of a production AaB.
‘where FIRST(B) contains € (ic... %.), then everything in FOLLOW(AVs
in FOLLOW(B).
By making use of above discussed grammar which is unambiguous, fet
recursion free, FIRST and FOLLOW function are determined.
FIRST(E) = FIRST(T) = FIRST(F) = {( id)
FIRSTE) }
FIRST(T)
FOLLOW(E) = FOLLOWE) = (), $)
FOLLOW(T) = FOLLOW(T) = (+), $}
FOLLOW) = {+, *)), S$}
Construction of Parsing Table ~ The idea behind the algorithm is the
following. Ifthe FIRST and FOLLOW functions ae evaluated then ths algorithm
is applied. If A> a is a production with a in FIRST(a), then the parser will
expand A by a: when curent the input symbol isa. The only complication occurs
of a “Se then we should expand A by aif he current input symbol is
FOLLOW(A) or ifthe $on the input has been reached and $ isin FOLLOW(A)
ALGORITHM
(_Foreach production A — a ofthe grammar do steps (i) and (i)
(i) For each terminal a in FIRST(a) add A> a to MA, a}
(Gi) If ¢ is in FIRST(a), add A> a to MA. b] for cach terminal Bin
FOLLOWAA):If¢ is in FIRST(q) and S isin FOLLOW(A) add A> a to MIAGSI.
Gv) Make each undefined entry of M be error. ‘
Q.16. What are the components of a table-driven predictive parser ?
‘Write ble-driven predictive parsing algorithm. (RGR, May 2018
Ans, Refer to Q.15. é
Q.17. Show whether the following grammar is LL(I) or not
E> TEATE€
T > FIAFI/e
Po eye
And explain the model of predictive parser. (R-GP.V., Dec.
Ans. Refer to Q.15.
‘Scanned with CamScanner60. Compier Design (Vi-Sem., CS-Branch) s ‘Syntax Analysis and Syntax Directed 7
‘table driven predictive parsing. a
8, Hite te str Eo c ie th primary operations of hp ae hi and ee
to generate a table-riven parser from a given gran ‘ctually four possible actions 2 shift-reduce parser can make ~
(@) Itisceasy ane a ee (@_ Shipt—The next input symbol is shifted tothe wp ofthe
y ey (ii) Reduce ~ The parset knows the right end ofthe handle is at the
tbl the only component tht depends he
the FIRST and set generation algo top of the 5
LA reporting in table-driven parser Fond and decide wit
tack. It must then locate the left end ofthe handle within the
ith what nonterminal to replace the handle,
‘error recovery b
done ent oy bing cnires in the table, which point to the error recov, (ily Accept ~The parser anno a i
Sas (qv) Error ~The parser discovers that a syntax ae
apd calls an erro recovery routine.
of shift-reduce parsing.
{o implement a shift-reduce parser is 10 we 2.
ifthe stack and the right end of ye
‘20, Describe the conflicts that may occur during shift reduce parsing,
(RGRY, Dec. 201:
“Ams. There are context-free grammars fr which shift-educe parsing
peused. Every shif-ceduce parser for such a grammar can reach configu
ws te hich the parser, owing the etre stack contents and the next input symbol
shifting zero of more input symbols onto the saci _gqnnot decide whether to shift or to reduce (a shifUreduce conflict), or cannot
parser operates b
oo "tthe stack. The parser then reduces tothe lefts. ecide which of several reductions to make (a reduce! reduce conflict)
ntl a handle is on 1p 0
it jon, The parser repeats Le ‘until it has detecteg Q.21. Discuss bottom-up parsing in brief.
So: sinleanaimaalalal “Ans. A bottom-up parser uses an explicit stack to perform a parse, similar
pee toanon-recursive top-down parser. The parsing stack will contain both tokens
3 letion of pas 14 nonerminals and also some extra state information. Initially the stack is
Aihispoion he pare alsandanounss sc pee y ee empty and will contain the start symbol at the end of a successful parse, A
Theses of ten es Pe ac eles
ee eee $ InputString $
- EE*E Bs ‘s
Eo $$ StartSymbol
“using the rightmost derivation is given in table 2.3. ao
iu oF a parser are on the right
asec eee seer Parsing. ‘A bottom-up parser has two possible actions (besides “accept”) ~
— @ Shife— A terminal from the front of the input is shifted to
of the stack.
ii) Reduce — Used to reduce a string a at the top of the stack to.
given the BNF choice A> a.
parser is thus sometimes called a shift-reduce parser.
by writing the word shift. Reduce actions are indi
‘Ans, A convenient Way
‘tack and an input buffer, The bottom of
input is marked by S.
fepaigmasiack Input
reduce and giving the BNF choice used inthe
‘grammars are always augmented with a new start
is the start symbol, a new start symbol S'is added to
‘unit production to the previous start symbol —
S'os
Scanned with CamScanner{62 Compiler Design (vi-Sem., CS-Branci) a
Bottom-up have less difficulty than top-down parser, ,
lookahead. Indeed, a bottom-up parser can ‘shift input symbols onto the .-"*
too crnes wat actions 10 perform. However, © bottom-up pune,
a ook deeper nto the stack than just the tp in order to determine
in predictive parsing. Explain shire,
‘taking suitable (R.GPY., May a
nonrecrsve predictive parer makes exp
term and nomterinas that the poser hopes 16 match With the ren,
seria Ameor i detected during predlivePASing Whe the emg
ofthe Pi ack doesnot match the nxt input symbol or when notte,
Hasee ee pot the sack, bis the next input symbol, and He Pein be my
parsing with
‘Ans. The stack of
recovery is based on the idea of skipping symbuls
token in a selected set of synchronizing ae appears, jy
"onthe choice of synchronizing set. The sets shoul,
ears re er recovers quickly from errors that are likely to oexy
in practice. Some points are as follows ~
mp GAs asarting point, we can place all symbols in FOLLOW i
into the synchronizing st fornonterminal B. If we skip tokens until an kn
rr OLLOW (B) is scen and pop B fom the stack, is likely tha parsing
‘continue.
{It is not enough to use FOLLOW (B) as the synchronizing:
for B, For example, if semicolons terminate statements, asin C. then keyvort
that begin statements may not appear inthe FOLLOW set ofthe nontemia
‘generating expressions. B missing semicolon after an assignment may thei
roaltin the keyword beginning the next statement being skipped. Often, tha
_ isa hierarchical structure on constructs in a language; ¢.g.. expressions app
‘within statement, which appear within b blocks and so on. We can add 10%
a wer construct the symbols that begin higher construc
ds that begin statements tothe synchronic
expressions.
in FIRST (B) to the synchronizing s'¢
‘resume parsing according 10 Bi
the input until 8
the empty string, then
Doing so may postpones
ssed. This approach red"
during error recov"?
be matched, 2 sin#|
was inser
De
‘Syntax Analysis and Syntax Directed Translation
asinve parsing. In eect. this approach takes the,
and com at oF all other tokens. synchronizing st of
Refer to Q21 and prob.8.
‘Distinguish berween top-down parsing and
aigee class of gran.rs Gat can Be parsed Ip cocn ea
(R.GRY., May 2018) —
023.
wat isthe
Ans.
Bottom Up Parsing
‘This parsing strategy first looks
atthe lowest level ofthe parse
tree and works up the parse tree
by using the rules ofa formal
Top Down Parsing
This parsing strategy first looks
at the highest level of the parse
tree and works down the parse
tree by using the rules of a
@
formal grammar. ‘grammar.
Gio | The parsing occurs from the | The parsing occurs from the input
string to the starting symbol.
starting symbol tothe input string.
Employed to select what produc-
tion rule to use in order to const-
Employed to select when to use a
production rule to reduce the
‘string to get the starting symbol.
)
rut the string.
(iv) | Top down parsing uses left most | Bottom up parsing uses right
derivation, most derivation.
From applicability point of view, the largest class of grammars for which
top-down parsers can work successfully is known as LL grammars. The
largest class of grammars for which bottom-up parsers can succeed is known,
as LR grammars.
0.24, Explain handle pruning. Explain the same for the grammar
E> E+ E/E*E/(E)/id and input string id] + id2 * id3.
(R.GPV., Dec. 2014)
Ans. A handle of a right-sentential form y is a production A > B and a
position ofy where the string | may be found and replaced by A to produce the
ial form in a rightmost derivation of 7. That is, if
AW = aw, then A> a
‘The string W to the right ofthe handle contains only terminal symbol. n oth
say “the substring isa handle of aw” ifthe postion of B and.
1A we have in mind are clear. Ifa grammar is unambiguous,
form of the grammar has exactly one handle.
by “handle pruning”. That is, we start with a
‘we wish to parse. If w is a sentence of the
Scanned with CamScanneri eae ee
‘64. Compler Design (Vi-Sem., CS-Branch)
‘Syntax Analysis end Syntax Directed Translation Oe
hnere 7, is the nth tight-sentential form
then W = Yq Yq is the nt OF ‘soy
‘hand, fos woe mei ‘probb. Remove eevee the following CFG. F
S-nangng-a'iR-* A AAD
reconstruct this derivation in reverse order, We locate the ha : RGRY, Dec. 2004
f replace Bi by the left side of some production A, -> fin cathe ‘sot Eliminating the immediate left recursion (productions of the form
Terie vightseniential form. We then repeat this proces Thy a -y Ad) 1 the production S and then A could be replaced by the now-tefl-
locate the handle. in, and reduce this handle tobtain therighigen* —gecarsve Proguctions— , 4,
form 1,» If by continuing this process we produce a right-seniemna): x ase
onsistng only ofthe slat symbol S then we halt and announce mx panging the set of strings derivable from A.
Completion of parsing. The reverse of the sequence of Productions ug. witOw S > AbSicS!
reductions is 2 rightmost derivation for the input string. S' + SaSj<
The sequence of steps for thie input string idl + id2 * 3 acco, Aaa
the given grammar using handle pruning is given in table 2.4 A! >AbATe
: ‘probs? Do left factoring the following ~
aes] S > aSaAlb
peeeten Used | A > aAbieAja A
i E>id (GRY, Dec. 2004,
ia2 Eid ‘Sol io she given grammar, apply left factoring procedure — :
ids Eid A aB;jab,
Aaa!
AY > BiB
‘Then, new productions are — x
Sas Z
S'>SA 3
AaAA 1
Ao be a
k. Design shift reduce parser forthe following grammar —
SIS | 080) 2 (RGPY., June 2004)
ce of steps through the actions a shiftreduce parser
the input string 10201 according to grammar, using the
Scanned with CamScannercre »_
‘68 Compiler Design (VI-Sem. eee ‘Syntax Analysis and Syntax Di 4
‘Prot.2. Consider the grammar sot Aawesecttatthe en grimariseReusne oe 2
‘S$ > aBDh rch recursion from the grammar. Then, the we
Bee al A+ cCAICAY
cobde Al >cBale
D>EF B > bBiid
mae c > BEBCIBC
table, RGPY, June ; C > aBcje
Co ee of FIRST and FOLLOW ~ Computation of FIRST and FOLLOW —
Sol. Computation Dh) = (2) FIRST(A) = FIRST(cCA’) U FIRST(CA’,
FIRST(S) = FIRST( = {e} U FIRST(BDBC) U FIRST(BC)
ARST(B) Sg FBSTCS) © 1) y= {b) v fe) = fe) = {6} U {by id} U {b, id)= (6, b, id}
= FIRST(bC) U emule 10)
AaAb|BOBa
Ave
ne Or Jane x
‘Construct top-down parser for the grammar —
S—> AaAB|BbBa
Ave
Boe RGPY, Jy
Sok To construct » parsing able, the FIRST and FOLLOW.
computed, as shown below ~
FIRST(S) = FIRST(AaAb) U FIRST(BbBa)
FIRST(S) = {a} U (0) = (2,6) Table 2.9 Parsing ty,
FIRST(A) = {e} |
FIRST(B) = {¢}
FOLLOWG) = {5} [ $ [S> Anat |S pay
(Using S > AaAb, we get Al Ase | As;
iS rottowadsimestany) [R{ Rtn
{a}, and
FOLLOW(A) = FIRST(b) = {b}, Therefore,
FOLLOW(A) = {a, b}
Gi) Using S > BbBa, we get
FOLLOW(B) = FIRST(bBa) ~ {b), and
FOLLOW(B) = FIRST(a) = (a), Therefore,
& FOLLOW(B) = {a,b}
‘The grammar is LL(]}), because the predictive parsing table cons»
‘multiple defined entries.
Prob.1$. Check whether the given grammar is LL(1) or not
| SESS
(RGPY, Dec.
‘Computation of FOLLOW
FOLLOW(S) = te, 5)
FOLLOW(S) = {e, bj
FOLLOWE) = {t'¢, b)
LL(1) parsing table for the given
shown in ble 2 9,
nutiple defined entries
Prob.16. Consider the following grammar —
S>1ABle
A> IAC OC
Boos
cor
‘and test whether the grammar ix LL(1) or not
Sol Computation of FIRST and FOLLOW
FIRST(S) = FIRST(AB) U FIRST(e)
(1) U(e))
le} |
FIRST(A) = FIRST(IAC) u FIRST(OC)
= (vo) 7
= (1,0)
FIRST(B) = FIRST(os)
= {0}
FIRST(C) = FIRST(1)
en
FOLLOW(S)= {5}
_ FOLLOW(A)= FIRSTB) L FIRSTIC) 4
= (MoM)
= {0,1}
})= FOLLOW {S} 7
= {8}
)= FOLLOW(A)
= (0,1)
(RGR, Jane 2012,
Scanned with CamScanner>
c$-Branch)
(sem, ‘Syntax Analysis and Syntax Directed Translation 13
_
72 Compler Desi” 5
% ven grammar is shown i re
Lay parsing table forte given Ba sto prevedeoe pares haven bees but a al langage SNOBOL
‘Table 2.11 Parsing Table ete ually “all erator is an example of a language for which operator |
bei orks wel
1 ] cedence W° “ . a three
—| presence Werrpreccdence parsing. we define three disjoint presedence
©] S2 IAB Soe | In operator iy =>, between certain pairs of terminals. These precedence
ye bene: jon of handles and have the following meanings
5 Meaning yi
c | co! J ayes precede
' . “has the same precedence as” b
Sit 2.11 contains no multiple-defined entries. So itis
Parsing table uy a “takes precedence over” b
jermining what precedence
oar following two common ways of det
The
slam bold bol between pair of eminals
(025, What do you understand by operator precedence parsing {This method i ntutive and is based on the wadtional notions
(RGPY, June 205 om, ¢asosinty and precedence of operators, For example, if io tae
Or . Figher precedence than +, We ‘make + + This approach will be
Dette precedence poring. (RCP, Dec q| _eapresstc tends of rammertoilocoahesio nina aPaaay
Or ence parser for it.
construct an unambiguous grammar for the
Explain operator precedence parsing method with example, Gi) In this method, we
(RGRV., Dec. 205, janguage, a grammar which reflects the correct as
“Ams. Operator precedence parser works on a grammar called opray in its parse tee.
‘These grammars have the property that ~ 2.26. Write a short note on operator precedence parsing and function.
(R.GPV., Nox. 2018)
wsociativity and precedence
{No production right side ise.
(i) No production right side has two adjacent non-terminals Ans. Operator Precedence Parsing — Refer to 0.25
i eer mere ewes fy expressions Operator Precedence Function — Compilers using operator precedence
E-> EAE|(E) Elid parsers nced not {0 store the table of precedence relations. Mostly the table
As+l-lit {ante encoded by two precedence functions fandg that map terminal symbols
is not an operator grammar, because the right side EAE has two consecut oe | ending ad symbols a and b, so that —
‘nonterminals. However, if we substitute for A each of its alternatives * Bates bib) whenever x ='b,
bean the folowing operator grammar — fia) > g(b) whenever a > b.
BB+ BIE ~BJE* E|E/ EJE 7 E\(E)-Elid 4 precedence relation between a and b can be determined by a
‘The operator precedence technique was first described as a manipula between fla) and g(b). However, it is to be noted that
any reference to an underlying grammar. In fac, o9¢: in the precedence matrix are obscured, since one of (i), (it) or (ii)
‘what f(a) and g(b) are. Generally, the loss of error detection
serious enough to prevent the using of precedence
ible; errors can stil be caught when a reduction is called
be found.
ce relations has not functions to encode it, but in
Scanned with CamScanner“14. Comper Dsign (Vi-Sem., CS-Branch)
0.27. How does am operator Precedence parser _yntax Anais ond :
operator precedence table 10 Ruide the parses” exustreie af operator precedence parsing?
recente oparaer precedence parser. (Ran m\ OOH, ie rcannge operat precedence Para"
im tare ao eer ono yl
oe ee 3) Te erat simpler 10 deb
ae # as Bane ohe limitations of operator precedence pars 7
EFE+E|E-E/E*E|BBIETE!©)|-E\ia gu Wee er (RGPN, Dec. 2002, June 2018)
“The id canbe either a variable ora number. The o smitation of operaor precedence parsing ae a8 fl0W8
teed rte ts shove gamer a shown in table 212” de Te tr pede pain i hao Randi i
“Table 2.12 Operator Precedence Relation Table seni een as iil prs (=P ing on whether
‘ Co nan tres mt
ote and the operator precedence parc sel i enous
tangs POE Pes the parser aceepis excl the desir nae,
aly asl class of grammars can be parsed using eror
eae er ks
ae (qh. Describe operator precedence parsing alorith
(R.GPV., Dec, 2005, June 2007)
a eae i E “ans. Operator precedence parsing algorithm is as follows ~ aa
Japut-An input string w and a table of precedence relations.
~ If w is well formed, skeletal parse tree, with placeholder
interior nodes; otherwise, an error indication.
“ 3 ie Te ts nit tt i
soe tod ip point to $ tem
rare °
begin <
ee Te be he topmost terminal symbol on the stack
ted kt bb these pintt Toby
‘Scanned with CamScanner|
|
|
|
78 Compiler Design (VI-Sem., CS-Branch)
0.31. Write the algorithm for calculation of operator py,
relations. ean pe,
ns Agri ft compution operator restene lions. 2H
Input — An operator grammar G by,
‘Output — The relations <:, = and > for G
Method —
(Compute LEADING(A) and TRAILING(A) fo 2 none,
(i), Execute the program of fig. 2.10 examining each pag
right side of each production. ct
foe ch pricion A> 8X.
toric ont do
Bein
if X, and X,, ave both terminals then st X, = x,
if m2 and X, and Xq ae terminals
And Xj, is nooteminal then
SX, 5 Xiah
EX, isa terminal and Xp is nonterminal then
Tor alls in LEADING (py) do set X, foray,
TRAILING(S), where S is the start symbol of G.
0.32. What is operator precedence grammar ? (R.GPY., June 6
Ans, An operator precedence grammar is defined as an € fiee ope
grammar in which the precedence relation <, = and + > constructed
disjoint. Thatis fora pair of terminal a and b never more than one of the rei,
a+>b,a<-b anda b if for some non-terminal A there is a right sie of
apa and AS ab, where, 8 is either € ora single nonterminal ai
0" derives‘! so
teh = appearing immediately to the left of ‘b’ derives
SA on Ste Det Tangy
‘pe above three can be easily appicd by using io nt
[BADING(A) an TRATLINGIA), whch can
LEADING(A) = (al &
TRAILING(A)~ 8A 1, hee Bie Corse
Leading(A) is the set which consist ofall terminals ty :
Paris derived om ‘AC Tring (A) thereto
saath be lst terminal na sting derived fem
funetio
ue 4 85 below
mb Whereis 6 or single nonemina
ns namely
at canbe firs terminal
ich consist ofall terminals
‘A’ For example consider a
eee: Table 2.13 First and Last Terminate
Es E+ TT [Reema | Fea temat | toreey
TOT? AF 7 a
F> (@jid + ~
(i) To compute the relation we look for right side in production
where two terminals are adjacent or with a single nonterminal in betwotn, fee
example (=).
(i) To compute < we see the RH.S. for terminals ‘a’ which are
followed by nonterminals ‘A’. For every such par ‘ais related by < to any
{terminal which comes in the leading list of A’. For example, + andT.* and
and, (and E.
(i) The > is computed in the same manner for every pair of non-
{erminal followed by terminal “b’ by making use of trailing ls.
9.33. Describe the error reporting and recovery schemes in operator
‘precedence parsing. (RGRY, Dec, 2012)
‘Ans. In operator precedence parsing process, there are two points at
which an operator precedence parser can detect errors. Fist, when no
precedence relation exists between the terminal ontop of the stack and current.
Input symbol. In these situations, the source of error ean be determined by
virtue ofthe position in the operator precedence table, Fo instance, the entry
Ofid} fd] is referenced when the input has two consecutive is without using
‘m operator between them. In such cases, we can issue a diagnostic message
“Missing Operand’. Insimitar manner, some other messages such as ‘Missing
Operator’, ‘Missing Left Parenthesis’ and ‘Missing Right Parenthesis’ can
also be flashed on the positon in the relation table. In all of these situations
where we can flash the error messages, we can also proceed with syntax
‘alysis ofthe next line without stopping at the error by filling inthe mising
Glement on the stack. For example, in case of “Missing Right Parenthesis
tessage, We can insert a right parenthesis in the stack and allow the syntax
‘analysis to proceed,
a
Scanned with CamScanner78. Compiter Desigh (Vi-Sem., CS-Branch) Fcc wie
jon wit
Second, when a handle has been found, but there #59 Fh a messa
this handle as a right side. In these situations. We ‘could fash eerron
depending on the resemblance toa particular production. FOS hac> Here, the
a handle ig “Be the production that it resembles i8 “A. cago A. In thi
rrissing clement is another id, which would have BEER Tua mess, -
‘ease, a diagnostic message is issued showing a "Mi se
Generally, forthe grammar, we can potentially check. eerie
Grclther ead of other operator, ir they do 80 OFS OT can inert th
donate ema, Mice OE or ee tat & none
on et cat Beer te parentsct of ce We flash a diagnostic
ae oe Feaing ha tne expression missing Detwert Ne a
tees
Prob.17. Find LEADING and TRAILING of the following 8rammar~
‘Table 2.15
erm TRAILING
3 bce
a a >’
B be be
1 operator precedence parser and then parse the
s>(@\a_ string “(a,(a, a)”
LOL SIS
(R.GP¥, Dec. 2008)
sot The s >a
LoLSsis
| LEADING (A) and TRAILING(A) for each, nonterminal
2 rated fare shown in table 2.16.
Me calege = ‘Table 2.16 ‘Table 2.17 Precedence Relations
BoBC (GRY, June 208) tee]
‘Sol Given grammar is not an operator grammar, 50 we can transform LEADING | TRAILING
the gen grammar into an equivalent grammar. Table 2.14 GAT Sse he
S_> aaAbB |aabB| A |8B | [Nonterminal |LEADINGITRATLEZ s Ga a ) > >|>
‘A->aAb|ab Ss ab Tit ths hella a
apts perisege.|o't. | s (gdumleaaetote|ligs| eae lee
“The LEADING and TRAILING for the above grammar are computed by
LEADING(X) = {a |X 2 198, where 7 is €or a single nonterminal
TRAILING(X) = {a |X 2 108, where 5 is ¢ ora single nonterminal)
Prob. 18. Find LEADING and TRAILING of the following grammar
S > aABIbAle
A>adble
B > bB\c (RGPY, June 2012)
‘Sol. Given grammar is not an operator grammar, so we can transform
the given grammar into an equivalent operator grammar n operator ramet,
no production of right side is € and no production of right side contains two
‘udjacent non-terminals, Elimination of adjacent non-terminals is performed by
putting all the alternatives of the non-terminal.
S aaAbB | aabB| aAbB | aAc|bA |aB |b
‘A aAb|ab
B>bB\c
relations for the given grammar are shown in table 2.17.
‘of shift-reduce steps for the given input string (a, (a, a)) is as
a
@@ as
a, (aa) $
>a)
+a)
(aa) $
a, a) $
ays
says
as
ys
ys
VVARVAAAY OS
Scanned with CamScannerCompt Design (vie, C$-Brench)
$<( 5
$<((M\a
T >T,S|S
Operator Precedence Relations ~ Refer to Prob.19, table 2.17,
Yes, it is an operator precedence grammar.
aaa
0.34. What do you understand by LR(K) grammar ?
(R.GPV, Dec. 2005, 2007)
Ans, A grammar for which we can construct a parsing table in which
‘every entry is uniquely defined is said tobe an LR grammar. Foran LR grammar,
itis sufficient thata left-to-right parser beable to recognize handles when they
appear on top of the stack
'AnLR parser does not have to scan the entire stack to know when the
handle appears ontop. Rather, the state symbol on top ofthe stack contains all
the information itneeds. It isa remarkable fact that fi possible to recognize
‘a handle knowing only what is in the stack, then finite automaton can, by
reading the stack from bottom to top, determine what handle, if any ison top
of the stack. The goto function of an LR parsing table is essentially such a
“finite automaton. The automaton need not, however, read the
| move. The state symbol stored on top of the stack is the state
sea eas oe
-_
j ng finite automaton would bein fit had read the grammar symbols of
Fem bonom 0 wp, Ths he LR pane can determine om te ate
op of the stack everything that it need to know about what i i the stack
fae npother source of information that an LR parser can use to help make is
| gues decisions ste next np symbols The eases k=O or k=
i acta terest, and we shall only consider LR parser with KSI.
are opr that canbe parsed by an LR parser examining up to k input symbols
ay move i caled an LR(K) grammar, The Lis for let-o-ight scanning
ne input, the R for constructing rightmost derivation in reverse and the k
ihe number of input symbols of lookahead that are used in making parsing
a When (k) is omitted, k is assumed to be I.
decisions
Explain the various steps for constructing SLR parsing tables.
0.35. Expl A
escribe he SLR(1) method of constructing the LR parsing table from
given grammar Musirate with an example. (RG, Dec. 2018)
vdns. SLR also known as “simple LR”, isthe easiest method to implement. A
ranma for which an SLR parse canbe consructed is sid © be an SLR
‘An LR(0) item of a grammar G is production of G witha dot at some
ston ofthe ight side. Thus, peduction A> XYZ yes the following items
A XYZ
A>XYZ
A XYZ
AOXYE
‘The production A> € generates A~> ~. An item can be represented by a
ir of integers, the frst giving the numberof the production and second the
| position of the dot. Intuitively, an item indicates how much of a product we
fave seen ata given point in the parsing process.
| ‘The SLR method is used to construct from the grammar a deterministic
finiteautomaton to recognize viable prefixes. Items are grouped into sets which
five rise tothe states of the SLR parsers. Canonical LR(0) collection is used
for constructing SLR parsers, for this we define augmented grammar and
two functions, CLOSURE and GOTO.
(Augmented Grammar ~10G isa grammar with start symbol S,
then G isthe augmented grammar for G, the G with a new start symbol S' and
1+. This indicates the completion of parsing and acceptance of
‘The acceptance occurs when parser is about to reduce by S'—> S.
(Gi) Closure Operation ~\f Lis set of tems for a grammar G then
is constructed from I by two rules ~
(2) Every item in Iis added to CLOSURE()
(b) IFA a.BB isin CLOSURE() and By isa production,
+710 1. Apply this until no more new items can be added to
Scanned with CamScanner
ay
Syntax Aly and Syntax OrectodTranaton 81 |‘Scanned with CamScanner4 Compiler Desir (V-Sem., C5-Srench)
Forcanavetng the SLR parsing le the CANONCA eli,
of LR(O) items. *
The item F —> + (E) gives rise tothe entry ACTIONIO, (|= shin
sem Froid othe etry ACTION( id] = Shift. Or items in,
‘actions. Now consider I ~
BoE
EoE+T
‘The first item yields ACTION[1, $] = accept. the second item
ACTION(1, +] = shift 6. For Ip ~
Bot
rer
LOWE) = {S.+. )}y the first item makes ACTION( 9)
ACTION, #) = ACTIONI2, ] = reduce E —> T. The second item ny,
‘ACTION, *] = shift 7. .
Continuing inthis fashion we obtain the parsing action and got uy,
‘The numbers of productions in reduce actions are the same as the order
which they appear inthe original grammar. That is, E —> E + Tis numb
E+ Tis 2, and s0 on.
(0.36, What ae the merits und demerits of LR-parser ? ConstruSip
parsing table forthe following grammar with necessary codes for actin
EB >E+TT
ToOrAF
Fo Eid
Since, FOLI
RGPY, Dec. 15
Ans. The merits of LR-parser are as follows ~
{@)Itis non recursive.
(i) tis the most general nonbacktracking technique known
(ii) An LR-parser can recognize virtually all programming angus
constructs writen with context-free grammars,
_ iv) Itean detect syntax erors as Soon as it is possible ina lef-o-
‘ight scan,
A eee cnet 77 fet names
ly disadvantage 10 LR wires hard work
~ parsers is that it requires hard work ©
Also, refer © Q.35.
emote
parser will detect an error when it
table. when it consults the parsing acti®
‘and finda blank or eror entry Errors are never detected by cons#
Bn
Syntax Analysis and Syntax Directed Translation 5
‘An LR parser will detect an error as soon as there is no valid
se ot raft pa rnd Cpu
usin ingle reduction before announcing the ex. SLR and LALR
spake even # SUE several reductions before detecting an eror, but they will
enon meroneous input symbol onto the stack.
meee Howe construct an CLR parser ? Explain
ical LR parser isthe most general technique for constructing sn
Ans Canon a rama In SLR mod, state calls for odactonle
Lx pain 20 ates contains em [ac] anda isin FOLLOW(A) In
a situations, yer, when state i appears on top of the stack, the viable
situations Mzack is such that BA cannot be followed by & in a right-
Mus, the reduction by A —> a would be invalid on input
abe cary more information inthe state that willow ws tre
vet esinvaldredusonsby A> a. By spliting at whennersy
autos tung to have cach sate of an LR pare inate enaely whch np
ean fol ahd fr which hee a pose ection w A
re extra information is incorporated into the sta by redefining ems 0
ipsa ermial symbol as a second component, Te genera form ofan tem
ines [A > ar al, where A~> a isa production and ai terminal othe
sesepamarter S. We call such an object an LRU) em. Te I ref 1 th
a vote second component, called the lookahead of thet. The lookahead
dene effet in an tem ofthe form [A ~> ca}, where, isnot, butan te
the form [A —> ce, a calls fora reduction by A -> a only ifthe next input
1 a. Thus, we are compelled to reduce by A —> «only on those input
pols for which [A -> ara] is an LR(I) iter inthe tate ontop ofthe stack
Formally, we say LR(1) item [A —> a-B, a is valid for a viable prefix yif
ae
et
there isa derivation S=2 5Aw = Saf, where
@ y= 6a
___ Gi) Bither ais the first symbol of w, or w is € and ais S
‘The method for constructing the collection of sets of valid LR(1) items is
similarto the LR(O) items. The need to modify the two procedures CLOSURE
and GOTO.
To appreciate the new definition of the closure operation, consider an
form [A —> a-B,f a] in the set of items valid for some viable prefix
{sa rightmost derivation S> 8Aax > daBspax, where y = 5
derives terminal string by. Then for each production of the
1 for for some 1, we have derivation $=> /Bby=>mby. Thus,
Scanned with CamScanner=. ee
‘Syntax Analysis and Syntax Ojrected Translation 87
2 Compier Desig (ViSam. CS-Brone") : a
os fory, We say that be can be any terminal in FIRg en 2
Bn Oa ee of ems constuction. Tifa BRE cons
wes ¢: SancS — h: Cretved
et A) eal i cons Br Caec.s
arphe sets of ER(I) items tat are the Set oF tems valid fy yr eecie
more viable prefixes of G’ Bec a
a ‘Method - The CLOSURE and GOTO and the main roy cord eld
constructing iphame.aro shown in fig. 2.12 “ 1s the ten sets of items with their goto’s.
mente rae ig 2.13 shows
tesco clare
ek
inch (4968 8
ees eo.
SP nal a FIRST
SAS rele tin ao
sa Bones
ei 09 wi tne on a
aaa
a
fence sont
tee
be te tof tems {A> OK, 4] ch at
Roemdene
ha a
(otonre (8S. $1)
repeat
{for cach st of item Fin Cand each grammar symbol X
‘ech that goto( 2 is mt empty and notin C do
add gow, 39 10 C
tanto more st of items can be added to C
ond
Fig. 2.12 Sets of LR() Items Construction for Grammar G’
Consider the following augmented grammar —
Bes | Fig, 2.13 The GOTO Graph
C+cCld “i the Algo/Procedure for construction of the canonical LR
The canonical collection of LR(1) items is as follows — (R.GRV, June 2004)
he Sss yy: Cod eld = Construction of canonical LR parsing table.
ie Beeson. ¢ }~ An augmented grammar Gi.
iE ees: canonical LR parsing table functions ACTION and GOTO
Cod, cld C4 40,$
‘Scanned with CamScannereae
20 -compler Design (vi-Sem., OS-Branch)
Method — (i) Construct © = {py
LR(1) items for GC.
(i) Sia of the parser is constructed from I. The par,
for state i are determined as follows ~ Bt
(@ IEA > aaB, B18 iH, and GOTOU, «) = 4
1} 10 “Shift ;” the
(b) If{A > a, ais inp then et ACTION, a 0 “reac 4
(IES S.,S] si I, then set ACTIONG, $] 10 “acco
Gi) Te gto wansons for sate i are determined as fogy,
IFGOTOG, A) = Ij, then GOTOLi, A] = j.
(iv) Allentres not defined by rules (i) and (ii) are made “ep
(0) The inital state ofthe parser isthe one constructed from
ing item [SSS Table 219 Canonical Parsing Tay.
v1 Is 1 collection
4,
ACTION(,
sa
7 med is called
Dei hed] ACHON Tc
LER parser using this tables called |] —]-s3 |] oa sfc
‘canonical LR parser. If the} 1 on ]
parsing action function has no] 3 | 6 | <7 s
tultple-defined entries, then the] 4 | +3 | #3 8
given grammar is called an LR(1)| 5 Fa
‘grammar. The canonical parsing $ 36 | 87 9
fable forthegrammar(),givenin] § [|e |
(Q38 is shown in table 2.19. 9 2
(0.40, Describe the construction of LALR parsing table.
Ans. The another parser construction method, LALR (Lookshesi-LR)
technique is often used because the tables obtained by it are considen'
smaller than the canonical LR tables.
‘The SLR and LALR parsing tables fora grammar alwa
A ys have the same
eae typically several hundred states. The LALR parsing table
‘ced lm the cnoical LR able by merging he sites which ave si
ars The ew st th fed ea ‘where digit represents te
Z what chet ahem ‘grammar (i), from Q37,
eprsarort {ROD tems were shown in fig. 2.12. Take pair of sim
{ooking states, such a8, and I,. Each ofthese states has only items with is)
component Cin ‘
i sare cords, $isthe only lookahea!
nae by yp, the union of I, and I, consisting!
‘The goto’s on d to |, 0"
‘The action of state 47 is to reduce
-
‘syntax Analysis and Syntax Directed Translation 89.
sqrevsed parser tehiaves eset ke he orginal although
inp Te in creustances where the original woul declare er,
By ede? Ot like ced or cde. The ror will eventually Be caught;
caomble he caueht before any more input symbols are shied. Similarly
Ii er pi wih ce (C > C9 Cr a Thee
fa eg al where (C—> «©
Je om ee of go (I, X) depends only on the ore of, the go's of
since the themselves be merged. Thus, thee is no problem revising the
nego Se oe merge sets of tems. The action functions are modified to
gon tn or acon fal sof tems nthe meres
rete Me ave 20 RI) arma, tt ane whose st of FR!)
parsing ation cons. IF we replace al sates having the
items pod heir union iis posible that he esting usin Wil hve 2
ben fof the merging of states with common cores can never produce
rai yon conic hat was ot preset none of rial ses, cane
aie ed only onthe core not the lookahead. However, itis posible
sin ergr wil produce a reducereduee conflict.
~ Constructing LALR parsing table.
— An augmented grammar G'
~The LALR parsing table functions action and goto for G.
sod (i) Construct C= {Igy Thy wy Jy the collection of sets of
LLR(I) items.
For each core in the set of LR(1) items, find all sets having that
these sets by their union.
vs Jmg} be the resulting sets of LR(1)
‘are Constructed from J, If there is a
‘Table 2.20 LALR Parsing Table
‘ACTION | GOTO
ela|s|s
Be rook. 336 | 47 1 [2
STATE
7
$36 | s47
8
3
2|2
Scanned with CamScanneri” ’ ing table fo
formed is called LALR parsing table for G. If there
caeite: Facts, ten tie ven TmDINAT is aid 1 be ap
oe fsx of me contri in ep (i) icy)
— calectee! : a Cte are Eee
‘The LALR ACTION and GOTO fanesons Condenaed stot ; : on the tp ofthe sack only when E* Enc Re te
‘ce shown in table 220. Thould have action reioce E>E* Eon bath and eps, |
What are the diferent methods of COSA he LR pay, a able 2.21 LR parsing table is obained
eit Som agen ranma’? GPK, June a! “ ‘Table 2.2 Parsing Table for Grammar
“Ans, Refer Q.35, 0.38 and Q40-
gn. Explain parsing wing ambigners ee
be used for LR parser construction ? ip,
Can ambiquons grammer (RGEV., Dec. 2005, Tene ee,
eT wcrin pes of saps ais ew
poco languages. An ambiguous panne
Eason rer pein ha 2 QUA a
Pe eds the following grammar for arithmetic expressions ve
nial”
TE-+E+BE* BEF
is the ambiguous because it does not specify the associativiy
as ar lis pees
Beate ‘Consider another case of ambiguous grammar for conditional statement
T4Te Wiese ‘stmt — if expr then stmt else stmt
Fo EoEte toast [if expr then stmt
‘The sets of LR(O) items are Roe e ost {other productions.
is in : ecause it does not resolve the dangling else ambiguity. For
£m)
brett
aan
Ly: Rete
Bante
EE 4 ts 0) items for grammar are
Bes eer , 2.15. The set of item I, yields a
Eoerk : nf 7 iS.c8 calls for
by Ey
‘Scanned with CamScanneraa
42. Compiler Design (VieSam., 0S-Branch)
‘on the
first input symbol, should
‘ve shift else onto the stack
(ie., shift e) ot reduce if
expr then stmt to stmt (ie
reduce by SiS). It is
concluded that the conflict
in, should be resolved in
favor of shift on input. The
parsing table constructed
from the sets of items for
abstract “dangling else”
‘grammar is shown in table 2.22.
For example, processing of input ‘iiaea’ is as follows ~
‘Table 2.23 Processing of linea
S.No. ‘Stack Input
o 0 iiaea$
Q) 2 inca $
@) oni aeaS
@ i2i2a3 aS
6). oizizs4 eS
© Oi2i2S4e5 3
o (12i2S4e5a3 s
(8) 21240536 s
9) oi2s4 $
(10) Osi s
Q.43. Describe the function of the LALR parser generator “Yacc",
Or
Write short note on “YACC”. (R.GRV., June 2003, Dec. 2003,
es June 2007, Dec. 2007, 2010)
‘Write short note on automatic parser generator.
Pa és (R.GRYV., June 2005, 2006)
Parser generator can be used to facilitate the construction of the fron!
widen epee
ate a; son in carly 1970's. Yac is avaiable asa comant
a Ieree has ben used to implement hundreds of compile
ec peateaneecondrctedusing ye’ Fin fil, naa, conaing
yace 7 ‘translator is prepared. The UNIX system command.
a i
tack and ese asthe Table 2.22 Parsing Table for “q
see Grammar 8
a
‘Syntax Anelysis and Syntax Directed Transietion 93
-y into a C program called y.tab.c using the LALR
file ra
soso ic rab a repcsraton ofa LALK pre wien
‘pete tng ye “Tong with the ly library that contains LR parsing program
o partes
BYE command: yaod af YAO Lo
ee go yiade VY- Speceton LCempler]~ **Y*
in the desired object program Transl
we Sa nthe rasan specified ™=h«—| eeSet |se
at eae
‘ee wigial ‘yace” DOB = sa
or gurce program hes te topu [at | om at
‘a yee” soul’ PORE declarations
% % Fig. 2.16 Creating an VO
translation rules Translator with YACC
%%
supporting C-routines.
‘The Declarations Part ~ There ae wo optional section in the
ace” program. Inthe frst section, ordinary C declarations
on a by % ( and % } are put. Here we place declarations of any
ike ed by the wansation rls o procedures of the second and hid
rt ecarations part also contain declarations of grammar tokens
(ii) The Translation Rates Part ~The translation rules ate pu afr
tpt 996 par inthe part of 203" specification. Each mle consists of a
Froduetion and the associated semantic ation. A ‘yaee’ semantic
nce of C statements.
)
O eat ota
action is a sequer
(iii) The Supporting C-routines Part ~ The thd part of a ‘yace’
jfication consists of supporting C-routines. A lexical analyzer by the name
splex() must be provided. Other procedures such as error recovery routines
ay be added as necessary:
desk calculator program derived from the given grammar is shown.
sken DIGIT
expe “a! ( prinu4d\e", $1)
+ expre “+ term ( $$=$1+$3;)
$ term
Scanned with CamScanner_ .
94 Compiler
“7
os-Branch)
mm gement TSE
i ited
i ages 185-8)
por
> Bid
ao
(RGPY, Dee
= ‘Syntax Analysis and Syntax Directed Translation 95
at collection sets of LR(0) items are computed as follows —
the b>
54s
S$ Ses
so is
so4
)
oo Un S)= (S'S.
SSeS
yeh
(85s
S> Ses
sis
3-4
}-h
goto (I 8) = {Sa} =
goto (I, €)— { SSeS
S— SeS
Ss 7 iS
Soa
y=
8)=( Sis.
$525
goto ly)
Scanned with CamScanner‘Since the ACTION/GOTO table shown in table 2.24 contains muiipe
entries, so the given grammar is not LR(0).
yr
‘Syntax Analysis and Syntax Directed Translation 97
he coletion of LR) tem se and dra the ote
an he fone SS || €
pe confi (0m) the various ates ofthe SLR pare
sp (RGPN, Dec. 2008)
grammar will be —
sak The SHEEN OS
S88
ssa
s+e
nical collection ses of LR(O) items are computed as follows —
me aroout(S + -S))= (59 S
sss
Soa
s-.
losure(| ,
=e (Ss S.))
seo, SV S'S.
S>Ss
S788
sa
aoe
poh
a= (Sa)
'S)= {S— SS.
8S oss
sss
in the I state of the SLR parser.
‘Scanned with CamScanner_
1, CS-Branch)
J fio - ‘Syntax Analysis and Syntax Directed Translation 98
ts of LR(I) items in the canonical collection that have
24, Show that the leans emer a
‘Aa [be de | 0
ang ! genses ERO) To ale ia lf a a Koken
ise ee for the given grammar i given in able 226.Th
‘ "pers : is table
in LALR (Pt not SER (I aenae A pa elie entries, bene, the gen anmar is LALE (1)
st rm eget ona = 005, Dee » # ‘able 2.26 LALR(1) Parsing Table
sos mI ACTION TABLE. GOTO TABLE)
Sa Aa | STATE |g 6 e fad 5 s]A
S— bAc facig | Ss Se x z
sode ee Accept
‘S— bda & L Ss
Aad [I S s
‘The canonical collection of sets of LR(1) items are ~ fe | Rae S,
= >SS faite} Ry
S+.Aa,$ Ts So
S—.bAc, $ 1, Sto si
eats z =
aca: a %
) Tio Ry
golly 8) = (SS, $) =I, “The SLR(1) parsing for the given grammar is given in table 2.27.
oa =(S+Aa,$}=1, ‘Table 2.27 SLR(1) Parsing Table
= {S bAc, $
pe ee eS, ACTION TABLE. GOTO TABLE
eS b € a 5 Ss A
Sp Sy aehoe
Aes
Scanned with CamScanner——
es et ee emer
Sp AaibAci Biba ‘
avd
Bd RGRW, June 200
me 206 204,
o 4
f Boe PERV, by,
%
Prove that grammar is CLR but not LALR
$+ AaibAc\BcbBa
And
Or
Show thet the following grammar is LR (1) but not LALR ;,
so
Avd
Boe ROP, De
Sol. The supmented grammar 1 -
Sas
Sa
Sr bac
ae
Boe RGR,»
am
i
Syntax Anaiyaa and Symtax Drvcted Trnseton 101
2 {5-7 dA, $
polly” $+ b.Ba, $
Ade
Bode
be
jy B)= (8? Ba, $) =
pote OA doa
potty Do oe
Tis Abe 3)
setts 8)
ir y= ($+ DBA. $} = by
Bain A SF
Boda
bs, Spel,
pele ee ace
= {S-> bAc. $}
ely (> bbe, 8} ~
SRELE(I) parsing table i given io table 228
‘Table 2.28 LR(I) Parsing Table
ACTION TABLE
GOTO TABLE
‘Scanned with CamScanner102 Compiler Design (VI-Sem., CS-Branch) YP
1x Anatyale and Syntax DWE
‘syn
Sinee,the ACTION/GOTO table shown in table 2.26 Leuven
‘multiple entries, so the given grammar is LR(1), O° ba. sentence HE
m “ nC
‘There is omy one set of LR() items in the canonical cotgeg | tnt BHnS!+
identical LR(0partitems and that difer ony in theirlookahee™ a | Bon St
So, the LALR() parsing abl forthe grammars gen nie a4 BR Si
e229 r 1%
‘Table 229 LALR(t) Parsing Table } To RS{tlt,
| F> S| +1
ACTION TABLE | FF id, $1 +1"
ite Tio eee
“ Sate E> E,S
Io Ss Ss 1 [aM E+E +T.S\+
T Accepi| PH | lt
fi] ET. S|+
b [Ss TOTRSI+*
i es Thy
i 5 ~| TOF Sit} =h
- = F>(,S/+1*
Toy | RoR RoR ot
6 R ry E>T)it+
T 5 To TR )I+I*
Z u ToE)I+I°
& |S | F>@,)|+I*
Tro R eS
i
- 2 Fid,$|+1°}=1,
a Ry | go (, +)= { E>E*T,$|+
Since the ACTION/GOTO table shown in table 2.29 contains main T> TF S|+1*
ee ade a olsen ToESiel
Prob.26. Construct LALR items for the grammar below ~ Fo id, $|+|*
E>E+T|T date
eee goto, *)= { T+ T*RSi+/*
5 F> 4€),$|+|*
FO @)\id (RGPV, June 10) é er
‘Sol The augmented grammar is =
ists ‘B00 (1, E)= {F + (.), $+) *
E>Eer |r ne
TOPE |F ET, )|+
ea
wd Teanatation 9
Scanned with CamScanner_ .s-Branch)
son, CS PP stax aay an Stax rece Tonaton 100
— pesian
04. cometer por )Itl*} = bo n= (BOE+Ts i+
goto (F=f syle Gh
ete Fo (8)! me ToT tls
OF foaen)I+
pals
10 (re A) bo
goto (hie = I
go (i) = Te
eee) {TOTPE)I+/"} =o
goto (hi = I
goto (Hix id) = Ta
poo tn = > LEE hn
goto (his 1) = he
goto (ly) = fv
are total ten sets of LR(I) items in the canonical collection that have
rss but ier ony in thei lookahead, namely (hs)
ee Che Tad lr Has (sr Hs ei) He Tad Cs a).
Tah se fe for the above grammar is shown in table 2.30.
go ie LALR pain 2b!
‘Table 2.30 LALR Parsing Table
Te: ie ie ® ' oft t ee ACTION TABLE. GOTO TABLE
T>F,)Itl* sta =. (element | $= |B rar
. t +
Beet bagels Sait 85,12 1 [2,9 | 3,10
Fo aid 141 bie 7 Accept
jehe 7 R R
pte (Tots B+" iE 2 2
F>.©,)141* ia bs us
pe)!" i ie | 84,13) $5,12 8,18] 2,9 | 3,10
Pint a i Bl Re
Bo 31.54 i Ssia)__|__|is.19] 3.10
1 $4 11 $5.12 14,20}
s
$15,211 3.
Ri is Rr
R3 R3
Zi Rs [Rs
‘Scanned with CamScannerS osion (v-Sem, o8-Branch)
ee ‘the following grammar —
Prob? Come TT
TorFlF
For lait, cnpetiy Se
parsing table for this
Gore th LALR parsing ble. hem
‘sot () The augmented grammar 1S
BoE
ESE+TIT
To TF|F
ae
cal collection of sets of items are cor
nee ee ry
(Rary,
PL,
ny
N
‘syntax Anaiysis and Syntax Directed Translate” 107
Tor
port Fo R*
EO EST.
ToOTF
ror
Foa
Fo
rhe
pode N-(T> Fr
}aho
goto lo DE
010 (ly) = Ts
goto (ho, Y= Is
‘Table 2.31 SLR Parsing Table
ACTION TABLE GOTO TABLE
pallet at [ase lee Foor
Ss, veh 3° [2
36 Recep
Ss
Ra
Ro.
R7
R3 | Ss | 3
Bs Rs |
|
Rt x
Rg | $8 a
Scanned with CamScanner‘Syntax Analysis and Syntax Drected Translation
09
goto (©)
Fb.
yels
goto, D
yess
“T341k$
Tors
gOF*S
Faas
$4 pss
Scanned with CamScannerr ‘Syntax Analysis and Syntax Directed Translation 111
Ts
iN TABLE jon of context free grammar in which each grammar symbol has an
ACTION TABLE GOTTA gna grids: Syntax directed translation provides convenient
TATE |—q [be E ue sociated specify compilation actions. It has three components ~ a source
parnework 10 SP set of attributes, and a set of semantic actions. A parser
fee program according to the source language grammar and
pases Stara tee in which each node represents a grammar symbol
struts 8 Pte some compile time information associated with the symbol
Sra es riable or expression, the address of is memory locaton,
Ea he OPE Cr instructions generated for a construct, et.
seenes action assigns an appropriate val to an atibute of grammar
agnbel I aa program by performing symbol table building, memory
sransiation Ore gencration etc. A semantic action is enclosed between
atcation | tos can be partitioned into two subsets called the synthesized
tribelgributes. The value of a synthesized atribute at a node is
pra the values of atributes athe children ofthe node inthe parse
computed om fan inherited atrbute is computed from the values of atrbute
atthe sibling es that will be represented by a graph. From the dependency
er ayaion order for semantic rules are derived. Evaluation ofa semantic
crete valves of tributes a each node is called an annotated parse
mls dee ess of computing the attribute values at the node is called
tr lng of decorating the pase tree.
every eammar symbol appearing inthe arse rea value is associated
ana hy together form the ansation of hat symbol. A pars wee thus modified
aot asta dreton translation scheme, Ithas entry such as X-VAL or X.
ea ere X the grammar symbol and X.VAL isthe value that takes when
srs. If we have a production with several instances ofthe
Ls | 8s 1
Le 36 Accept
H
sree)
short note on syntax directed translation. actual evaluation occu! a
een (RGPN, June 2004, same symbol on the right they are distinguished with separate superscripts.
Or eg, B> BC) +E) actually get converted as
Explain the syntax directed definition for constructing syntax te R t
‘an arithmetic expression. Also explain what is annotated parse tree. E.VAL:= BVAL+E?VAL
(RGR, Dei vhichstates that translation E.VAL on L-H.S. is determined by adding together
Or the translation associated with the E’s on the right side of the production.
‘What is syntax directed translation ? Why are they important? ‘This translation scheme allows subroutine or * semantic actions” to be
(RGPY, Dec 28 stahed to the production of a context free grammar. These subroutines generate
Pa intemediate code when called at appropriate times by a parser for that grammar.
Define syntax directed definition. (R.GBY, Dec. 1) Letus consider how the semantic actions defines the value of translation.
Ans. Syntax directed definition are high level specifications for trans Asmall grammar with its production is given below with the semantic actions
many implementation details and fre the user from having'0 BS BO 4B) feva: BUl-vaLe 2°). vat}
sles are to be evaluated, so they slo *
» be shown. A syntax directed definition
Scanned with CamScanner‘Syntax Analysis and Syntax Directed Translation 113
ing suitable syntax directed definition, eons
ner a 2+ 3. A parse tee ea f5,Asiresion a 4+ RGD, Dee 201)
oing 1 42*2: ‘randiation is dmwm inf. 29° 22 ie es ee
: ei ‘ PN Det. 201)
oe Ca yeti
ee as A POY syntax tree for the expression a— 4 +. The pare tree
os gate po olla pono he alco gee
sion depicted through nonterminalsE and. AtrbuteT ps
oe ae t fr esr eat for an identifier and x numbe is defncd by he
were ich are connected with the productions T~>id andT > num.»
gaia te ih the tokens id and num retums attributes ae and
or (ase my re esl ves. In 225, when a expen Ea
aes a pits equivalent othe use ofthe production E —T. The value of
gran een: nde Gained by the atte Fp. Previous rules ave stp nd
aibute TF vets tothe leaves for ‘a’ and "4 respectively. When the semantic
Computed Translation
Expression 1+2*3
-Gopsiderng the bottom leftmost E. This node corespondig
the patuaan >. Te eresponding Semantic ston ser ry
pe ates the value 1 with the translation E.VAL ate
sat , Similarly we can asocat the Value wi he tansy
Sthstig a re
~The value of E.VAL at the roo is calculated using the semantic ny,
- BvaL:=E().VAL+E®.VAL
permenant
ing this rule we substitute the value of E.VAL at the bua
the value of E. VAL at its right sibling F for,
of evaluation is 1+ 2=3 and then 3 +36)
‘with grammar production are put to use Fin
pte! were Pmiknode (—",E1-P.T.p) connected with the production E> E, -T
rule EP fh interpreting fig 2.25, its essential to known that the lower oe,
isimab joveloped from records is rea syniax tee thet et ouput, but he
sey use above isthe parse tree develop only ina fguatv sense
Syntax Tree Construction for a—4+e
by syntax directed translation ? Explain. Give the
| for the expression 23 * § + 4 according to the
| scheme, (R.GP¥, Dec. 2009)
‘Translat ition — Refer to Q.44.
pete n 23*5 + 4, the program is to produce the valve
1 such a translator, we must first write a grammar to describe
the nonterminals E for expression and I for integer: The
Scanned with CamScanneroo (vise, o&-Branh)
productions = py BFE
OEE
Eo!
{> [digit
1 digit
aisare "and dist
=f emantit actions {0 the productions, Wj
‘Now, we add ee associate one integer-valued translation is
‘which denotes the ona value of the camety
aby ode ofthe parse tre labeled E or I Wit yes,
Oe LEXVAL, which we assume is
‘associate the
a pnnt ofthe pic igi LEXVAL) returned by the lexical analye,
token of type digit is found. ‘Semantic action for grammar is shown in f°
“Semantic Action
SEs Em EOLVAL+ FOAL
Eno sD.VAL*E®LVAL}
s NAL
1910 ait
1 at
“Fig. 2.26 Symtax Directed Translation Scheme
Using syntxcirected translation scheme, the input 23 +5*4 woot,
the pase tree and translations shown in fig. 2.27. m
ee
‘ae by syntax directed translation ? Explain. Give
expression 25%5 + 4 according @
A June 213
eee
‘Syntax Analysis and Syntax Directed Translation 115
atts a translation scheme How si diferent fom a stax
ot Mn teh or ofexctan of sont
rected Oe scheme. (R.GRY., Dec. 2015)
St eying Wansttion ding ping» anlation scheme i
sn home pei us ey hws
money btmeo tts swell hore ovation
gi ranslation scheme is a context free grammar in which program
2 Assos ere embedded wii he ih ie of
eats ealle® Mesition at which an action is to be executed is shown by
ie sn braces and writing it within the right side of production,
secheme produces an output foreach sentence X generated by
Asani cxeoting te acon nth ore ey 7 during
the ra ey parse toe for x. When drawing a parse tre for a
a depth peer we rt icate an action by constructing for it an extra child,
‘ont oth ners prion, Toston chet
connects ‘that the semantic actions refer to attributes whose values
to ensure)
ved by a previous semantic action or by the lexical analyzer
a
‘comp’
a Mirected definition, semantic actions associated with a grammar
vouction need not be performed i 2ny specific order, whereas in a translation
cdot axons must be performed in the same orderin which they are specified.
Refer to 44
(0.49. What are advantages of syntax directed translation ?
cz Syntax directed translation is a scheme that indicates the order in
which semande rules are to be evaluated. The main advantage ‘of SDT is that
ips in deciding evaluation order. The evaluation of semantic actions
a pecated with SDT may generate code, save information in symbol table, or
may issue error message.
0.50. Explain the types of attribute used in syntax directed translation.
Or
Differentiate between inherited attribute and synthesized attribute with
example. (R.GRV, June 2016)
Ans. In a syntax directed translation, each production A—> a has
eee ‘it a set of semantic rules of form pp Carevnrrea Oe)
fis function, and
@ bis a synthesized attribute of A and c}, €3, ¢y are attributes
; fo the grammar symbols on the production, or
Gil) bisan inherited attribute of one of the grammar symbols on the
of the production, and c1, c, cy are attributes belonging to the
‘of the production.
‘Scanned with CamScanneraera
s- tei said
M Shes eine omatibutesalyso
Melee pace ibues are used extensively in pracy thea
Spat uss ait td we ans On
on dfnition parse HC CA AVY yh
for the atrbutes at cach node boy
Aninherited attribute ison
ned in terms of atrbute atthe pay
es dbus ate used or expressing i er
me angage consruct on the contest in which “ee
ree sya esd dion 0 No ee
Airibues, An inerited attribute distributes type information to 1h.
identifiers ina declaration. “Yah
1.31 Diferentane beoween synthesized translation and in
a (RGR, Dec. 2008, June 2005, i
stm Syteszd translation dines the vale ofthe Waly a
SS nan aide of production asa function ofthe asl
nails de igh sie Sochatraslationicalle! synthesized ania
Consider the following production and action ~“
A-XYZ{Y.VAL:=2°A.VAL}
Here the translation ofa nonterminal onthe right side ofthe prod
ee mete rundaton ofthe nonerminalon the lef. Sichatanten
is called an inherited translation. ranslain |
Wes ees tine alse They are more natural hn
inherited tansation for mapping most sming language construc
feome cl Bataplapeed spy cjcion va
top-down parser
How.
932. (el ha baad
brief note saris (R.GBV., Dec. 2005, June 200)
(RGR, June 206
(RGRY, Dec. 6
to specity the constuctit
‘of language construc.
tation allows transit
‘a condensed fom
‘syntax Analysis and Syntax Directed Translation 117
for representing language constructs, The production §
might appear in a syntax tee as
ethene
S;
eae ‘else Sr
tree, operators and keywords do pot appear as leaves, but
ya Ber err node that would be the arent of those leave in
nae
mn be based on syntax trees as wefl
with
occu. ‘syntax
as parse va prected translation scheme provides a method for describing an
sya pig an hat desertion dependent of any implementation
age cnt TT After writing & synuas directa wanton scheme itis
1s gam that implement the input op mapping One way
Favre irc translators to we extra ids in the pase sack
so impeeronding translations. «grammes involved is LR grammar then
secimen tine artmetc expression gamma ten follows ~ where S
Fa am ands for exresson an | fF ner The productions are
a @ SOEs (i) E> E+E
(i) E> E*E (ivyE>(B)
@) Bo! (vi) > Isigit
(wi) > digit (vii) digit > 0123.../9
spe seri actions ar add tothe productions, wih each ofthe nonemnals
andl one ntege-valued translation called E.VAL and VAL is associated. Which
sand the rumerca valve ofthe expression or inege represented by anode ofthe
sete labled Er. The st of mantic action i grerted fr ech an vey
lucion a the fist step. The set of semantic actions are =
directed translation ca
nto a
{print E.VAL}
{ EVAL: = BC).VAL +E®.VAL}
{EVAL : = E.VAL * E®).VAL }
(B.VAL: = BC)).VAL}
{EVAL = LVAL}
{ LVAL== 10° 1().VAL + LEXVAL }
{LVAL:= LEXVAL}
‘only indicates different assignment of digit hence
tic action specification. The semantic action for the
term LEXVAL ic, lexical values, which means any of
‘Scanned with CamScanner(visem, 6S-5'eneh) mai
‘Syntax Analysis and Syntax Directed Translation 4
e lation 119
418 Compiler Desian
the valu asocatd wit digit (0109) 28 indicated by the expr,
i ‘ e wv, the parser shifts thes
foie i No Sere
Value, The eompete parse whose EXYAL gan obs eserecan AL e
mnie ne ary og of te ack ety for gto aczurefte uke 2a
seein The onal ean 1 top ofthe VAL stack contains the value af
i ata gen mt er win ele oe ng oe
rite short neon the rpndeny raph (GP ame 20)
sit
rebate bat anode in parse ee depends onan atl
Be iat vole one be fn a
parse tree using stac
. Iran at
reduce technique cam be
Testy done, Table 2.33
Shows the sequence of “i enantic
em-3@ © © uu sen odie © THe imterdependencies among the inherited and synth
ue at defies in pase Wee canbe depicted by dete gap
moves made by the parser
fon input 23*5+4S. The
‘column STATE
but
sieaa dependency 8760
graph has @.node
ode for © if a
fot cab en Cipe ea
eC tegen on bau e e a
the status of the stack and
the column VAL gives OC | jon he
Valuation result at any 1%8t-t GE Gad eno gph fra given parse te is construct
parle. ot and LEXVAL=3 TERVAL-s se Sead ae pigs tS a6 see aaooaeal
production columns efor ‘il
‘represent the input applied Tor each attribute a of the grammar symbol at» do
se eee es spstuct a node inthe dependency graph fr 35
aa on ey Fig. 228 Parse Tre with Translations | foreach pode nin the pans w=
ed for each semantic rule Da F (Cyy Cyndy
‘Table 2.33 Sequence of Move feats
Sha] Taper a vAE cere E ‘associated with the production used at n do:
Ne ze Production Used for i ;= 1 to k do construct an edge from the node for ¢, to the
Del aeeee [os node for b:
By Seseas 1 i For example, suppose A.a : = f(X.x, ¥y) is a semantic
@ [esas | a Ge To digit juction A -> XY. This rule defines a atin d er condi oe
Gh lssas [1 Bi the atributes X.x and Y.y. If this production is used in the pase tee, hen
(or heasaelen ) Tey igt fare mill be three nodes A.a, X.x and Y.y in the de eek
(ysis | Br 23) ESI tdge to Aa from X.x since A.a depends pe ee ee ites
© | Be She Oe | | Sine A.aalso depends on Y.y and on ies 10 he
45 Bet i If the product
a PUPA. sc woduetion A> XY has the semantic role Xi: =
te oe Ks 03)-5 aie mess eco ie ma
es E a3) rates ‘Vy. Since X.i depends on both A.a and Y. os
(3)/ eee? 0.54, What do you und bees
t ai Ea (ii) aiid? ferstand by bottom up evaluation of S-atributed
5)] is)-4 | 1 digit - SES
5)/ 5 Be | dis)-4| Et | “Write oa ae
E > ‘short note on the S-attril
ae tus oer mesic attribute definitions. (R.GPV, June 2009)
soeize nut “an be evalue aris’
ited & 9) jes ‘te inptis being oe ‘can be evaluated by a bottom-up parser 8S
ie ses ing parsed The parse can hep he vues of te
= with the grammar symbols on its stack, wl
Scanned with CamScanner120. Comper Design (Vt
is all
reduction is made, the ¥
a he atbutes appearing oh
fight side ofthe reducing producto?
‘Only synthesized atributes apps
+ fig. 2.29 for constructing the
ues ofthe new synthesized attributes ap,
yma ice dy,
Pere wee fran EPSON Fe oy | Eapi aimee
re approach of tis ca | yey | Raptr = mkaw¢ 2
‘refore be applied t0 | Eo» Kemper = Taptr Ty
therefore Ri Ee) | Taper apr
cemstruc syntax trees during
bottom-up parsing. The
translation of expressions
Adrng top down arsingoen
uses inherited tributes
435. Write short noe onthe L-atributed definition,
(RGPY., Jane yy
“Ans. The order of evaluation of aributes is linked tothe order inn
nodes ofa parse wes ar created by the parsing method, when tansaion ie |
Placed parsng Anew class of syntax directed definition cll Lat
esntions, whose attributes can always be evaluated in depth-first on He
tands for “Left because the information flow is eft to right |
‘A synlax dreoted definition is L-attributed ifeach inherited ate
xj.15 50, onthe right side 0f A > Xp Xp) eoe-o%y depends on
(i). The attributes of the symbols X45 Xp, som» XU the eof
in te production.
(i) The inherited atributes of A.
(ii) AllSatvibuted definition is L-ttribute.
‘The syntax directed definition in fig. 2.30 is not L-atributed becuse
inher ibe no ste pamnisebolQ depends one auuibues
of the grammar symbol to its right.
‘translation scheme isa context fee Seno
grammar in which attributes are Lista
roid
Bovoum
Fig. 2.29 Syntax-directed Defi
nition jy
Constructing a
na Tree for an Espa,
ASLM
associated withthe grammar symbols and. Mis ail)
‘semantic actions enclosed between braces en
{reinserted within the right sides of | °°" ata
production. For example, A simple Sisco
translation scheme that maps infix
expressions with addition and subtraction Fi Von-L-attribuiel
mae h jon Fig. 2.30.A Non-L-at
“Aaa Postfix expression, Syntax-directed Definition
R- addop T {print
TS naitnie ec Jexeme)} Ryle
a
‘Syntax Analysis and Syntex Orected Translation 421
Seen gana
so!
refers
on |
Fig, 2.31 Parse Tree for 9-5 +2 Showing Actions.
ir
Semantic Rule
Tal al * Fval
sid the following production and semantic actions ~
ToT, *F (val :=Ty.val * Eval)
0.56. Explain S-attribute and L-atiribute.
Ans. Refer to Q.54 and Q.55.
0.57, What is a syntax-directed definition ? Mlustrate with an example,
Explain S-attribute and L-attribute definition. (RLGR¥., May 2019)
«Ans. Refer to Q44, Q.49, Q.54 and Q.5S. ‘
0.58. What do you understand by top-down translation ?
(R.GRY,, June 2005, 2006)
(RGPY,, Dec. 2012)
Or
‘What do you understand by TOP down translation ? Explain taking”
my example of your choice. (RGPY, Dec. 201)
top-down translation, L-atributed definitions willbe implemented
parsing, L-attributes always be evaluated in depth-first ordet;
nslation we work with translation schemes rather than Syni@=
So we can be explicit about the order in which actions
fons take place, We eliminate left-recursion from translation
4
‘Scanned with CamScannerscheme as follows
‘The transtation scheme of fig. 2.32 is transformed
scheme in fig, 2.38. The new scheme produces the annot
2.34 forthe expression 9 = 5: 2. The atrows in the fi
determining the value of the expression.
in fig, 2.34 the individual numbers are generated by
‘al fom the lexical value ofthe number, given by ateib 8
in the subexpression 9 ~ 5 is generated by the leftmost yy."
Operator and S are generated Dy the R at the right chy te me
inherited atribute Ri obtains the value 9 from’Tval. The sue fk
thoparsingo the result4 downto the middle node for R are yore Sat
the flowing action betwean T and Ry in Ry —> TR PYM
(RLis@ Ri Tal} }
similar ation add 2 othe value of 9~ 5, yiekin
‘the bottom node for R. The result is needed at the
> HT fea!
Op Bt sat
BT foals Fat
Tm (EYE vat Bava
> bem (Tra = moma)
ig. 252 Translation Scheme with Left Recursion Granmay
Bot ds Toa
4 into the ,
aed Parse 4
Ue supp
YT, andy |
he rsa
vt
R (eval: = RS)
Roe
Tyr
RE (Rs: = Ris)
Ro.
T (REL: RA~ Teal
RE (Ra = Ris}
Re Ree Ry
TOC
©
> (vals = Reval
ig. 233 Transformed Translation Scheme with Right-recursve ranma
+ Tvale2
‘numa i
ig 236 Bratuation ofthe Expression 9 5+?
SYm0X Anata ang
Syntax
ction of # Predictive tot
construct the Syma Tn
unl fo preitivepasng "With an inderigs
i it~ Code 10" 9 SYN deca yay ~~
method - The technigue i a megan a |
tion. of the Predicts
const e-parer
(Fr ech nner A comin
cet foreach inherited atribute of a
paragied atsibutes of A. For simpli,
rae ayntherized attribute, The furcr
that each
88 Hoa
oe
a
* olin a
ion for Ai
bute of each grammar symbot that appear, ye
(i) The code for nonterminal A
cn the current input symbol
teed (ii). We consider the tokens, nonterminas,
fhe production form left 10 right, The nea
joes the following
(a) For token X with synthesized atribure
inane variable declared for X.x. Then generate
‘advance the input
(b) For nonterminal B, generate an assignment ¢
with a function call on the right side, where by by.
‘ariables forthe synthesized attribute of B
(€) For an action, copy the code into the purse, reph
reference to an attribute by the variable for that attribute
0.59, Explain botiom-up evaluation of inherited auribuies,
‘Ans. We give @ method to implement L-attributed definitions in the
framework of bottom-up parsing. This method is capable of handling all Le
atuibuted definitions in that it can implement any L-attributed definition based
onan LL(I) grammar. It can also implement many (but not all) L-atrbuted
c
has ust
cel atti
€, For
» the translation scheme 7
el
Scanned with CamScanner