Professional Documents
Culture Documents
Ss Mod4 PDF
Ss Mod4 PDF
o Parser or syntax analysis basically checks for the syntax of the language.
. A parser or syntax analysis is a process which takes the inputs (as tokens) and
produce either parse tree (Syntactic structure) or generates error report.
In our compiler model, the parser obtains a string of tokens from the lexical analyzer (as
shown in Fig. 4.1,) and verifies that the string of token names can be generated by the grammar
for the source language.
In the process of cornpilation the parser and lexical analyzer works together. That means,
wlren parsel recluires string of tokens t invokes lexical analyzer. In turn, the lexical analyzer
supplies tokens to parser
The parser call the lexical analyzer (by the getNextToken command), causes the lexical
analyzer to read characters fi'om its input until it can identify the next lexeme and produce for it
the next token, which it returns to the parser.
We expect the parser to report any syntax errors in an intelligible fashion and to recover
frorn commonly occurring errors to continlle processing the remeinder of the program.
3. Bottom-Up Parser:
Bottom-up methods start from the leaves and work their way up to the root. In
either case, the input to the parser is scanned from left to right, one symbol at a time.
Ex: LR Parser
Planning the error handling right from the start can both simplify the structure of a
compiler and improve its handling of errors.
. Lexical errors-
It include misspellings of identifiers, keywords, or operators .
'Syntactic errors - include misplaced semicolons or extra or missing braces; that is, "{" or "}."
As another example, the appearance of a case statement without an enclosing switch is a
syntactic error.
' Semantic errors: include type mismatches between operators and operands. An example is a
return statement in a Java method with result type void.
' Logical errors : It can be anything from incorrect reasoning on the part of the programmer to
the use in a C program of the assignment operator: instead of the comparison operator__.
compilation.
(x*y'r*z)*k /
. reading (, X, -F, Y,
The parser ,.un, the input from left to right and finds no errors after
and*. *
. and it
After reading the second 1, it knows that no expression has consecutive operators
*
display un erto, "Extra in the input expression"'
. token found (i.e 'z')'
In panic mode recovery, it skips all input-symbols till the next valid
Here z is the sYnchronizingtoken'
Phrlse-Levql Begoverv :
Ilrrgr rlroductioni
. The error procluctions specify commonly knoln rnistakes in the grammar'
when we imprement the parser, when an effor production
is used, it displays the
appropriate error message.
For exampre, consider the expression r0x. Mathematically
it means multiply 10 with x'
* errors can be identified very
But in programming languug. *, should write 10 x. such
easily by error Production.
Global Correction:
theoretical interest.
4 . 2 Context-Free Grammars:
E) E+TIT
T) T*FIF
F+ id
l. Terminals:
. Terminals are the basic symbols from which strings are formed'
. ,,token name" i, u synonym for "terminal" and frequently we will use
The term
the word "token" for terminal.
* tidt .
. In above grammar terminals are * , and
o
2. Nonterminals:
. Nonterminals are syntactic variables that denote sets of strings'
. E, T and F are nonterminals in above grammar.
. language generated by
The sets of strings denoted by nonterminals help define the
the grammar.
f*
3. stirrt symbol:
. The first no-terminal of the grammar is the start syrnbol'
i]
The productions of a grammar specify the manner in which the terminals and nonterminals can
be combined to form strings.
S ) E+TIT
(head) ; (body)
P "T,A
fc
't
?" Unlw* stated otherwi#en the head af the first pradtr**i*n ie the st*rt syffi-
bnl.
{q
I
3'l"g--eit'"*, "!: gY*b'D
, .fl. g.'"e.al o| 9tt<tso6 ,^zlth r'on-*e*r"';nJCsio't- "h ^
abJ succees?vefu te?tAu'''fl iL-bd tt',. b"\ of, oo- o$ ita
Xvo-,nrruv
Fvcdrrsons ts c--L-[r*l ^ &ona-f,'Oo.
'T-;to o Tir ptg oh D e'\wo-iiDcra'.
-{trz- stnnn N F{
A + X B $- o",d ts+ p a*e, '\.Ac- P*&^.1-
o nE ,
o AV
cqn \>u obtdooJ -e','* 'A^ ?Fd;^t}"o'
\
t*P{ A?xBI)
A+e Bil *fiu- P^,od'.u$"on
- 1d'P{-(rqt^k-,8?F-*n1fio-p"'dut$onB-"d
o.'9 '
I\>e- a-bor.e dt-lt'^fuo- cctn oJgo be vo*{"n
hb *pr
. -rt. 9y-to[ " +" '"d.*"* ln one gt?'' -
m('-^a
? xp rs &a"dJ-"o$tn 9*+
^+xP ".p",.r"^16th"f
@ n d.-s'o X P !n. orP-
s+"P '
*
c Nt =+Xn C
, X,5o(^ C
X, & "r{*fcA odn ^, D o o<- o( rr\o€ *t*+6 '
n
9vb= :
+
'le-.
C s &*\r-4 o( \n $-vo
ot- fflo'e- *+.f,
E+ r d '[o.1 * Sente'''c<-
Ir
@erPro) rrt Ntta
-r-
choBeo
le$trnoc€ 'crbc,terror,,.e"L 'rn eadr g"^-k^$ti
'
,'Th.
c<- eb-
. -tfre- pvocrrD r* o' S{cor6 o ! -{s-*mr"ol* 4-"""^ o 9-qro-rr
obt+'^-i^3
+u^-t-ut'g, t'1ri-J'st' a"'\.-co-<[
aty\'-o"ne-*=** "'o''-+.*'''lJ'ro
o} "^* l-d'b*oec c-{.-?''rJoo'
a^4 e-'c-*tr =*o Ytc'0J,d
(^-){- .,c*{e- *#P
L. @CRMD ?n eqc'b gt.'-tt'''t"9' ts a-0oo16 <J'avo'
. '6c-'ngn*.r,orE- noote-'doJ
n
o o(t-te""'lJr *:' Seq'1e-"'ta o6
.'t'\-.p',sc-cre "[ obtai".r5 *tnt
l a ?"7t'co.l
a.zpl"'ci,.,,-.]- S, r-A il*.t "''J'tr tlt'^t"no4f noo-{e9"-^t-1
*-L, *-",J
?J +\J
*o
"'&
At
t5ftt'od *3
d-""''"*on -{"' Sttl ld'+:J'ald cc-n ba
-tFr- ?iqt^tr.og+
v-
Sho.,rn bnio O .
1Z',q# 9'^+'^d*{
UIA+,'+e"A'I "a
9+ L+E o eb S e. ,^y wa- g?'*'t u i6 a'
4 E+ E*L #
+84 E* TJ ' 4,- 'h th- flr+n''-".
L,U- s"^+^#.J
E+TJ *TJ -6^ -tAJ x t\A a-
+ I
" + >< , v)e- n"'d
So lo'- t
f\D. Qqrbeq=
L \AD Q
S+ss-P$ S
s ? Es*- Trr)
+ So-+c
,/r \
,/t\
+ gs+sx S3-|e
+ ctQ#Sx -) 39Ja'-r ./\t
I
,/ l\ q
) aa+$x =) ta't-a'n
* S bS +
+ aa.*o\k 'a ecl ta
_\
\\
, C.q
E+ ?d
3ot'" Qr-^J c-Lo"* otcttg '
"'- ?tv.rrvrv-c-
6rr* +AJ \t fA
Qrebi Svnd'
0 cong*d-r Qro rnrrta-A* o,^J-
E + Eo.-E
F-> tr+E-
tr+ tJ
3
fsnnttt^c") id +
id' orld '
goloi coneld,q tlv- stvin6:
*t"3 t .h€-
^'i
€ti-q-rnzrrg- t".rq .^^ o^u&-*"o 'ht'6**J- Po"{"
.: cco
0:
Po"6tlJr€4,l 'gt
39oce ldr"--{^9o
db4*-t--
t a-.-l= o3 gLooo b"lo"o ,
-l<r #'e gaa- r.6 a't'lt^,"'^s'
ltt [suxt^r.) ,'tbe 6'aro'nar
g E
,/t \
L + E
,');
| /\ \
/ I \ \r
t
id L t- E
t\ L+_E
n
d id Ir
id \d
n *16o n,
, Jya-rr)nq-r,5 aga rnoz- Poro*$J rlotc.-t''on R. E
/
fr+L
{c
pvsd,r-.-Soc, h-*^4.
of st"*- I ut"f" i oD ioP'$ t.,
o,a b
X
o-& t-,.. pro&-cl'oo Al -) ru
q. Lk ,,u th. IcaJL sboJe, rl/ta+<e- A; Se t''
gb'rtsg-bd
9F tAt flYa-rr"rqr
&n*"r€w*/t'2''1
Ao+ (H"s
A,+ t+l"tAt l*,i Ms jL
\
brnar :
LF3 , | lsru c_*I \hrs.^-g
"t"$c.A
. plurlffi^g "fh..t c".^ be d-egcrrtb; Cd"{"^"J, F.f
? -
can aLto be-
drrcrP be-J l>y a eva;r>rnafr {^{-n
9J
tr rohV ?. E lr1 d-u-$ n . llr
v,-*>e c Lerct cAI glntoocb o [- G Long uoSog'
l
abaz qg\z SorennL Ato,gan*,
F_+ El a elr
t+ trtE
I
9+ Ln)'\ J
o-4er.i ^o{
ocioS ritY
T
A ss no
g-,P@fs ,-
+t- T.Ft
' , rf
1)
F, / LoW T
4 P
Rioq \"-f
F
8 os+c u,.i t Cil., ?J
Un o,nlA1.re. g ffsm3t l
\t4
0w
E
-) E+T \ e-t\ 1^
T+f*rP\TtP\?
lt,
P+ F^? \F
F+ CE) \:J
@ c-onutr-f tA- +[o.o]rro, curnk\o6unu-* rbL i,tJD Unctnn$^rr*
'
.'t{ ?r.*- t*r.I
e_+ E+E lE-L
tr4 E^E
E+ tE) [ rf
.|to6
bH C-on3i &*",* * anJ ope p4orr [oo'"gt ?$t>d\ anJ Gs-€-
UJ -
L. [-f o;rto c.\ 4;w , / *d + oporos,"l hone
'th.- tvgh^c.gL priori ! on4
aA!. *aB * qSlo1a/7ive ar,tJ n yt*o v hos p reCcdanae \ n bdi^r-zn on d
"p.
i t 16 L*L a-s'sat".$ *
eo [n,. r - tn,arb
.DgsDgJ'\ 11O D-telrot'
e-=J*ctcs ' o De ttJPrj f
L
(o,^r.gb) kl€ l_"f| ,.
Ci n tra"ry) n L"t+ P
Ch\qh.rt ) - /,r* ?tfl"-t_ T-
$
ft
a"l a,
,, .
St.n b '
/r \\
/ t\\
i+ e*?r ii.,eo St"" F
/r
I
E1
I I I
I
Ly Sr S2-
/t
Str.t
T;h::);*"3*"*
a*^"
11L €r?n th"" St""FL il+-" 3t"""b
q" uJtt*-r par6)"
/tN
+o '- sq-rrne, **"8
I
1 CSg"*.^q ,
S,>
tr1 \
EL Sr
e
Uog-b.lgu.o*a 9 rq_nnr^qL* -
Strnt
-), oeotcheJ- st^t \ tl"n-st'nt
I o tl-,ur
/t\ \
o Pert- s{"n b i'
/\
i+ e*P" f?a.t a^ft.r'"e-s+,nt e_@
I I t
E> Sl s2_
@
roosldJL \ D c-n
tL\ ,kn gru-tnno^L " gho(jo.\ bJ-o*r o.nd P.,{r,qlFr6
O,r,b{fl*.* . CLO{ Qonr<..q-t it )^b tlocrn,[,3u4r{ ,
8*rDrry1l^
s+L EtsIt Et S eS l*
E+b
Sg_L"t C-o,s)d.r tl,e 9ti,3 [s""+en"; $bt?bto,e-a*
? Lt
-/ l\\
;/r
s
I Lt Ee
s \
N s
z\/I\
i Et
I
b
s
s
CI.
I
Itt 7L
taa I
+s
o.
b
rL L IE LS<-3
I l l1
b 5 a* q.
?bt Pb t a s-or-
+
4 g3 Lb*tr$un oF L"kL Pa,.rsTan."
L"Ft Qn-u,tr'9i oD
F
9Je 4 tL p-d*dton t6
"'
Qn^,il
,-l 4,'r).. €-at 6 ^,
,
Qun;:t"SJoo:
u-,b
*mr**b -1b4 T'r3 L^t siJn- g^d*t{ {b J61rAa o'3
o o "$tfin-
Jgd'-"& th.
o
P'*&-'J$o
bz eymbot oo the- t4+ han
LrE'- f
++xn\ r )
L+T+E\T
A >oAlb
, *t,:* P-'n'd"".F<
gLo^^r.' b.to*l
' A-+ P B',
Ar-> xd I g
o r5+6rD +" el...ri n^Ie k4+ ?"ur€ion i
Tnp"i; Q"--rnar q iDTdfi no q&! @{ E- prcd.'dSon cbntai^ &-f^d,rtt*)
tt1"' L+b 9ei-t-trgioo - Cit -o..l
ou*p*t : Qro--*, ^' riD -
p4**doJ '. A?d-^d eho d fl'o 6 C"l
. S 3ramma.l
)o o{ r',o€_ p.ro&rr-So.,g+*r- a- noo_4rxro}rqr(
eohJch -t\,eo
A dp.,ot hav<- o Co.ornoo p".h a of gy.nbot4 oo t6e- nfqr^-L-hqn.l
SIdo ob tb. A-pv"d*cAo^8 )Eco[e-J t_.pt €"U""ed
Trainrrnr,
f
"t @
-
A xl,lxP,
u&t .fl*S""r{ra"^ ny{
A-)o<fi1
A,) F,lp.
ErcX
o s+ sS+]sbe
t
Soln\
f+
s + obsl
ll
tr+ aq S'-) ff tB
B-l qb tr+ ."a-
g )o*b
J
,^\ ll
@ c-eJ-fi" -[f.t -'q noo- [..dt*rn1 g'arr"'','r- Crnvut rb_ m#a
^'A
eou-ry,-Qr,.*.Q- t+ (^r&,"'qr*1--,
t+itrtss'lo
rf
S sJes
-)
tr-+ b
r,A
hb^#- #. t"k+:fub#\z
Qt*"o-r t C.,so- L.bt- f-ttot 9.l"rn''*L)
5,np,rL:
o"+ft : .A"r zqtusvaJont U't+-.&Cl"^A Qvunor.*--
m{*fpJ t
t. fr. e.ch noD-{e'r,rro1 A, Fl",dfl->.'Lo7*e prcfi* oC
eorn rnoo to -too o{ rrrotet af*efndfn a , "
sr 4 F,JF, l 1F;"
-f73 1il.e tm'o$,-o**, ,,r-,.fi( no +o. .d{tnJ"u
^p"r&ed
-&" * noO-*e-C.r.,rro,{ har.e- CorrrrnOo p2rfik .
.. Ao TaPdog,cn porsioglbe p.rae trt-e 16
Seoerotrecl
{-""^ 6p to
bo++tr1^,'Ci.e 4"* "aoot +t [e.*s) . '
T^'1ti6 o+Prac},, r^re. gtlab t,c\tA 9b*b-91^b'o( 'ou Ke-<1, oo
'
drr.,r*'3 th€- Vorte +"*. C1-, r4*n-16" non-{u'l"al"*"n3 9t"&r*"0)
u.,b,l ,-nnkr,d.. B.F *b" !l*,- i"p^.&"<ttig', .
Blg$ln e'P"-'st*-D"
\/o?d Ar )
t
c-h sobe. ar> A- rprodurA\oo , A +X, Xz Xo )
-+-" Cf= t +o' k)L
i + LX, lrt a no^ ta'rnln "-i_)
c^l^[ proced,^re- Xi O )
nJSu i + (Xi eru",a,i-s -1fi.- G.'.,.'.f tnpJ -ey",bol o.J
o*dvrrrn ce- il--- $op** -{.o tbe- De&t 9y^ 5"1 )
I
elA e-
€-.r'Y^o.(- CJ '
)
I
3
S- I +'cAJ
A-+ abl",-
9o lo."
\np.l* gt'n3
s
./l
-C-A d iol&potnLl
ft io pr^L Po k\i-t -
a. c I o ld
tra-Po\^1*-
dc
C us,'ib tf* Q"{rrr$he D esceb
PorNV il. W
J Qro,rynil-
S+ r' Ar
E+ L+r A + a,b la-
\
$otoi
VolJ EC) 8oh,t
f r/otJ s q
e (); Lt\
ll UTJ ey1-h[= j+') .ll
iI (tny^I g1-boL=
='O' )
{ ft:a&{qnaz,'np,J
ad vanv- tltz-\of^t Povtt*; P'oin$e,
Tn; AO)
)
] !h C,o X>^Lscl,nboL = ='J
/4e-
gftArL)', np & pnt'*Ll*
I
;
^&:fa(xQ9t ctusrn ' d\l L
) t
dJr.-
C F+ Ce) l;a er}trL))
3
su rda FL) i
a
l)
[\ AC)
i [- C,'rpJ SqmboL= ) (') sL
- , - r ,\
f i h CAp*f s3obot = ='q' )
nJn,.*Le iDP^f Po\ntrr
f
EC) ) cu.dto6 k-,nFJ S.1 rnboL;l
:"
Algo"'fu
--rFJF---+
3
I .
r Ttr x r$ a *e'rnioa0 ,4-rrnn FrBsr
cx) , f r I
f
o s + #gca
S-+ ?sfrs.s/"
A ++al g
\nbol 9 F tpv Ftlo.c I + +*ele
I
o
.4_,J
la
c + vtslz
S-+ CC 31 onL,o l6 Fr&sr Folto u.-r
c-+.C \J s + -yT./ A +
S.1..,bola
Ftto,^r
A -l- L +r "/ J
.d {q
s B *L +'A.I
C c!+ L 7L d
L
(r)
l. A.hE** t*-rna.s. f{t not r_u ,
flrarnro
3. "T6" (,q fletrn rno-i- t^.r Lic |^y iA Alt t"$ -P"*"*-d f6 elgo not LLtt ,
pcrs?n q
T..ud
vl
T+ Frl E, (iJ )*
g'
r1+ +FTl L +€, )j
F+ cE) id T C ;d +)3
TI J(a +)+
F c?d .+# ) +
3
pa-rg\ n tabL"
id + # ( ) 4
E E )TEr E;ral
I
E E \+rtl E\E E't+ €
T T+ FTI F+rr I
TI T5L o-l
r4*Ff I
15 €. r\ €-
F F, ir F+ te)
m
"khe-d sfuw fI f ,^-t- +*oaL
E+ i{+id +
j
td + rrtl id 4 MoJekd +
iJ+ FTIEII rJ $ {
o,/p*-t- Tb FT I
i"t- + Fidr'Et 4 id 4 o&t F ->, d
fc+t.{ rt elj I id
rd+ld L,4 f TL) I
8-
H+id f I t
f,r-S e
tr/
{t
C-s ns{d,r the- Atc--Tot
A+ 5+Tl3-l '
'T+ v \u*u\u+v
V+ "tlb n
$. ?"edig['ve pass's
2
S. tT-- gt-stot(*
@ l*.
? Do oeLe-tL*1
c,"o"a*
oo y :; * F-edt
@r"h*i F"rt ,(6, LLtD o'r€b
6 c-o*r^r"
ct^n'tt-'*1
::::*-
::1 ch.L,
6 tPt*l'"*t : :^
Po'6t-tr{€
CO til
const^r"'$1
tl
hD I t(9q,
E+g+5 Iu--
_l
I E
5z +
T-)v I
{q
.l
S 3?.nnomos f L LL CD '
4ftnk- '+t'<. ? e-s*Ijtt
o- b :r + +
6 3
5fl EtA;T
tr E+
T+ tar
I
T-+.rt I
T rbE,
T5*v r-I+ v
T I
\^-) o.
v+b
;
io P".&"u4;'u Po-.sl ,
re .An srrl,( JL
d+.pa &'\"fl p"'d"t#"e- ?arslng uchrn'tf"q--
q) tt
". -[oqr"o,,nJ..tp 06 gtrq< Cctonot be_ ,r._[ehod
) PtP -ta <
-+.Jroln ^Jrs -R-,^ tfrn- st.c/< c.n J \sSrra v €-ro, t-De_(ne
qn 4- ,./
T+ prJ
T'+ FT'll
qA
F + te) lit
@co",rt'*J tlre Pa'sl^O
'ba-bk,
E;TEf
f-+ fJ
A P".-sl brorueil {o l<o.,g
?A -l- + C ) $
f D+rll E+TE I
Syn ch Sy ncb
E
I
,D+
E+ ry+
iJx-+-lJ +
?-*-t , 9Kr'p -Remo\r< )l*"p{
o.r*-p.^-F D+TEl "tPt^,
\d-r.- i d I t'
{q
ALQo hr+{ w) : corro+*
"!tr^ n#- ,eM
?q'oilv"q wilo
furgu*-:
I
n
e,.tO,vtnwgf q ,
f\i
ptl{putr: --il* l-1 i
I
f*unj
- lrn
F{e5o4' ;'
/.|
'Frl. 9"ll.
- fl*^hn^{.
L. ?t e,ca-[. lrr l,*.1 a- )n cL{)/
fnR's
.di A.+ .L +@ M t,S.Al
+?*lt^y t-d
g. . rP L $o $,T FI.-..\T (tl,
d.,',.t$rJ l" n po LJ-o r^,r (*l , "A ry
rr