Homwork #2: CMPT-379 Disribud on Oc 2 du on Oc 16 Anoop Sarkar anoop@cs.su.ca Rading or his homwork includs Chp 4 o h Dragon book. I ndd, rr o: hp://ldp.org/howto/lx-yacc-howto.hml Only submi answrs or qusions markd wih. Provid a makil such ha mak compils all your programs, and mak s runs ach program, and supplis h ncssary inpu ils. (1) Th ollowing program is yacc cod or a vry simpl (and incompl) xprssion inrprr. #includ <sdio.h> %okn NAME NUMBER samn: NAME = xprssion { prin("%c = %d\n", $1, $3) } xprssion { prin("%d\n", $1) } xprssion: xprssion + NUMBER { $$ = $1 + $3 } xprssion - NUMBER { $$ = $1 - $3 } NUMBER { $$ = $1 } Th... scion conains arbirary C/C++ cod and h %okn diniions is a lis o okns providd by h lxical analyzr. bison can b usd o convr his parsr diniion ino a parsr implmnaion by using h ollowing command: bison -osimpl-xpr.ab.c -d simpl-xpr.y Th -d opion producs a hadr il calld simpl-xpr.ab.h o convy inormaion abou h okns o h lxical analyzr. Examin h conns o his il. Th lxical analyzr can b dind in lx cod as ollows: #includ "simpl-xpr.ab.h" #includ <sdlib.h> xrn in yylval [0-9]+ { yylval = aoi(yyx) rurn NUMBER } [a-z] { yylval = yyx[0] rurn NAME } [ \\n]. rurn yyx[0] Th lx il can b compild o a C program using lx: lx -oxpr-wih-vars.lx.c xpr-wih-vars.lx Th inal inrprr binary is crad by compiling h oupu rom lx and bison wih a C/C++ compilr: gcc -o./simpl-xpr simpl-xpr.ab.c simpl-xpr.lx.c -ly -ll cho "a=2+3+5"./simpl-xpr 1
Convr h abov yacc and lx cod so ha i can handl mulipl xprssions, xacly on pr lin. For xampl, i should prin ou a valu or ach lin in h ollowing inpu: a = 10 b = 5 + 10-5 2 + 5 2 + 3 + 5 You will nd a rcursiv rul in ordr o handl mulipl lins o inpu. Try dirn ways o wriing his rcursiv rul. (2) Th yacc and lx cod in Q. (1) dos no y handl assignmns o variabls. In ordr o implmn his, w nd wo dirn kinds o valus o b rurnd rom h lxical analyzr: on or numbrs, and anohr or variabl nams. Th ollowing ragmn o lx cod now rurns wo dirn yps o valus o yacc: or numbrs i rurns yylval.rvalu and or variabl nams i rurns yylval.lvalu.... /* rmovd cod common wih prvious lx cod */ /* convr NUMBER okn valu o ingr */ [0-9]+ { yylval.rvalu = aoi(yyx) rurn NUMBER } /* convr NAME okn ino indx */ [a-z] { yylval.lvalu = yyx[0] - a rurn NAME }... /* rmovd cod common wih prvious lx cod */ Th wo yps o rurn valu, rvalu and lvalu ar dind in h yacc cod using h %union dclaraion, as shown in h ragmn blow: in symbl[26] %union { in rvalu /* valu o valuad xprssion */ in lvalu /* indx ino symbl or variabl nam */ } %okn <rvalu> NUMBER %okn <lvalu> NAME %yp <rvalu> xprssion samn: NAME = xprssion { symbl[$1] = $3 } xprssion { prin("%d\n", $1) } xprssion: xprssion + NUMBER { $$ = $1 + $3 } xprssion - NUMBER { $$ = $1 - $3 } NUMBER { $$ = $1 } 2
Th %union dclaraion can includ complx daayps. Th yacc cod dins a yp no jus or h okns, bu also or nonrminals, which is spciid in h %yp diniion abov. This allows yacc o chck ha h yp o h non-rminal xprssion is rvalu, an ingr yp. Us h abov cod ragmns, and add h ncssary lx and yacc cod in ordr o handl h ollowing inpu and oupu: Inpu: Oupu: a = 2 + 3 a b = a + a - 2 + 3 + a b 5 16 Exnd your xprssion inrprr o includ consans o yp doubl, and variabls ha can hold ihr ingr or doubl yps. Finally, add h uncions: xp, sqr, log so ha you can inrpr h inpu: a = 2.0 b = xp(a) b (3) Wri a Dca program ha implmns h quicksor algorihm o sor a lis. Your program should prin h sord lis by iraivly calling h prin in library uncion. Submi h program as h il quicksor.dca (4) Wri down a conx-r grammar or h srucur o Dca programs basd on h rrnc grammar in h Dca languag diniion (mak sur ha h non-rminal and rminal symbols usd in h CFG corrspond as much as possibl o h symbols usd in h rrnc grammar). Submi a il calld dcagrammar.x which conains h CFG in h ollowing x orma: For h CFG: sar A sar B ɛ h x orma you should us is: sar A sar B sar Follow h convnion ha h non-rminals in his x orma ar wrin in h sam orma as idniirs in Dca bu ar in lowrcas (.g. sar, and or hyphnad non-rminals lik mhod-nam rplac h hyphn wih an undrscor,.g. mhod nam) and wri h rminal symbols in h sam orma as idniirs bu nirly in upprcas (.g. A). You should vriy h corrcnss o your CFG ihr by xamining i closly or you can vriy aspcs o h CFG by wriing som simpl cod or chcking whhr non-rminals ar usd in h righ-hand sid o ruls bu no dind on h l-hand sid anywhr ls in h CFG, whhr h rminal symbols ar valid okns, c. (5) Wri down a yacc parsr or h ollowing conx-r grammar: PLUS TIMES LPAREN RPAREN Th okns PLUS, TIMES, LPAREN, RPAREN ar dind o b +, *, (, ) rspcivly. And h okn is dind o b an idniir as in h Dca spciicaion. Ths okns should b dind using a lxical analyzr producd using lx. 3
For h inpu sring x + y * ( z ) h oupu producd by h yacc parsr should b h pars r or h inpu sring producd in h orma shown in h l column blow. No h backslash prcding ach insanc o a liral parnhsis o avoid conusion wih h parnhss usd o dno h r srucur. No ha you may nd o augmn h grammar o produc h righ oupu. Do no bohr o indn your r, jus prin ou your pars r in a singl lin o oupu x. A graphical viw, using h Tcl/Tk program viwr, is shown in h righ column blow: ( ( ( ( ( x)))) (PLUS +) ( ( ( ( y))) (TIMES *) ( (LPAREN \() ( ( ( ( z)))) (RPAREN \))))) PLUS + TIMES * LPAREN RPAREN \( \) x y z (6) Grammar Convrsion: Considr h ollowing ragmn o a Dca program: class oo { in bar No ha w could coninu h abov ragmn wih a ild dclaraion, or a mhod dclaraion. This issu will no b a problm or a LR parsr i h CFG or Dca can b wrin as an LR grammar. Convr h ollowing CFG, which rprsns a small ragmn o Dca synax, ino an LR grammar. As a rsul, h LR parsing abl or such a grammar will hav no shi/rduc or rduc/rduc conlics. program CLASS LCB ild dcl lis mhod dcl lis RCB ild dcl lis ild dcl ild dcl lis ild dcl lis ɛ mhod dcl lis mhod dcl mhod dcl lis mhod dcl lis ɛ ild dcl yp ASSIGN INTCONSTANT SEMICOLON mhod dcl rurn yp LPAREN RPAREN rurn yp yp rurn yp VO yp INT yp BOOL 4
You can s i your rvisd grammar is an LR grammar by wriing down h grammar as yacc cod wih no acions and simply chck i you g any shi/rduc conlics. No ha in his sklon yacc cod, h okn diniions do no nd o b linkd wih a lxical analyzr in ordr o chck or shi/rduc conlics. (7) Dca Pars Trs: Wri a yacc program ha prins ou a pars r or any inpu Dca program. You will nd o mak sur ha h conx-r grammar you us is an LR grammar. Th pars r orma should b h sam as in Q. (5). Us h lxical analyzr you hav alrady buil basd on h Dca spciicaion. 5