Вы находитесь на странице: 1из 14

Compiler Design Lab Session Plan

Class: III/II CSE-A Page No S.No Part-B: 1 Name of the program Design a lexical analyzer for given lang age !"#e lexical analyzer s#o l$ ignore re$ n$an" spaces% "abs an$ ne& lines! Implemen" "#e lexical analyzer sing )Lex% flex or o"#er lexical analyzer genera"ing "ools! a! Design Pre$ic"ive Parser of "#e given lang age Design a SL- bo""om p parser for "#e given lang age Conver" "#e B/0 r les in"o 1acc form an$ &ri"e co$e "o genera"e abs"rac" syn"ax "ree! A program "o genera"e mac#ine co$e Date Planned Date Conducted

'

' , . *

+ 1( 12 13

Class: III/II CSE-B

PART B COMP !"R D"S #N

Page No S.No Part-B: 1 ( Name of the program Design a lexical analyzer for given lang age !"#e lexical analyzer s#o l$ ignore re$ n$an" spaces% "abs an$ ne& lines! Implemen" "#e lexical analyzer sing )Lex% flex or o"#er lexical analyzer genera"ing "ools! Design Pre$ic"ive Parser of "#e R"C&RS )" D"SC"NT PARS"R for "#e given lang age Design a SL- bo""om p parser for "#e given lang age Conver" "#e B/0 r les in"o 1acc form an$ &ri"e co$e "o genera"e abs"rac" syn"ax "ree! A program "o genera"e mac#ine co$e ' *

Date Planned

Date Conduct ed

'

, . *

1( 12 13

S$ST"M R"%& R"M"NTS '. (. In"el base$ $es4"op PC of 1**56z or fas"er processor &i"# a" leas" *, 5B -A5 an$ 177 5B free $is4 space! C compiler

!AB OB*"CT )"S 1! (! 8o provi$e an 9n$ers"an$ing of "#e lang age "ransla"ion pec liari"ies "ransla"or for mini lang age! 8o provi$e an n$ers"an$ing of "#e $esign aspec" of opera"ing sys"em by $esigning comple"e

S$!!AB&S 1! Design a Lexical analyzer for "#e above lang age! 8#e lexical analyzer s#o l$ ignore re$ n$an" spaces% "abs an$ ne&lines! I" s#o l$ also ignore commen"s! Al"#o g# "#e syn"ax specifica"ion s"a"es "#a" i$en"ifiers can be arbi"rarily long% yo may res"ric" "#e leng"# "o some reasonable val e! Implemen" "#e lexical analyzer sing flex% flex or lex or o"#er lexical analyzer genera"ing "ools! Design Pre$ic"ive parser for "#e given lang age Design SL- bo""om p parser for "#e above lang age! Conver" "#e B/0 r les in"o 1acc form an$ &ri"e co$e "o genera"e abs"rac" syn"ax "ree! :ri"e program "o genera"e mac#ine co$e from "#e abs"rac" syn"ax "ree genera"e$ by "#e parser! follo&ing ins"r c"ion se" may be consi$ere$ as

(! '! ,! .! *!

"+P"R M"NT : ' NAM" O, T-" "+P"R M"NT; Des.gn A !e/.cal Anal01er AM

'

Design a Lexical analyzer! 8#e lexical analyzer s#o l$ ignore re$ n$an" s "abs an$ ne& lines! I" s#o l$ also ignore commen"s! Al"#o g# "#e syn"ax specifica"ion s "#ose i$en"ifiers can be arbi"rarily long% yo may res"ric" "#e leng"# "o some reasonable <al e! T-"OR$ Lexical analyzer or scanner is "#e process &#ere "#e s"ream of c#arac"ers ma4ing p "#e so rce program is rea$ from lef"-"o-rig#" an$ gro pe$ in"o "o4ens! Tokens are se= ences of c#arac"ers &i"# a collec"ive meaning! 8#ere are s ally only a small n mber of "o4ens for a programming lang age; cons"an"s >in"eger% $o ble% c#ar% s"ring% e"c!?% opera"ors >ari"#me"ic% rela"ional% logical?% p nc" a"ion% an$ reserve$ &or$s! A!#OR T-M step'; step(: step2; step3; step4; step5; step6; step7; step8; S"ar" Declare "#e variable c#ar exp@(7A%i$@17A%$ig@17A%c# % in" i%BC En"er "#e Den"er expression;DC -ea$ "#e expression Ini"ialize iE7 -epea" s"ep . n"il iFs"rlen>exp?C p"o s"ep (. Assign c#Eexp@iAC Declare BE7C -epea" s"ep + >c#GEHaHIIc#FEHzH? p"o s"ep1,

step'9: Assign i$@BJJAEc#C step''; Incremen" i EiJ1 step'(; -epea" s"ep 11 &#ile>>exp@iAGEHaHIIexp@iAFEHzH?KK>exp@iAGEH7HIIexp@iAFEH3H?? p"o s"ep1( step'2; Assign i$@BJJAEexp@iJJA step'3; Declare i$@BAEHL7H step'4; Display D i$en"ifierD step'5; M"#er&ise repea" s"ep 1. if>c#EEHJHKKc#EEH-HKKc#EEHNHKKc#EEH/HKKc#EEHOHKKc#EEHEH? p"o s"ep12 step'6; Display Dopera"orD step'7; Incremen" iEiJ1 step'8; M"#er&ise repea" s"ep 1+ if>c#GEH7HIIc#FEH3H? p"o s"ep(, step(9; Assign $ig@BJJAEc# step('; Incremen" iEiJ1 step((; -epea" &#ile>exp@iAGEH7HIIexp@iAFEH3H? step(2; Assign $ig@BJJAEexp@iJJAC step(3; Declare $ig@BAEHL7HC step(4; Display Dcons"an"D step(5; S"op

O&TP&T

) )A %&"ST ONS 1! (! '! ,! .! *! :#a" is lexical analyzerP :#ic# compiler is se$ for lexical analyzerP :#a" is pa""ernP :#a" is "#e o "p " of Lexical analyzerP :#a" is "#e lexeme P &#a" is "#e "o4en P

"+P"R M"NT : ( NAM" O, T-" "+P"R M"NT: Implemen" lexical analyzer sing lexical analyzer genera"ing "ools!

A M: Implemen" "#e lexical analyzer sing )Lex% flex or lex o"#er lexical analyzer genera"ing "ools! T-"OR$ Lex "ool se$ "o genera"e lexical analyzers an$ parsers! 8#e Mvervie& $escribes "#e basic b il$ing bloc4s of a compiler an$ explains "#e in"erac"ion &i"# lex! Conven"ional ari"#me"ic opera"ions an$ con"rol s"a"emen"s% s c# as if-else an$ while% are implemen"e$! A!#OR T-M: S"ep1; S"ep(; S"ar" Declare "#e $eclara"ions for "#e given lang age "o4ens li4e $igi"% alp#abe"% &#i"e space% $elimi"ers% e"c! $igi"@7-3A le""er@A-Q a-zA $elim@L"LnA :RS$elimTJIDS>le""er?>le""er/$igi"?TJ In"eger S$igi"TJ OO S&sT Sprin" >USpecialC#arac"ersV?T SIDT Sprin">UI$en"ifiersV?T S$igi"T Sprin">ULn In"eger?T if Sprin"f>U4ey&or$V?T else Sprin">4ey&or$?T UI IV Sprin">logoical opera"ors?T UGVSprin">logoical opera"ors?T UFVSprin">logoical opera"ors?T UFEVSprin">logoical opera"ors?T UGEVSprin">logoical opera"ors?T UEV Sprin"f>ULn LnV?T UWVSprin"f>ULn LnV?T UJV Sprin"f>Uari"#me"ic opera"orV?T U-U Sprin"f >Uari"#me"icV? UNV Sprin"f>Uari"#me"icV?T UOV Sprin"f>ari"#me"icV?T O O Sprin"f>Uari"#me"icV?T :ri"e "#e a xillary proce$ re in main>? f nc"ion en$ S"op

S"ep'; S"ep,; S"ep.;

O&TP&T @roo"Xlocal#os"AY lex lexprog!l @roo"Xlocal#os"AY cc lex!yy!c @roo"Xlocal#os"AY !/a!o " lexprog

) )A %&"ST ONS 1! (! '! ,! .! *! :#a" is ParsingP :#a" is 8o4enP 6o& many sec"ion in LEZ programP :#a" is mean" by ex"ension of $o" l>!l? in LEZP :#a" is "#e syn"ax of LEZ compiler program "o save i"P &#a" is "#e o "p " comman$ of LEZP

"+P"R M"NT: 2 NAM" O, T-" "+P"R M"NT : Pre$ic"ive Parser of "#e R"C&RS )" D"SC"NT PARS"R AM : Design Pre$ic"ive Parser of "#e R"C&RS )" D"SC"NT PARS"R given lang age

T-"OR$ -ec rsive $escen" parsing is probably "#e mos" &ell-4no&n an$ in" i"ive "ec#ni= e applicable "o a s bclass of con"ex"free grammars! A s bro "ine for eac# non-"erminal s#o l$ $e"ermine a r le accor$ing "o &#ic# "#e s bs"ring s#all be parse$! A!#OR T-M: Step': Step(: Step2: Step3: Step4: Step5: Step6: Step7: Step8: Step'9: O&TP&T S"ar" $eclare &@17A as c#ar an$ Q as an array en"er "#e s"ring &i"# R a" "#e en$ if >A>&@zA? "#en incremen" z an$ c#ec4 for >B>&@zA?? an$ if sa"isfies incremen" z an$ c#ec4 for [$\ if $ is presen" "#en incremen" an$ c#ec4 for >D>&@zA?? if s"ep , is sa"isfie$ "#en "#e s"ring is accep"e$ Else s"ring is no" give for "#e grammar A bc/ab in "#e loop A>in" 4? Describe "#e grammar b c/$ in "#e loop B >in" 4? Similarly $escribe "#e grammar D $/abc$ if s"eps2%+%3 are sa"isfie$ accor$ingly s"ring is accep"e$ Else s"ring is no" accep"e$ S"op

) )A %&"ST ONS 1! (! '! ,! .! *! :#a" is Pre$ic"ive parserP 6o& many "ypes of analysis can &e $o sing ParserP :#a" is -ec rsive Decen" ParserP :#a" is L- ParserP :#a" is bac4 8rac4ingP :#a" are "#e problem in 8op-Do&n parsingP

"+P"R M"NT: 3 NAM" O, T-" "+P"R M"NT: Design of SLA M: Design SL- bo""om p parser for "#e above lang age

T-"OR$ 8#e easies" "ec#ni= e for genera"ing L--base$ parse "able is 4no&n as SL- >Simple L-?!9n$ers"an$ing "#is "ec#ni= e s#o l$ provi$e an$ &#a" yo nee$ "o 4no& "o n$ers"an$ #o& L- parsers &or4 in general! -emember "#a" "#e i$ea be#in$ L- parsing is "o pro$ ce a D0A "#a" $efines "#e #an$les >s"ring of "erminals an$ non"erminals "#a" in$ica"e a re$ c"ion? of "#e inp " lang age! 8#e SL- "ec#ni= e is base$ on genera"ing se"s of L->7? i"ems "#a" $escribe "#e s"a"es of "#e D0A% as &ell as a "ransi"ion f nc"ion "#a" maps be"&een "#ese s"a"es! Defn; An !R:9; i"em of a grammar ] is a pro$ c"ion of ] &i"# a $o" >.? a" some poin" on "#e rig#" si$e! Defn: Closure: ; &#ere I is a se" of L->7? i"ems consis"s of Every i"em in I If A -G ^ . B _ in clos re>I?% for all pro$ c"ions B -G ` % a$$ B -< . ` "o clos re>I? if no" alrea$y "#ere! aeep applying "#is r le n"il no more i"ems can be a$$e$! Ini"ial elemen"s >I? are of"en referre$ "o as "#e kernel elemen"s of clos re>I?!

Defn: #oto: =+;= &#ere I is a se" of i"ems% Z is a "erminal or non-"erminal% is "#e clos re>A a Z . b? &#ere A a . Z b is in I! Algor.thm: b C ESclos re>SS\. ST?T% &#ere S\ S is "#e pro$ c"ion a$$e$ for a gmen"a"ion b -epea" c 0or eac# i"em I in C an$ grammar symbol Z s c# "#a" ]o"o>I%Z? is no" emp"y an$ no" in elemen" of C b A$$ ]o"o>I%Z? "o C A!#OR T-M S"ep1; S"ep(; S"ep'; S"ep,; S"ep.; S"ep*; S"ep2; S"ep+; S"ep3; S"ar" Ini"ially "#e parser #as s7 on "#e s"ac4 &#ere s7 is "#e ini"ial s"a"e an$ &R is in b ffer Se" ip poin" "o "#e firs" symbol of &R repea" forever% begin Le" S be "#e s"a"e on "op of "#e s"ac4 an$ a symbol poin"e$ "o by ip If ac"ion @S% aA Es#if" S "#en begin P s# S1 on "o "#e "op of "#e s"ac4 A$vance ip "o nex" inp " symbol Else if ac"ion @S% aA% re$ ce A-GB "#en begin Pop (N KBK symbols of "#e s"ac4 Le" S1 be "#e s"a"e no& on "#e "op of "#e s"ac4 M "p " "#e pro$ c"ion AB En$ else if ac"ion @S% aAEaccep"e$% "#en re" rn Else Error>? En$ S"op

alrea$y

an

S"ep17;

17

O&TP&T I$N>i$Ji$?R ]rammer accep"e$

) )A %&"ST ONS 1! (! '! ,! .! :#a" is SL- parsingP :#a" is S#if" re$ ce$ parserP :#a" are "#e opera"ions of ParserP :#a" is "#e se of parsing "ableP :#a" is bo""om p parsingP

11

"+P"R M"NT: 4 NAM" O, T-" "+P"R M"NT: Conver" "#e B/0 r les in"o 1ACC A M: Conver" "#e B/0 r les in"o 1ACC form an$ &ri"e co$e "o genera"e abs"rac" syn"ax "ree

T-"OR$ 1acc is a "ool for b il$ing syn"ax analyzers% also 4no&n as parsers % yacc #as been se$ "o implemen" # n$re$s of lang ages! I"s applica"ions range from small $es4 calc la"ors% "o me$i m-size$ preprocessors for "ypese""ing% "o large compiler fron" en$s for comple"e programming lang ages! A yacc specifica"ion is base$ on a collec"ion of grammar r les "#a" $escribe "#e syn"ax of a lang ageC yacc " rns "#e specifica"ion in"o a syn"ax analyzer! A p re syn"ax analyzer merely c#ec4s &#e"#er or no" an inp " s"ring conforms "o "#e syn"ax of "#e lang age! A!#OR T-M: S"ep1; S"ar" S"ep(; $eclare "#e $eclara"ions as a #ea$er file Sincl $eFc"ype!#GT S"ep'; "o4en $igi" S"ep,; $efine "#e "ransla"ions r les li4e line% expr% "erm% fac"or Line;exp [Ln\ Sprin">ULn O$ LnV%R1?T Expr;expr\J\ "erm >RRER1ER'T 8erm;"erm [J\ fac"or>RR ER1NR'T 0ac"or 0ac"or;\>[en"er\? [SRR ER(? OO S"ep.; $efine "#e s ppor"ing C ro "ines S"ep*; S"op O&TP&T: >root?localhost@A le/ codegen.l >root?localhost@A 0acc Bd codegen.0 >root?localhost@A cc le/.00.c 0.taC.c Bll -lm >root?localhost@A .Da.out

NP&T (J' ,-(

O&TP&T . (

) )A %&"ST ONS 1! (! '! ,! .! :#a" is Abs"rac" Syn"ax "reeP :#a" are B/0 - lesP :#a" is DA] represen"a"ionP 6o& LAL->1? s"a"es are genera"esP In &#ic# con$i"ion "#e ser #as "o s pply more informa"ion "o 1ACCP

1(

"+P"R M"NT : 5 NAM" O, T-" "+P"R M"NT: 8o genera"e mac#ine co$e A M: :ri"e program "o genera"e mac#ine co$e from "#e abs"rac" syn"ax "ree genera"e$ by "#e Parser !8#e follo&ing ins"r c"ion se" may consi$ere$ as "arge" co$e! T-"OR$ 5ac#ine co$e% also 4no&n as mac#ine lang age% is "#e elemen"al lang age of comp "ers% comprising a long se= ence of binary $igi"al zeros an$ ones A!#OR T-M S"ep1; S"ar" S"ep(; for every "#ree ca$$ress s"a"emen" of "#e form xEy op z S"ep'; begin S"ep,; Call ge"reg>? "o ob"ain "#e loca"ion L &#ic# "#e comp "a"ion y op z s#o l$ be performe$ S"ep.; Mb"ain "#e c rren" loca"ion of "#e operan$ y by cons l"ing i"s a$$ress $escrip"or %an$ if "#e val es of 1 are c rren"ly bo"# in "#e memory loca"ion as &ell as in "#e regis"er% "#en prefer "#e regis"er!If "#e val e of y is no" c rren"ly available in 1%"#en genera"e an ins"r c"ion 5M< y%l S"ep*; ]enera"e "#e ins"r c"ion MP Q%l an$ p$a"e "#e a$$ress $escrip"or of Z "o in$ica"e "#a" Z is no& available in l an$ in regis"er "#en p$a"e "L i"s $escrip"or "o in$ica"e "#a" i" &ill con"ain "#e r n "ime val e of x S"ep2; If "#e c rren" val es of y a$/or z are in regis"er an$ &e #ave no f r"#er se for "#em%an$ "#ey are live a" "#e en$ of "#e bloc4%"#en af"er "#e regis"er $escrip"or "o in$ica"e "#a" af"er "#e exec "ion of "#e s"a"em%en" xEy op z%"#ose regis"ers &ill no longer con"ain y an$ / or z! S"ep+; s"ore all res l"s S"ep3; S"op O&TP&T

) )A %&"ST ONS 1! :#a" is "arge" co$eP

1'

(! '! ,! .!

:#a" is mac#ine co$eP :#a" is Cross compilerP ]ive "#e example for cross compilerP :#a" is "#e $ifference be"&een syn"ax I Seman"icsP

1,

Вам также может понравиться