Sunteți pe pagina 1din 59

Pad.Dr.D.Y.

Patil Institute of Engineering and Technology, Department of Information Technology (LAB Manual) Class: TE (IT) (2008 Revised Syllabus)
Software Design ab
Staff In!"#arge

$rof% Ru&es# 'a#a(an

$ad% Dr%D%)%$atil Institute *f Te"#nology+ $i,&ri

Assignment 01
Aim: I,&le,entation of T-* $ass asse,bler wit# #y&ot#eti"al Instru"tion set Instru"tion set s#ould in"lude all ty&es of asse,bly language state,ents su"# as I,&erative+ De"larative and .sse,bler Dire"tive% -#ile designing stress s#ould be given on a) /ow effi"iently 'ne,oni" o&"ode table "ould be i,&le,ented so as to enable faster retrieval on o&!"ode% b) I,&le,entation of sy,bol table for faster retrieval% ( Con"e&ts in DS0 s#ould be a&&lied w#ile design) Objective: To learn t#e basi" translation &ro"ess of asse,bly language to ,a"#ine anguage%

Theory: . language translator bridges an e1e"ution ga& to ,a"#ine language of "o,&uter syste,% .n asse,bler is a language translator w#ose sour"e language is asse,bly language% anguage &ro"essing a"tivity "onsists of two &#ases+ .nalysis &#ase and synt#esis &#ase% .nalysis of sour"e &rogra, "onsists of t#ree "o,&onents+ e1i"al rules+ synta1 rules and se,anti" rules% e1i"al rules govern t#e for,ation of valid state,ents in sour"e language% Se,anti" rules asso"iate t#e for,ation ,eaning wit# valid state,ents of language% Synt#esis &#ase is "on"erned wit# "onstru"tion of target language state,ents+ w#i"# #ave t#e sa,e ,eaning as sour"e language state,ents% T#is "onsists of ,e,ory allo"ation and "ode generation% Data stru"tures

$ass I

$ass II

Inter,ediate "ode Data a""ess Control transfer *ver view of two &ass assembly

De&art,ent of Infor,ation Te"#nology

$ad% Dr%D%)%$atil Institute *f Te"#nology+ $i,&ri .nalysis of sour"e &rogra, state,ents ,ay not be i,,ediately followed by synt#esis of e2uivalent target state,ents% T#is is due to forward referen"es issue "on"erning ,e,ory re2uire,ents and organi3ation of anguage $ro"essor ( $)% 0orward referen"e of a &rogra, entity is a referen"e to t#e entity+ w#i"# &re"edes its definition in t#e &rogra,% -#ile &ro"essing a state,ent "ontaining a forward referen"e+ language &ro"essor does not &osses all relevant infor,ation "on"erning referen"ed entity% T#is "reates diffi"ulties in synt#esi3ing t#e e2uivalent target state,ents% T#is &roble, "an be solved by &ost&oning t#e generation of target "ode until ,ore infor,ation "on"erning t#e entity is available% T#is also redu"es ,e,ory re2uire,ents of $ and si,&lifies its organi3ation% T#is leads to ,ulti!&ass ,odel of language &ro"essing% Language Processor Pass: It is t#e &ro"essing of every state,ent in a sour"e &rogra, or its e2uivalent re&resentation to &erfor, language!&ro"essing fun"tion% Assem ly Language statements: T#ere are t#ree ty&es of state,ents I,&erative+ De"larative+ .sse,bly dire"tives% .n i,&erative state,ent indi"ates an a"tion to be &erfor,ed during t#e e1e"ution of asse,bled &rogra,% Ea"# i,&erative state,ent usually translates into one ,a"#ine instru"tion% De"larative state,ent e%g% DS reserves areas of ,e,ory and asso"iates na,es wit# t#e,% DC "onstru"ts ,e,ory word "ontaining "onstants% .sse,bler dire"tives instru"t t#e asse,bler to &erfor, "ertain a"tions during asse,bly of a &rogra,+ e%g% ST.RT4"onstant5 dire"tive indi"ates t#at t#e first word of t#e target &rogra, generated by asse,bler s#ould be &la"ed at ,e,ory word wit# address 4"onstant5 Function Of Analysis An !ynthesis "hase: Analysis Phase: Isolate t#e label o&eration "ode and o&erand fields of a state,ent% Enter t#e sy,bol found in label field (if any) and address of ne1t available ,a"#ine word into sy,bol table% 6alidate t#e ,ne,oni" o&eration "ode by loo7ing it u& in t#e ,ne,oni"s table% Deter,ine t#e ,a"#ine storage re2uire,ents of t#e state,ent by "onsidering t#e ,ne,oni" o&eration "ode and o&erand fields of t#e state,ent% Cal"ulate t#e address of t#e address of t#e first ,a"#ine word following t#e target "ode generated for t#is state,ent ( o"ation Counter $ro"essing) !ynthesis "hase: *btain t#e ,a"#ine o&eration "ode "orres&onding to t#e ,ne,oni" o&eration "ode by sear"#ing t#e ,ne,oni" table% *btain t#e address of t#e o&erand fro, t#e sy,bol table%

De&art,ent of Infor,ation Te"#nology

$ad% Dr%D%)%$atil Institute *f Te"#nology+ $i,&ri Synt#esi3e t#e ,a"#ine instru"tion or t#e ,a"#ine for, of t#e "onstant as t#e "ase ,ay be% #esign of a T$o "ass Assembler: Tas7s &erfor,ed by t#e &asses of two!&ass asse,bler are as follows: Pass I: Se&arate t#e sy,bol+ ,ne,oni" o&"ode and o&erand fields% Deter,ine t#e storage!re2uired foe every asse,bly language state,ent and u&date t#e lo"ation "ounter% 8uild t#e sy,bol table and t#e literal table% Constru"t t#e inter,ediate "ode for every asse,bly language state,ent% Pass II: Synt#esi3e t#e target "ode by &ro"essing t#e inter,ediate "ode generated during Data structures re!uired for pass I: 9% Sour"e file "ontaining asse,bly &rogra,% 2% '*T: . table of ,ne,oni" o&!"odes and related infor,ation% It #as t#e following fields Mnemonic : Su"# as .DD+ E:D+ DC T%"& : IS for i,&erative+ D for de"larative and .D for .sse,bler dire"tive O"- co e : *&eration "ode indi"ating t#e o&eration to be &erfor,ed% Length : engt# of instru"tion re2uired for o"ation Counter $ro"essing /as# table I,&le,entation of '*T to ,ini,i3e t#e sear"# ti,e re2uired for sear"#ing t#e instru"tion% 'n e( 0 9 2 = ? > < ; Mnemonic .DD 8C C*'$ DI6 E@A DC DS E:D T%"& IS IS IS IS .D D D .D O"-)o e 09 0; 0< 08 0= 09 02 0> Length 09 09 09 09 ! ! ! ! Lin* !9 !9 !9 > ; < !9 !9

/as# 0un"tion used is .SCII 6alue of t#e 0irst letter of 'ne,oni" B <>% T#is #el&s in retrieving t#e o&! "ode and ot#er related infor,ation in ,ini,u, ti,e% 0or E1a,&le t#e instru"tion starting wit# al&#abet C.D will be found at inde1 lo"ation 0+ C8D at inde1 9+ so on and so fort#% If ,ore instru"tions e1ist wit# sa,e al&#abet t#en t#e instru"tion is De&art,ent of Infor,ation Te"#nology

$ad% Dr%D%)%$atil Institute *f Te"#nology+ $i,&ri stored at e,&ty lo"ation and t#e inde1 of t#at instru"tion is stored in t#e lin7 field% T#us instru"tions starting wit# al&#abet CDD will be stored at inde1 lo"ations =+>+and <% T#ose starting wit# E will be stored at ? and ; and t#e &ro"ess "ontinues% 9% S)'T8: T#e sy,bol table% 0ields are Sy,bol na,e+ .ddress ( C 6alue)% Initiali3e all values in t#e address fields to !9 and w#en sy,bol gets added w#en it a&&ears in label field re&la"e address value wit# "urrent C% T#e sy,bol if it used but not defined will #ave address value !9 w#i"# will be used for error dete"tion% !ymbol A ress oo& 20? :e1t 29? ?% ITT.8: and $** T.8 : iteral table stores t#e literals used in t#e &rogra, and $** T.8 stores t#e &ointers to t#e literals in t#e "urrent literal &ool% Literal E C>D E C9D E C9D A ress

>% Inter,ediate for, used 6ariant 9 F 6ariant 2 Students are su&&osed to write t#e variant used by t#e,% Data "tructure used y Pass II: 9% *$T.8: . table of ,ne,oni" o&"odes and related infor,ation% 2% S)'T.8: T#e sy,bol table =% ITT.8: . table of literals used in t#e &rogra, ?% Inter,ediate "ode generated by $ass I >% *ut&ut file "ontaining Target "ode F error listing% .lgorit#, 9 *&en t#e sour"e file in in&ut ,ode% 2% if end of file of sour"e file go to ste& 8% =% Read t#e ne1t line of t#e sour"e &rogra, ?% Se&arate t#e line into words% T#ese words "ould be stored in array of strings% >% Sear"# for first word is ,ne,oni" o&"ode table+ if not &resent it is a label + add t#is as a sy,bol in sy,bol table wit# "urrent C% .nd t#en sear"# for se"ond word in ,ne,oni" o&"ode table% <% If instru"tion is found "ase 9 : i,&erative state,ent "ase 2: De"larative state,ent "ase =: .sse,bler Dire"tive Generate Inter,ediate "ode and write to Inter,ediate "ode file% ;% go to ste& 2% 8% Close sour"e file and o&en inter,ediate "ode file

De&art,ent of Infor,ation Te"#nology

$ad% Dr%D%)%$atil Institute *f Te"#nology+ $i,&ri H% If end of file ( Inter,ediate "ode)+ go to ste& 9= 90% Read ne1t line fro, inter,ediate "ode file% 99% -rite o&"ode+ register "ode+ and address of ,e,ory( to be fet"#ed fro, literal or sy,bol table de&ending on t#e "ase) onto target file% T#is is to be done only for I,&erative state,ent% 92 go to ste& H% 9=% Close all files% 9?% Dis&lay sy,bol table+ literal table and target file% I,&erative state,ent "ase : 9% If o&"ode 5E 9 II o&"ode 4E8 ( Instru"tion re2uires register o&erand) a% Set ty&e as IS+ get o&"ode+ get register "ode+ and ,a7e entry into sy,bol or literal table as t#e "ase ,ay be% In "ase of sy,bol+ used as o&erand+ C field is not 7nown so C "ould be !9% $erfor, C &ro"essing CJJ% A&dating of sy,bol table s#ould "onsider error #andling% 2% if o&"ode is 00 ( sto&) : Set all fields of Inter,ediate "all as 00% CJJ =% else register o&erand not re2uired ( Read and $rint) Sa,e as "ase 9+ only register "ode is not re2uired+ so set it to 3ero% /ere again u&date t#e sy,bol table% CJJ *n si,ilar lines we "an identify t#e "ases for de"larative and asse,bler dire"tive state,ents based on o&"ode%

Algorithm for first +ass of t$o +ass assembler

De&art,ent of Infor,ation Te"#nology

$ad% Dr%D%)%$atil Institute *f Te"#nology+ $i,&ri

Algorithm for "ass '' of '' +ass Assembler De&art,ent of Infor,ation Te"#nology

$ad% Dr%D%)%$atil Institute *f Te"#nology+ $i,&ri

De&art,ent of Infor,ation Te"#nology

$ad% Dr%D%)%$atil Institute *f Te"#nology+ $i,&ri List of hy+othetical instructions: 'nstruction *&"ode 00 09 02 0= 0? 0> 0< 0; 08 0H 90 Assembly ,ne,oni" ST*$ .DD SA8 'A T '*6ER '*6E' C*'$ 8C DI6 RE.D $RI:T ,emar*s

sto& e1e"ution first o&erand ,odified "ondition "ode set first o&erand ,odified "ondition "ode set first o&erand ,odified "ondition "ode set register ,e,ory ,e,ory register sets "ondition "ode bran"# on "ondition "ode analogous to SA8 first o&erand is not used% first o&erand is not used%

!am+le 'n+ut - Out+ut: !AM"L& '."/T F'L& '.T&,M&#'AT& )O#& ST.RT 202 '*6ER .REG+ ED> '*6E' .REG + . **$ '*6ER .REG+ . '*6ER CREG+ 8 .DD CREG+ E C9D '*6E' CREG+ 8 SA8 CREG+. 8C .:)+ :EKT T*RG .DD CREG+8 D 8C E **$ :EKT SA8 .REG+ E C9D 8C T+ 8.CL ST*$ *RIGI: 29H 'A T CREG+ 8 . DS 9 8.CL E@A **$ 8 DS 9 END

!AM"L& O/T"/T F'L& OF /sing 0ariant One 202) (IS+0?)(9)( +9) 20=) (IS+0>)(9)(S+9) 20?) (IS+0?)(9)(S+9) 20>) (IS+0?)(=)(S+=) 20<) (IS+09)(=)( +2) 20;) (IS+0>)(2)(S+=) 208) (IS+02)(=)(S+9) 20H) (IS+0;)(<)(S+?) 290) (.D+ 0?) 299) 292) (IS+09)(=)(S+2) 29=) (IS+0;)(2)(S+2) 29?) (IS+02)(9)( +=) 29>) (IS%0;)(9)(S+>) 29<) (IS+00) 29;) (.D+0=) 29H) (IS+0=)(=)(S+=) 220) (D +02) (C+9) 229) (.D+02) 229) (D +02) (C+9) 222) (AD,05)

De&art,ent of Infor,ation Te"#nology

$ad% Dr%D%)%$atil Institute *f Te"#nology+ $i,&ri S)'8* T.8 E Inde1 9 2 = ? > Sy,bol . **$ 8 :EKT 8.CL $** T.8 E ITMInd 09 0= .ddress 220 20? 229 29? 20? ITER. T.8 E Inde1 9 2 = ITER. > 9 9 .DDRESS 290 299 222

!AM"L& '."/T F'L& ST.RT 202 '*6ER .REG+ ED> '*6E' .REG + . **$ '*6ER .REG+ . '*6ER CREG+ 8 .DD CREG+ E C9D '*6E' CREG+ 8 SA8 CREG+ . 8C .:)+ :EKT T*RG .DD CREG+ 8 D 8C E **$ :EKT SA8 .REG+ E C9D 8C T+ 8.CL ST*$ *RIGI: 29H 'A T CREG+ 8 . DS 9 8.CL E@A **$ 8 DS 9 E:D

!AM"L& O/T"/T F'L& FO, TA,1&T )O#& 202) 0? 9 290 20=) 0> 9 220 20?) 0? 9 220 20>) 0? = 229 20<) 09 = 299 20;) 0> = 229 208) 0= = 220 20H) 0; < 29? 290) 00 0 00> 299) 00 0 009 292) 09 = 229 29=) 0; 2 20? 29?) 02 9 222 29>) 0; 9 220 29<) 00 0 000 29;) 29H) 0= = 229 220) 229) 229) 222) 00 0 009

Instructions to the "tudents: De&art,ent of Infor,ation Te"#nology

$ad% Dr%D%)%$atil Institute *f Te"#nology+ $i,&ri Students are su&&osed to write about t#e organi3ations of t#e different data stru"tures su"# as array+ lin7 list+ et"% .ssu,&tions and li,itations if any s#ould be "learly ,entioned% Students are su&&osed to "reate two out&ut files+ one wit#out errors I one wit# errors indi"ating t#e ty&e of error% .t least following errors ,ust be #andled% &rrors: #or$ard reference%"ym ol used ut not defined&: T#is error o""urs w#en so,e sy,bol is used but it is not defined into t#e &rogra,% Duplication of "ym ol: T#is error o""urs w#en so,e sy,bol is de"lared ,ore t#an on"e in t#e &rogra,% 'nemonic error: If there is invalid instruction then this error will occur. (egister error: If there is invalid register then this error will occur. )perand error: T#is error will o""ur w#en t#ere is an error in t#e o&erand field+ 'nstructions : Student ,ust use in"re,ental a&&roa"# at t#e ti,e of develo&,ent%i%e%"onsider si,&le in&ut first and t#en go for "o,&le1 in&ut% "latform : D*S ( language B C) !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ! #A*s E1&lain w#at is ,eant by &ass of an asse,bler% E1&lain t#e need for two &ass asse,bler% E1&lain ter,s su"# as 0orward Referen"e and ba"7ward referen"e% E1&lain various ty&es of errors t#at are #andled in two different &asses% E1&lain t#e need of Inter,ediate Code generation and t#e variants used% State various tables used and t#eir signifi"an"e in t#e design of two &ass .sse,bler% !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ! De&art,ent of Infor,ation Te"#nology 9% 2% =% ?% >% <%

$ad% Dr%D%)%$atil Institute *f Te"#nology+ $i,&ri

Assignment 02
Aim: I,&le,entation of 'a"ro $ro"essor% 0ollowing "ases to be "onsidered a) 'a"ro wit#out any &ara,eters b) 'a"ro wit# $ositional $ara,eters ") 'a"ro wit# Ley word &ara,eters d) 'a"ro wit# &ositional and 7eyword &ara,eters% ( Conditional e1&ansion + nested ,a"ro i,&le,entation not e1&e"ted)

Objective: 9% To understand ,a"ro fa"ility+ features and its use in asse,bly language &rogra,,ing% 2% To study #ow t#e ,a"ro definition is &ro"essed and #ow ,a"ro "all results in t#e e1&ansion of "ode% Theory : .n asse,bly language ,a"ro fa"ility is to e1tend t#e set of o&erations &rovided in an asse,bly language% In order t#at &rogra,,ers "an re&eat identi"al &arts of t#eir &rogra, ,a"ro fa"ility "an be used% T#is &er,its t#e &rogra,,er to define an abbreviation for a &art of &rogra, I use t#is abbreviation in t#e &rogra,% T#is abbreviation is treated as ,a"ro definition I saved by t#e ,a"ro &ro"essor% 0or all o""urren"es t#e abbreviation i%e% ,a"ro "all+ ,a"ro &ro"essor substitutes t#e definition% 'acro definition part: It "onsists of 9% 'a"ro $rototy&e State,ent ! t#is de"lares t#e na,e of ,a"ro I ty&es of &ara,eters% 2% 'odel state,ent ! It is state,ent fro, w#i"# asse,bly language state,ent is generated during ,a"ro e1&ansion% =% $re&ro"essor State,ent ! It is used to &erfor, au1iliary fun"tion during ,a"ro e1&ansion% 'acro +all , E-pansion: T#e o&eration defined by a ,a"ro "an be used by writing a ,a"ro na,e in t#e ,ne,oni" field and its o&erand in t#e o&erand field% .&&earan"e of t#e ,a"ro na,e in t#e ,ne,oni" field leads to a ,a"ro "all% 'a"ro "all re&la"es su"# state,ents by se2uen"e of state,ent "o,&rising t#e ,a"ro% T#is is 7nown as ,a"ro e1&ansion%

De&art,ent of Infor,ation Te"#nology

$ad% Dr%D%)%$atil Institute *f Te"#nology+ $i,&ri 'acro #acilities: 9% Ase of .I0 I .G* allows us alter t#e flow of "ontrol during e1&ansion% 2% oo&s "an be i,&le,ented using e1&ansion ti,e variables% Design Procedure: 9% Definition &ro"essing ! S"an all ,a"ro definitions and for ea"# ,a"ro definition enter t#e ,a"ro na,e in ,a"ro na,e table (':T)% Store entire ,a"ro definition in ,a"ro definition table ('DT) and add au1iliary infor,ation in ':T su"# as no of &ositional &ara,eters (N$$) no of 7ey word &ara,eters (NL$)+ ,a"ro definition table &osition ('DT$) et"% 2% 'a"ro e1&ansion ! E1a,ine all state,ent in asse,bly sour"e &rogra, to dete"t t#e ,a"ro "alls% 0or ea"# ,a"ro "all lo"ate t#e ,a"ro in ':T+ retrieve 'DT$+ establis# t#e "orres&onden"e between for,al I a"tual &ara,eters and e1&and t#e ,a"ro% #ata structures re3uire for macro efinition +rocessing 9% 'a"ro :a,e Table O':TP ! 0ields! :a,e of 'a"ro+ N&& (no of &ositional &ara,eters)+ N 7&( no of 7eyword &ara,eters)+ + 'DT$ ( 'a"ro Definition Table $ointer)+ Leyword $ara,eters Default Table $osition (L$DT$)+ 2% $ara,eter :a,e Table O$:T.8P ! 0ields ! $ara,eter :a,e =% Leyword &ara,eter Default Table OL$DT.8P ! 0ields ! $ara,eter :a,e+ Default value ?% 'a"ro Definition Table O'DTP ! 'odel State,ent are stored in t#e inter,ediate "ode fro, as: *&"ode+ *&erands% Algorithm for definition processing: 8efore &ro"essing any definition initiali3e L$DT.8M&tr+ 'DTM&tr to 0 and ':TM&tr to !9% T#ese table &ointers are "o,,on to all ,a"ro definitions% 0or each ,a"ro definition &erfor, t#e following ste&s% 9% Initiali3e $:T.8 B &tr to 0 I fields of ':T+ N &&+ N 7& to 0 and in"re,ent ':TM&tr by 9% 2% 0or ,a"ro &rototy&e state,ent fro, ':T entry% a% Entry na,e into na,e field% b% 0or ea"# &osition &ara,eter field i% Enter na,e in &ara,eter na,e table% ii% In"re,ent $:T.8 B &tr by 9% iii% In"re,ent N && by 9% "% L$DT$ L$DT.8 ! &tr d% 0or ea"# 7eyword &ara,eter i% Enter na,e I default value in L$DT.8% ii% In"re,ent L$T.8 B&tr by 9%

De&art,ent of Infor,ation Te"#nology

$ad% Dr%D%)%$atil Institute *f Te"#nology+ $i,&ri iii% Enter na,e in $:T.8 I in"re,ent $:T.8 B &tr by 9% iv% In"re,ent N 7& by 9% e. MDTP MDT ptr ( c rrent MDT Ptr) =% Read ne1t state,ent a% 'odel state,ent i% 0or &ara,eter generate s&e"ifi"ation (&+ N n)% ii% Re"ord inter,ediate "ode in 'DT% iii% In"re,ent 'DT ! &tr by 9% end b% If 'E:D state,ent 8egin Enter 'E:D in 'DT+ in"re,ent 'DTM&tr by 9% If N7& EE 0 t#en L$DT$ E 0 Return to ,ain logi" ie ste& < of ,ain logi"% #ata structures re3uire for e(+ansion +rocessing :9% ."tual &ara,eter table: .$T.8 2% 'a"ro e1&ansion "ounter : 'EC% Algorithm for macro e(+ansion: 9% Initiali3ation i% 'EC 'DT$ fro, ':T% ii% Create .$T.8 wit# N && I N 7& entries and set .$T.8 &tr a""ordingly% iii% Co&y 7eyword &ara,eter defaults fro, L$DT.8 in .$T.8O&&P to .$T.8ON&& J N7& !9P% iv% $ro"ess a"tual &ositional &ara,eters in "all and "o&y t#e, in .$T.8 fro, 0 to N &&!9% v% 0or 7eyword &ara,eter s&e"ifi"ation sear"# na,e in &ara,eter na,e field of L$DT.8+ get ,at"#ing entry in 2 I enter value in .$T.8 O N&& J 2 B L$DT$ P% 2% -#ile State,ent &ointed by 'EC in 'DT is not 'E:D% i% If ,odel state,ent t#en re&la"e o&erands of t#e for, (&+ Nn) by values in .$T.8% ii% In"re,ent 'EC by one% iii% -rite t#e ,odel state,ent on e1&anded "ode file% =% E1it fro, ,a"ro e1&ansion 'ain $rogra, ogi" : 9% Initiali3e L$DT.8M&tr+ 'DTM&tr to 0 and ':TM&tr to !9% T#ese table &ointers are "o,,on to all ,a"ro definitions ( T#ere "ould be ,ore t#an one ,a"ro definition in &rogra,) 2% Read t#e state,ent fro, sour"e file+ one line at ti,e

De&art,ent of Infor,ation Te"#nology

$ad% Dr%D%)%$atil Institute *f Te"#nology+ $i,&ri =% Se&arate t#e words fro, t#at line and "ount t#e no of words% Save t#e se&arated words in t#e array say word w#i"# is a array of strings ?% If "ount for words in a line is one t#en "#e"7 if t#at only word ,at"#es wit# Q'.CR*R 7eyword+ if '.CR* 7eyword found t#en &erfor, definition &ro"essing% >% If it does not ,at"# t#en "#e"7 w#et#er first word of t#e line ,at"#es wit# any of t#e entries in t#e ,a"ro na,e table% (Sear"# t#e "o,&lete ,a"ro na,e table for &resen"e of ,a"ro "all)+ if so t#en &erfor, ,a"ro e1&ansion routine% <% If no 'a"ro "all or no definition t#en enter t#e line as it is in t#e e1&anded "ode file% ;% If not end of file go to ste& =% "ample Input: The assem ly language program $ith macro definitions , macro calls '.CR* '.C9 '*6ER .REG+ ' .DD 8REG+ ' '*6E' CREG+ ' 'E:D '.CR* E6. IK+I)+IS '*6ER .REG+ IK SA8 .REG+ I) .DD .REG+ IS '*6ER .REG+ IS 'E:D '.CR* C. C IK+I)+I*$E'A T+I .8E I .8 '*6ER .REG+ IK I*$ .REG+ I) '*6E' .REG+ IK 'E:D ST.RT '*6E' .REG+ 8 E6. .+ 8+ C .DD .REG+ : '*6E' .REG+ : C. C $+ @+ .8E **$: '*6E' .REG+ : '.C9 C. C $+@+*$EDI6+ .8E:EKT ' DS 9 . DS > De&art,ent of Infor,ation Te"#nology

$ad% Dr%D%)%$atil Institute *f Te"#nology+ $i,&ri 8 C : $ @ DS 9 DS 9 DS 9 DS 9 DS 9 E:D

!am+le out+ut: 'a"ro na,e Table :a,e '.C9 E6. C. C N&& 0 = 2 N7& 0 0 2 ,dt& 0 ? H 7&dt& 0 0 9

'.CR* DE0I:ITI*: T.8 E Inde1 0 9 2 = ? > < ; 8 H 90 99 92 State,ents of t#e ,a"ros '*6ER .REG+ ' .DD 8REG+ ' '*6E' CREG+ ' 'E:D '*6ER .REG+ ($+0) SA8 .REG+ ($+9) .DD .REG+ ($+2) '*6ER .REG+ ($+2) 'E:D ($+=) '*6ER .REG+ ($+0) ($+2) .REG+ ($+9) '*6E' .REG+ ($+0) 'E:D

E1&anded "ode wit# no ,a"ro definition I ,a"ro "alls%

ST.RT '*6E' .REG+ 8 J '*6ER .REG+ . FF e1&anded "ode of E6. JSA8 .REG+ 8 J.DD .REG+ C J'*6ER .REG+ C .DD .REG+ : '*6E' .REG+ : J **$ '*6ER .REG+ $ FF e1&anded "ode for C. C J'A T .REG+ @ De&art,ent of Infor,ation Te"#nology

$ad% Dr%D%)%$atil Institute *f Te"#nology+ $i,&ri J'*6E' .REG+ $ '*6E' .REG+: J'*6ER .REG+ ' FF e1&anded "ode for '.C9 J.DD 8REG+ ' J'*6E' CREG+ ' J :EKT '*6ER .REG+ $ FF e1&anded "ode for C. C JDI6 .REG+ @ J'*6E' .REG+ $ . DS > 8 DS 9 C DS 9 : DS 9 $ DS 9 @ DS 9 E:D Students are su&&osed to dis&lay $:T.8 and .$T.8 also% 'nstructions ,egar ing testing of the +rogram: 9% Students are e1&e"ted to ta7e ,ini,u, two ,a"ro definitions 2% $ositional I 7eywords &ara,eters are to be #andled% =% T#e "ode generated fro, ,a"ro e1&ansion s#ould be &re"eded wit# J sign% ?% Students s#ould write about error #andling% .t least following errors s#ould be #andled i) $ositional &ara,eters ,ust &re"ede t#e 7eyword &ara,eters ii) 'is,at"# in total no of &ara,eters in for,al and a"tual &ara,eters% >% Students s#ould write about error #andling% <% Students are su&&osed to write assu,&tions+ li,itations if any Future &nhancement 4 5 .OT Man atory6 T#e ,a"ro is defined wit#in t#e ,a"ro or t#e "all is wit#in a "all 4! 'a"ro definition wit#in ,a"ro 4! 'a"ro "all wit#in a ,a"ro% 4! $ro"essing of .I0 and .G* 'nstructions : T#e students s#ould use in"re,ental a&&roa"# of develo&,ent so t#at "onsider si,&le in&ut first and t#en go for i,&le,entation of "o,&le1 in&ut% "latform : D*S ( language B C)

De&art,ent of Infor,ation Te"#nology

$ad% Dr%D%)%$atil Institute *f Te"#nology+ $i,&ri ----------------------------------------------------------------------------------------------------------FA7! 9% Define t#e ter, ,a"ro% 2% Distinguis# between ,a"ro and a subroutine =% Define and Distinguis# between &ara,eters t#at "an be used in ,a"ros% ?% State various tables used in &ro"essing t#e ,a"ro% >% E1&lain t#e role of sta"7 in nested ,a"ros% !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !

De&art,ent of Infor,ation Te"#nology

$ad% Dr%D%)%$atil Institute *f Te"#nology+ $i,&ri

Assignment 08
Aim : Regular E1&ression to D0. ( To be ta7en fro, "o,&iler &oint of view) T#e i,&le,entation to be done as &er t#e algorit#, "overed in t#e boo7 Q Co,&iler B Design and $rin"i&lesR 8y .#o B All,an Set#i%

Objective: ! To understand t#e role of regular e1&ressions and finite auto,ata in a&&li"ations su"# as Co,&ilers% Theory: Regular e1&ressions are used to s&e"ify regular languages and finite auto,ata are used to re"ogni3e t#e regular languages% 'any "o,&uter a&&li"ations su"# as "o,&ilers+ o&erating syste, utilities+ te1t editors ,a7e use of regular languages% In t#ese a&&li"ations+ t#e regular e1&ressions and finite auto,ata are used to re"ogni3e t#is language% Co,&iler is a &rogra, w#i"# "onverts t#e given sour"e &rogra, in #ig#!level language into an e2uivalent ,a"#ine language% -#ile doing so+ it dete"ts errors and re&orts errors% T#is &ro"ess is 2uite "o,&le1 and it is divided into nu,ber of &#ases su"# as e1i"al .nalysis+ Synta1 and Se,anti" .nalysis+ Inter,ediate Code generation+ Code Generation and Code *&ti,i3ation% T#e le1i"al analysis &#ase of "o,&iler reads t#e sour"e &rogra,+ "#ara"ter by "#ara"ter and t#en grou&s t#ese "#ara"ters into to7ens w#i"# are furt#er &assed to ne1t &#ase+ w#i"# is not#ing but &arsing or synta1 or se,anti" analysis% .fter synta1 and se,anti" analysis+ Inter,ediate Code is generated w#i"# is followed by a"tual "ode generation% e1i"al .naly3er re"ogni3es t#e to7ens fro, series of "#ara"ters% . QCR &rogra, "onsists of to7ens su"# as Identifiers+ Integers+ 0loating $oint :u,bers+ $un"tuation sy,bols+ relational and logi"al and arit#,eti" o&erators+ 7eywords and "o,,ents (to be re,oved)% To identify t#ese to7ens+ le1i"al analy3er needs t#e s&e"ifi"ation of ea"# of t#ese sy,bols% T#e set of words belonging to a &arti"ular to7en ty&e is a regular language% /en"e ea"# of t#ese to7en ty&es "an be s&e"ified using regular e1&ressions% 0or e1a,&le+ "onsider t#e to7en Identifier% In ,ost of t#e &rogra,,ing languages+ an identifier is a word w#i"# begins wit# an al&#abet ("a&ital or s,all) followed by 3ero or ,ore letters or digits (0%%H)% T#is "an be defined by t#e regular e1&ression (letter) % ( letter T digit)U w#ere letter E .T8TCTVVTST aT b T" TVVT3 and digit E 0T9T2TV%TH *ne "an s&e"ify all to7en ty&es using regular e1&ressions% T#ese regular e1&ressions are t#en "onverted to D0.Ds in t#e for, of D0. transition table% e1i"al analy3er reads a "#ara"ter fro, a sour"e &rogra, and based on t#e "urrent state and "urrent sy,bol read+ ,a7es a transition to so,e ot#er state% -#en it rea"#es a final state of D0.+ it grou&s t#e series of "#ara"ters so far read and out&uts t#e to7en found% De&art,ent of Infor,ation Te"#nology

$ad% Dr%D%)%$atil Institute *f Te"#nology+ $i,&ri Formal efinition of ,egular e(+ression T#e "lass of regular e1&ressions over W is defined re"ursively as follows: 9% T#e letters X and Y are regular e1&ressions over W % 2% Every letter CaD " Wis a regular e1&ression over W % = If CR9D and CR are regular e1&ressions over W+ t#en so are C(R9TR2)D+ C(R9%R2)D and (R9)U -#ere CTD indi"ates alternative or &arallel &at#s% C%D Indi"ates "on"atenation CUD indi"ates "losure ?% T#e regular e1&ressions are only t#ose t#at are obtained using rules (9) and (2)% Formal efinition of #FA: T#e for,al definition of finite auto,ata is denoted by a tu&le ( @+ W+d+ 2o+ f) -#ere @ 0inite set of table W finite in&ut al&#abet 2o Initial state of 0.+2o+ 2o @ 0 set of final states+ 0 " @ d Transition fun"tion "alled as state fun"tion ,a&&ing @UW@ i%e% dE @UW @ . 0. is "alled deter,inisti" (D0.) if fro, every verte1 of its transition gra&#+ t#ere is an uni2ue in&ut sy,bol w#i"# ta7es t#e verte1 state to t#e re2uired ne1t state% D0. is "onstru"ted dire"tly fro, an aug,ented regular e1&ression ( r )N% -e begin by "onstru"ting a synta1 tree T for ( r ) N and t#en we "o,&ute four fun"tions :ullable+ 0irst&os+ ast&os and 0ollow&os% T#e fun"tions :ullable+ 0irst&os+ ast&os are defined on t#e nodes of a synta1 tree and are used to "o,&ute 0ollow&os w#i"# is defined on set of &ositions% -e "an s#ort "ir"uit t#e "onstru"tion of :0. by building t#e D0. w#ose states "orres&ond to t#e sets of &ositions in t#e tree% $ositions+ in &arti"ular + en"ode t#e infor,ation regarding w#en one &osition "an follow anot#er% Ea"# sy,bol in an in&ut string to a D0. "an be ,at"#ed by "ertain &ositions% .n in&ut sy,bol C"D "an only be ,at"#ed by &ositions at w#i"# t#ere is a C"D but not every &osition wit# a C"D "an ne"essarily ,at"# a &arti"ular o""urren"es of C"D in in&ut strea,% Algorithm T#e ste&s in algorit#, are 9% .""e&t t#e given regular e1&ression wit# end of "#ara"ter as N 2% Covert t#e regular e1&ressions to its e2uivalent &ostfi1 for, ,anually% ( students need not write t#e "ode for "onverting infi1 to &ostfi1 but+ t#ey "an dire"tly a""e&t &ostfi1 for, of t#e infi1 e1&ression) =% Constru"t a synta1 tree fro, t#e &ostfi1 e1&ression obtained in ste& 2% ?% .ssign &ositions to leaf nodes >% Co,&ute following fun"tions% :ullable+ 0irst&os+ ast&os+ 0ollow&os De&art,ent of Infor,ation Te"#nology

$ad% Dr%D%)%$atil Institute *f Te"#nology+ $i,&ri

Co,&utation of :ullables : .ll nodes e1"e&t t#e U nodes are not nullable% .lso if so,e leaf node is for Z+ t#en it is also nullable% 0irst&os (0irst&osition): .t ea"# node n of t#e synta1 tree of a regular e1&ression+ we define a fun"tion first&os(n) t#at gives t#e set of first &ositions t#at "an ,at"# first sy,bol of a string generated by sub e1&ression rooted at CnD% ast&os (last&osition) : .t ea"# node n of t#e synta1 tree of a regular e1&ression+ we define a fun"tion last&os(n) t#at gives t#e set of last &ositions t#at "an ,at"# last sy,bol of a string generated by sub e1&ression rooted at CnD% To "o,&ute first&osition and last &osition+ we need to 7now w#i"# nodes are t#e roots of sub e1&ression t#at generate languages t#at in"lude t#e e,&ty string% Su"# nodes are :ullable% -e define nullable(n) to be true if node CnD is nullable + false ot#erwise% Co,&utation of 0ollow&os : 0ollow&os(i) tells us w#at &ositions "an follow &osition i in t#e synta1 tree% T#is "an be "o,&uted as follows% 9% if n is a C%D ("at) :ode+ wit# a left "#ild C9 and rig#t "#ild C2 and i is a &osition in t#e ast&os(C9)+ t#en all &ositions in 0irst&os(C2) are in 0ollow&os(i) 2% if n is a U ("losure) :ode and i is a &osition in t#e ast&os(n)+ t#en all &ositions in 0irst&os(n) are 0ollow&os(i) <% Constru"t D0. fro, 0ollow $os9 .ote : !te+ : can be one uring construction of tree; since you are buil ing the tree from bottom to to+; an $hen com+utations at some root of sub tree are to be one; information of sub tree is available9 !o no nee to o any traversal9 Data "tructures: :ode Stru"ture for $arse Tree [ eft"#ild and Rig#t"#ild : &ointers to t#e node stru"ture :ullable : 8oolean Ty&e Data : C#ara"ter Ty&e 0ist&os and ast&os : set of integers $os : integer (t#is ,ay or ,ay not be &art of tree node) \ Sta"7 : Sta"7 is re2uired to build t#e tree% T#is "an be i,&le,ented eit#er using lin7 list (&referable) or as an array% Ite, or data t#at will be &us#ed into or &o&&ed out fro, sta"7 is &ointer to t#e node stru"ture of a $arse Tree and not (ust a single "#ara"ter%%

De&art,ent of Infor,ation Te"#nology

$ad% Dr%D%)%$atil Institute *f Te"#nology+ $i,&ri

Co,&utations of 0irst&os and ast&os% :ode n : is a leaf labeled Z : T


T

:ullable(n) true :ullable("9) or :ullable ("2) false :ullable("9) an :ullable ("2)

0irst&os(n) < 0irst&os (C9) A 0irst&os(C2) [i\ If nullable (C9) t#en 0irst&os (C9) A 0irst&os(C2) else 0irst&os(C9) 0irst&os (C9)

ast&os(n) < ast&os (C9) A astt&os(C2) [i\ If nullable (C2) t#en ast&os (C9) A ast&os(C2) else ast&os(C2) ast&os (C9)

C9

C2

: is a leaf labeled wit# &osition i n


C9

C2

true

C9

Algorithm for construction of #FA transition table 2% Initially + t#e only un,ar7ed state in Dstates is first&os(root)+ w#ere root is t#e root of a synta1 tree% =% -#ile t#ere is an un,ar7ed state T in Dstates do 8egin 'ar7 T 0or ea"# in&ut sy,bol a do 8egin et A be t#e set of &ositions t#at are in 0ollow&os($) for so,e $ in T+ su"# t#at t#e sy,bol at &osition $ is a% If A is not e,&ty and is not in Dstates t#en add A as an un,ar7ed state to Dstates Dtran OT+aP E A End End

De&art,ent of Infor,ation Te"#nology

$ad% Dr%D%)%$atil Institute *f Te"#nology+ $i,&ri

"ample Input : Enter &ostfi1 for, of t#e regular e1&ression (r%N) and use dot o&erator for "on"atenation and CTD+ for *R and U for "losure% 0or E1a,&le (r% N) is : (aT b)U% a %b %b %N t#en e2uivalent $ostfi1 E1&ression is abTUa%b%b%N% "ample output : $ost *rder Traversal Traversal of t#e tree is abTUa%b%b%N% Students are E1&e"ted to Draw t#e $arse Tree and S#ow t#e 6alues 0irst&os+ ast&os and t#e values of $ositions for leaf nodes% :ode a b T (or) U ("losure) a % ("at) b % ("at) b % ("at) N % $osition 9 2 0 0 = 0 ? 0 > 0 < 0 :ullable 0 0 0 9 0 0 0 0 0 0 0 0 0irst&ositon [9\ [2\ [9+2\ [9+2\ [=\ [9+2++=\ [?\ [9+2++=\ [>\ [9+2++=\ [<\ [9+2++=\ ast&osition [9\ [2\ [9+2\ [9+2\ [=\ [=\ [?\ [?\ [>\ [>\ [<\ [<\

0ollow $ositions eaf :ode &osition 9 2 = ? > Sy,bol at t#at &osition a b a b b 0ollow $ositions [9+2+=\ [9+2+=\ [?\ [>\ [<\

De&art,ent of Infor,ation Te"#nology

$ad% Dr%D%)%$atil Institute *f Te"#nology+ $i,&ri

D0.: ( gra+hical out+ut is not e(+ecte at all) In&ut Sy,bol a b 8 . C D .

State . [9+2+=\ 8 8 (9+2+=+?\ C 8 [9+2+=+>\ D 8 [9+2+=+<\

b 0F9 start . a 8 b a a Instructions to the stu ents for testing: Test your &rogra, for following regular e1&ressions i) (aTb)U ii) (aUTbU)U iii) (aTb)U%a%b%b%(aTb)U C b D

"latform : D*S ( anguage B C) !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ! FA7s 9% Define regular e1&ression and D0.% 2% Give a&&li"ations w#ere regular e1&ressions "ould be used% =% E1&lain t#e signifi"an"e of first&osition+last&osition and follow&ositin in t#e algorit#,%

De&art,ent of Infor,ation Te"#nology

$ad% Dr%D%)%$atil Institute *f Te"#nology+ $i,&ri ?% .ny alternate ,et#od to "onvert RE to D0.] If yes "o,&are t#e ,et#ods fro, s&a"e and ti,e "o,&le1ity &oint of view% !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !

Assignment 0=
Aim : -rite a &rogra, in C to i,&le,ent e1i"al .naly3er for t#e subset of C% Objective: 9% To understand t#e basi" &rin"i&les in "o,&ilation% 2% To study le1i"al analysis &#ase of "o,&iler% Theory: Co,&iler ta7es in&ut as a sour"e &rogra, I &rodu"es out&ut as an e2uivalent se2uen"e of ,a"#ine instru"tions% T#is &ro"ess "onsists of two!ste& &ro"essing of sour"e &rogra,% 9% .nalysis of sour"e &rogra,% 2% Synt#esis of target &rogra,% Analysis ste+: It "onsists of t#ree sub ste&s 9% e1i"al .nalysis ! Deter,ine le1i"al "onstituents in sour"e &rogra,% 2% Synta1 .nalysis ! Deter,ine stru"ture of sour"e string% =% Se,anti" .nalysis ! Deter,ine ,eaning of sour"e string% "ynthesis "tep: It deals wit# ,e,ory allo"ation I "ode generation% T#e a"tions in analysis &#ase are uni2uely defined for a given language% 8ut synt#esis ste& "onsists of ,any a"tion instan"es w#ere a"tions de&end on t#e as&e"t "on"erning t#e e1e"ution environ,ent of t#e "o,&iler% e%g% ! *&erating syste, interfa"es target ,a"#ine features su"# as instru"tion set+ addressing ,odes+ et"% Le(ical analysis: T#e a"tion of s"anning t#e sour"e &rogra, into &ro&er synta"ti" "lasses is 7nown as le1i"al analysis%

De&art,ent of Infor,ation Te"#nology

$ad% Dr%D%)%$atil Institute *f Te"#nology+ $i,&ri Tas* of Le(ical Analysis: 9% To s"an t#e &rogra, into basi" ele,ents or to7ens of t#e language% 2% To build t#e Anifor, sy,bol table (table of to7ens)% =% To build t#e sy,bol I literal table% ?% To re,ove w#ite s&a"es I "o,,ents% >% To dete"t errors su"# as invalid identifier or "onstant% #ata structures > #atabases re3uire : 9% Sour"e &rogra, ! *riginal Sour"e &rogra,+ w#i"# is s"anned by "o,&iler as string of "#ara"ters% 2% Ter,inal Table ! . &er,anent database t#at #as entry for ea"# ter,inal sy,bols su"# as arit#,eti" o&erators+ 7eywords+ &un"tuation "#ara"ters su"# as C^D + C+Det" 0ields: :a,e of t#e sy,bol% =% iteral Table ! T#is table is "reated during le1i"al analysis so as to des"ribe all literals in t#e &rogra,% 0ields: :a,e of t#e literal% ?% Identifier Table ! Created during le1i"al analysis and des"ribes all identifiers in t#e &rogra,% 0ields: :a,e of t#e identifier% >% Anifor, Sy,bol Table ! Created during le1i"al analysis to re&resent t#e &rogra, as a string of to7ens+ rat#er t#an of individual "#ara"ters% Ea"# unifor, sy,bol "ontains t#e identifi"ation of t#e table to w#i"# it belongs%( ID: ! Identifier table+ IT ! iteral Table TR' ! Ter,inal Sy,bol Table)and inde1 wit#in t#at table% <% 8uffer ! *ne buffer or two buffer s"#e,es to load sour"e &rogra, &art by &art to redu"e dis7 iFo% ?ey$or s can be store either in the terminal table or in the *ey$or table % 8asi" ste&s in algorit#, 9% Initiali3e line no to 9% 2% Read t#e sour"e &rogra, line by line ( 8uffer is line) =% 0or ea"# line se&arate t#e to7ens su"# as i) identifierF fun"tion na,e F 7eywords : 0ollow t#e transition diagra, to dete"t t#is^ i%e% letter followed by letter or digit% Sear"# in 7eyword table for e1isten"e of 7eyword+ ot#erwise it is identifier or fun"tion na,e% ii) Integer Constant : Digit followed by digit iii) .ll ty&es of o&erators su"# as 5+ 5E+ JJ+ J+ ! et" iv) Re,ove "o,,ents of t#e ty&e FU UF v) Re,ove all w#ite s&a"es% ?% .ssign a line no and in"re,ent a line nu,ber% >% Re&eat ste&s 2!? till end of file%

De&art,ent of Infor,ation Te"#nology

$ad% Dr%D%)%$atil Institute *f Te"#nology+ $i,&ri

"ample Input: . &rogra, in C language ,ain() [ int i+ no+ su,+ ,a1 ^ su, E0^ ,a1 E !=2;<;^ for ( i E 0^ i4 90^ iJJ) [ s"anf(Q_dR+ Ino)^ su, E su, J no^ if ( ,a1 5 no) ,a1 E no^ \ &rintf(Qsu, E _d ,a1 E _d`nR+ su,+ ,a1)^ get"#()^ \ 0ery 'm+ortant : #o not have @inclu e statement as +art of ) +rogram over here9 Other$ise you have to o +re-+rocessor han ling9 "ample )utput: Ter,inal Table ( ) [ \ J ! E I + ^ 4 5 Q

De&art,ent of Infor,ation Te"#nology

$ad% Dr%D%)%$atil Institute *f Te"#nology+ $i,&ri JJ

Leyword Table for if int float Identifier Table identifier ,ain i no su, ,a1 s"anf &rintf get"# iteral Table iteral 0 !=2;<; 90 Q_dR Qsu, E _d ,a1 E _d`nR /niform !ymbol Table Ty&e ID: TR' TR' TR' Inde1 0 0 9 2 attribute :u,eri" "onstant :u,eri" "onstant :u,eri" "onstant String "onstant String "onstant .ttribute 0un"tion na,e

0un"tion na,e 0un"tion na,e 0un"tion na,e

,ain ( ) [

De&art,ent of Infor,ation Te"#nology

$ad% Dr%D%)%$atil Institute *f Te"#nology+ $i,&ri int i + no + su, + ,a1 ^ su, E 0 ^ ,a1 E !=2;<; ^ for ( i E 0 LE) ID: TR' ID: TR' ID: TR' ID: TR' ID: TR' IT TR' ID: TR' IT TR' LE) TR' ID: TR' IT 0 9 8 2 8 = 8 ? H = < 0 H ? < 9 H 0 0 I < 0

Instructions: 9% Students are su&&osed to draw t#e transition state diagra,s for identifying literals+ identifiers+ relational o&erators+ "o,,ents et"% 2% Students s#ould use a buffer to load a &art of sour"e &rogra, to avoid large dis7 iFo% =% Effi"ient sear"# algorit#,s s#ould be i,&le,ented for o&ti,u, ti,e "o,&le1ity% ?% Students s#ould #andle le1i"al errors su"# as invalid identifier or literal% >% .ny assu,&tions s#ould be "learly ,entioned% Referen"e: Syste,s $rogra,,ing by Donavan 'nstructions: T#e students s#ould "learly s#ow in t#eir &rogra, t#e &ro"essing of Co,,ent lines+ tabs+ w#ite s&a"es+ new line "#ara"ters et"% in le1i"al analysis &ro"ess% "latform : D*S ( anguage B C)

De&art,ent of Infor,ation Te"#nology

$ad% Dr%D%)%$atil Institute *f Te"#nology+ $i,&ri !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ! FA7As 9% -#at are t#e different &#ases of t#e "o,&iler] 2% -#at is t#e role of le1i"al analysis in t#e &ro"ess of "o,&ilation] =% -#at are t#e tablesF data stru"tures used as in&ut and out&ut of le1i"al analysis] ?% /ow 7eywords and identifiers are distinguis#ed and &ro"essed] >% -#at ty&es of errors are generated by le1i"al analysis &#ase of "o,&iler] !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !

Assignment 0:
Aim Objective: To study different ter,s of editors + ty&es of editors Design a s"reen editor% : Design and I,&le,ent S"reen editor in C anguage

Theory : -rite in brief: Role of Editor% Ty&es of Editor Ase t#e "on"e&t of buffer to i,&le,ent s"reen area% Students "an also use video ,e,ory "on"e&t for t#is assign,ent 'n+ut :

Out+ut 5 &(+ecte 6 : T#e above sour"e &rogra, is to be stored in a se&arate %C file and t#e e1&e"ted out&uts are: 'ove,ent of "ursor (u& + down + left +rig#t) 8uilding u& ,enu wit# :ew 0ile +0ile save+0ile o&en% $roviding Leyboard s#ort"uts Delete and ba"7s&a"e Algorithm : Students are e1&e"ted to draw a flow "#art or algorit#, of t#e &rogra, written a""ording to t#e assu,&tions and a&&lied logi"%

De&art,ent of Infor,ation Te"#nology

$ad% Dr%D%)%$atil Institute *f Te"#nology+ $i,&ri 'nstructions: T#e students are e1&e"ted to 7now t#e various ty&es and fun"tions of editors in detail% "latform : D*S ( anguage B C) !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !FA7s 9% -#i"# are different ty&es of s"reen editor 2% -#at do you ,ean by s"an "ode -----------------------------------------------------------------------------------------------------------

Assignment 0B
Aim: I,&le,entation of Cal"ulator using EK and ).CC% Objective: To study t#e &ro"ess of le1i"al analysis and &arsing% Theory: During t#e first &#ase t#e "o,&iler reads t#e in&ut and "onverts strings in t#e sour"e to to7ens% -it# regular e1&ressions we "an s&e"ify &atterns to le1 so it "an generate "ode t#at will allow it to s"an and ,at"# strings in t#e in&ut% Ea"# &attern s&e"ified in t#e in&ut to le1 #as an asso"iated a"tion% Ty&i"ally an a"tion returns a to7en t#at re&resents t#e ,at"#ed string for subse2uent use by t#e &arser% Initially we will si,&ly &rint t#e ,at"#ed string rat#er t#an return a to7en value% T#e following re&resents a si,&le &attern+ "o,&osed of a regular e1&ression+ t#at s"ans for identifiers% e1 will read t#is &attern and &rodu"e C "ode for a le1i"al analy3er t#at s"ans for identifiers% letter5letterC igit6D T#is &attern ,at"#es a string of "#ara"ters t#at begins wit# a single letter followed by 3ero or ,ore letters or digits% T#is e1a,&le ni"ely illustrates o&erations allowed in regular e1&ressions: 9 re&etition+ e1&ressed by t#e QDR o&erator alternation+ e1&ressed by t#e QCR o&erator Eistory of Le( - %acc e1 I )a"" were develo&ed at 8ell aboratories in t#e ;0Ds )a"" was develo&ed as t#e first of t#e two by Ste&#en C% ao#nson e1 was designed by 'i7e E% es7 and Eri" S"#,idt to wor7 wit# )a"" Standard A:IK utilities

De&art,ent of Infor,ation Te"#nology

$ad% Dr%D%)%$atil Institute *f Te"#nology+ $i,&ri e1 I )a"" $rogra,,ing Tools for writers of "o,&ilers and inter&reters .lso interesting for non!"o,&ilerwriters .ny a&&li"ation loo7ing for &atterns in its in&ut or #aving an in&utF"o,,and language is a"andiate for e1F)a"" le1 and ya"" #el& you write &rogra,s t#at transfor, stru"tured in&ut B le1 !! generates a le1i"al analy3er b divides a strea, of in&ut "#ara"ters into ,eaningful units (le1e,es)+ identifies t#e, (to7en) and ,ay &ass t#e to7en to a &arser generator+ ya"" b le1 s&e"ifi"ations are regular e1&ressions B ya"" !! generates a &arser b ,ay do synta1 "#e"7ing only or "reate an inter&reter b ya"" s&e"ifi"ations are gra,,ar "o,&onents Le(: T#e Ani1 &rogra, Qle1R is a Q e1i"al .naly3er GeneratorR B Ta7es a #ig#!level des"ri&tion of le1i"al to7ens and a"tions B Generates C subroutines t#at i,&le,ent t#e le1i"al analysis b T#e na,e of t#e resulting subroutine is Qyyle1R Generally+ yyle1 is lin7ed to ot#er routines+ su"# as t#e &arsing &ro"edures generated by ).CC %acc an Le(

OrganiFation of a Le( +rogram

De&art,ent of Infor,ation Te"#nology

$ad% Dr%D%)%$atil Institute *f Te"#nology+ $i,&ri 4de"larations5 __ 4translation rules5 __ 4au1iliary &ro"edures5 Translation rules "onsist of a se2uen"e of &atterns asso"iated wit# a"tions e1 reads t#e file and generates a s"anner B Re&eatedly lo"ates t#e Qlongest &refi1 of t#e in&ut t#at is ,at"#ed by one or ,ore of t#e &atternsR B -#en t#e a"tion is found+ le1 e1e"utes t#e asso"iated a"tion B In t#e "ase of a tie: b Ase w#i"#ever rege1& uses t#e ,ost "#ara"ters b If sa,e nu,ber of "#ara"ters+ t#e first rule wins ,egular &(+ressions in Le( Referen"es to a single "#ara"ter B 1 t#e "#ara"ter c1c B c1c an c1c+ even if 1 is an o&erator B `1 an c1c+ even if 1 is an o&erator B (1) an 1 B O1yP t#e "#ara"ter 1 or y B O1!3P t#e "#ara"ter 1+ y or 3 B Od1P any "#ara"ter e1"e&t 1 B % any "#ara"ter e1"e&t newline Re&etitions and o&tions B 1] an o&tional 1 B 1U 0+9+2+ %%% instan"es of 1 B 1J 9+2+=+ %%% instan"es of 1 %acc 'ntro uction )a"" is a t#eoreti"ally "o,&li"ated+ but QeasyR to use &rogra, t#at &arses in&ut files to verify t#at t#ey "orres&ond to a "ertain language )our ,ain &rogra, "alls yy&arse() to &arse t#e in&ut file T#e "o,&iled ).CC &rogra, auto,ati"ally "alls yyle1()+ w#i"# is in le1%yy%" )ou really need a 'a7efile to 7ee& it all straig#t )a"" ta7es a gra,,ar t#at you s&e"ify (in 8:0 for,) and &rodu"es a &arser t#at re"ogni3es valid senten"es in your language Can generate inter&reters+ also+ if you in"lude an a"tion for ea"# state,ent t#at is e1e"uted w#en t#e state,ent is re"ogni3ed ("o,&leted) T#e )a"" $arser $arser reads to7ens^ if to7en does not "o,&lete a rule it is &us#ed on a sta"7 and t#e &arser swit"#es to a new state refle"ting t#e to7en it (ust read -#en it finds all to7ens t#at "onstitute t#e rig#t #and side of a rule+ it &o&s of t#e rig#t

De&art,ent of Infor,ation Te"#nology

$ad% Dr%D%)%$atil Institute *f Te"#nology+ $i,&ri #and sy,bols fro, t#e sta"7 and &us#es t#e left #and sy,bol on t#e sta"7 ("alled a reduction) -#enever ya"" redu"es a rule+ it e1e"utes t#e user "ode asso"iated wit# t#e rule $arser is referred to as a shift/reduce parser ya"" "annot run alone !! it needs le1 OrganiFation of a %acc file Definition se"tion " B De"larations of to7ens used in gra,,ar+ t#e ty&es of values used on t#e &arser sta"7 and ot#er odds and ends B 0or e1a,&le+ _to7en $ AS+ 'I:AS+ TI'ES+ DI6IDE B De"laration of non!ter,inals+ _union+et" Rules se"tion B . list of gra,,ar rules in 8:0 for, B E1a,&le: B Ea"# rule ,ay or ,ay not #ave an asso"iated a"tion (a"tions Co,,uni"ation between e1 and )a"" -#enever e1 returns a to7en to t#e &arser+ t#at #as an asso"iated value+ t#e le1er ,ust store t#e value in t#e global variable yylval before it returns% T#e variable yylval is of t#e ty&e ))ST)$E^ t#is ty&e is defined in t#e file yy%tab%# ("reated by ya"" using t#e o&tion CBdD)% 8y default it is integer. If you want to #ave to7ens of ,ulti&le valued ty&es+ you #ave to list all t#e values using t#e %union de"laration %acc file for the calculator e(am+le 5calc9y6 _to7en :A'8ER+ $ AS+ 'I:AS+ TI'ES+ DI6IDE _left 'I:AS $ AS _left TI'ES DI6IDEe _nonasso" A'I:AS __ state,ent : e1&ression [&rintf(cE_d`nc+f9)^\ ^ e1&ression: e1&ression $ AS e1&ression [ff E f9 J f=^\ T e1&ression 'I:AS e1&ression [ff E f9 ! f=^\ T e1&ression TI'ES e1&ression [ff E f9 U f=^\ T e1&ression DI6IDE e1&ression [if (f= EE 0) yyerror(cdivide by 3eroc)^ else ff E f9 F f=^ \

De&art,ent of Infor,ation Te"#nology

$ad% Dr%D%)%$atil Institute *f Te"#nology+ $i,&ri T e!e e1&ression _&re" A'I:AS [ff E !f2^\ T e(e e1&ression e)e [ff E f2^\ T :A'8ER [ff E f9^\ ^ __ Eo$ it $or*s ya"" "reates a C file t#at re&resents t#e &arser for a gra,,ar ya"" re2uires in&ut fro, a le1i"al analy3er^ le1i"al analy3er no longer "alls yylex be"ause ya"" does t#at Ea"# to7en is &assed to ya"" as it is &rodu"ed and #andled by ya""^ ya"" defines t#e t#e to7en na,es in t#e &arser as C &re&ro"essor na,es in y.tab.h Overall structure of le( an %acc for +rogram e(ecution is sho$n belo$9

'n+ut: :u,bers Out+ut: Result of t#e o&eration &erfor,ed on o&erands% !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ! FA7s

De&art,ent of Infor,ation Te"#nology

$ad% Dr%D%)%$atil Institute *f Te"#nology+ $i,&ri 9% -#at is t#e role of le1 ] 2% -#at do you ,ean by ).CC ] =% /ow le1 and ya"" "o,,uni"ates ]

Assignment 0G
Aim: Objective: 19 To write an a&&li"ation in 6CJJ to "reate D % 29 To learn to use .&&-i3ard to "reate '0C!based D s 89 To understand t#e signifi"an"e of dyna,i" lin7ing over stati" lin7ing+ as lin7ing stati"ally is t#e ,ost e1&ensive ,et#od in ter,s of dis7 s&a"e and ,e,ory re2uire,ents% Theory: "roce ure: T#e first ste& is to start 6isual CJJ and "reate our D &ro(e"t% I,&le,entation of D %

16 !tart 0isual )HH an choose File C .e$ from the menu9 T#is will o&en t#e .e$ dialog% T#is is t#e starting &oint for "reating all 7inds of &ro(e"ts% 'a7e sure t#at you are on t#e c$ro(e"tsc tab% 26 !elect IJin82 #ynamic-Lin* LibraryI as the +roject ty+e an name the +roject IMa*eTem+Filename#LLI9 -e want to "reate a -in=2 D + not a '0C D % '0C!based D s "an also be very useful and offer a lot ,ore fun"tionality+ #owever t#ey re2uire t#e over#ead of t#e '0C library D s (over 9 '8) (ust to get started% T#at is too ,u"# over#ead for our si,&le D needs%

De&art,ent of Infor,ation Te"#nology

$ad% Dr%D%)%$atil Institute *f Te"#nology+ $i,&ri

So+ t#e :ew dialog s#ould now loo7 so,et#ing li7e t#e one above% Cli"7 O? to "ontinue to t#e ne1t ste&% 86 )hoose IA sim+le #LL +roject9I an clic* Finish9 T#e ne1t s"reen you will see loo7s li7e t#is:

0or t#is &ro(e"t+ we will (ust start wit# a si,&le D % .fter you "li"7 t#e Finish button+ you will see t#e .e$ "roject 'nformation dialog w#i"# su,,ari3es your

De&art,ent of Infor,ation Te"#nology

$ad% Dr%D%)%$atil Institute *f Te"#nology+ $i,&ri settings% Cli"7 O? to "o,&lete t#e &ro(e"t "reation% )our new &ro(e"t will now be ready to wor7 on fro, t#e 6isual CJJ design environ,ent% .t t#is &oint+ your &ro(e"t will "o,&ile and your D "an be "reated+ #owever it will not do anyt#ing be"ause we #ave not "reated and e1&orted any fun"tions yet% T#at is t#e ne1t ste&% A the Function t#at we want to be able to "all

T#e ne1t ste& is to a"tually add t#e fun"tion to t#e D fro, ot#er &rogra,s%

16 O+en the IMa*eTem+Filename#LL9c++I file for e iting9 Sele"t t#e File0ie$ tab on t#e -or7s&a"e bar% T#en o&en t#e !ource Files folder% :ow double!"li"7 on t#e c'a7eTe,&0ilena,eD %"&&c file% )ou will now be able to edit t#e file% 26 A the I,etrieveTem+FilenameI function9

:e1t we want to add t#e a"tual "ode for t#e fun"tion% *ur fun"tion will be "alled cRetrieveTe,&0ilena,ec% It will a""e&t two string argu,ents% T#e first is cs3Dire"toryc w#i"# is t#e dire"tory t#at we want to generate t#e uni2ue filena,e in% T#e se"ond argu,ent+ cs3$refi1c is a string t#at will be t#e &refi1 of t#e te,&orary filena,e% T#e cGetTe,&0ile:a,ec .$I fun"tion will use t#e first t#ree "#ara"ters of t#is string w#en generating t#e uni2ue filena,e% T#e fun"tion will return t#e uni2ue filena,e if su""essful+ or t#e string cERR*Rc if it fails% 8e"ause we want to return a string+ we need to ,a7e t#e returned string a global variable% T#is is ne"essary be"ause if we de"lare t#e string inside of t#e fun"tion it will be destroyed w#en t#e fun"tion is finis#ed% -e need to "reate a string t#at will last for t#e life of t#e D % )ou "an "o&y and &aste t#e "ode above rig#t into your file below t#e cDll'ainc fun"tion% If you build your &ro(e"t now it will "o,&ile su""essfully but t#e fun"tion will still not be "allable fro, outside &rogra,s% T#at is be"ause t#e 6isual CJJ "o,&iler cde"oratesc t#e fun"tion na,es so t#at t#ey would not be re"ogni3able to t#e "alling &rogra,% .lt#oug# t#ere are several ways around t#is &roble,+ we will use an cE1&ort%defc file% A an I&(+ort9 efI File

.dding an E1&ort%def file will allow us to tell t#e "o,&iler t#e &ro&er na,es of our D fun"tions t#at s#ould be e1&osed% 16 A a ne$ te(t file to your +roject calle I&(+ort9 efI

De&art,ent of Infor,ation Te"#nology

$ad% Dr%D%)%$atil Institute *f Te"#nology+ $i,&ri C#oose File C .e$ fro, t#e ,enu wit#in 6isual CJJ and sele"t t#e 0iles tab% Sele"t cTe1t 0ilec as t#e file ty&e and ty&e in cE1&ort%defc as t#e filena,e% Cli"7 O? to "reate t#e file%

26 A

the IL'B,A,%I an I&K"O,T!I sections to the I&(+ort9 efI file9

.ll t#at we need to do now is to add a few se"tions to t#e cE1&ort%def:c file and we will be in business% T#e first se"tion is t#e c I8R.R)c de"laration wit# our D na,e% T#e se"ond is t#e cEK$*RTSc se"tion w#i"# de"lares our e1&orted fun"tions% I8R.R) dl EK$*RTS add9 g 9 Co&y and &aste t#e above lines into t#e cE1&ort%defc file% :ow you "an build your &ro(e"t% 0irst of all+ ,a7e sure you are building t#e cReleasec build by sele"ting Buil C !elect Active )onfiguration+ "#oosing c'a7eTe,&0ilena,eD ! -in=2 Releasec and "li"7ing O?% T#en (ust #it FG to build t#e D % It s#ould "o,&ile and lin7 wit# no troubles%

De&art,ent of Infor,ation Te"#nology

$ad% Dr%D%)%$atil Institute *f Te"#nology+ $i,&ri /sing the #LL from 0B a++ication .t t#is &oint you #ave a fully!fun"tional D t#at "an be "alled fro, 6isual 8asi"+ anot#er CJJ a&&li"ation or any ot#er &rogra, "a&able of loading "an "alling D fun"tions% /ere we will fo"us on #ow to "all our new fun"tion fro, any of t#e Indigo Rose develo&,ent tools t#at use t#e ua s"ri&ting language% $rivate De"lare 0un"tion add9 ib cC:`ss`dl`Debug`dl%dllc (8y6al a .s Double+ 8y6al b .s Double) .s Double 'n+ut: In&ut is given t#roug# 68 environ,ent Out+ut: )ou will be "reating a &ro(e"t file for Dyna,i" in7 ibrary &ro(e"t% T#e "o,&iler and lin7ing settings will be "onfigured to generate a D file% T#e &ro(e"t will be blan7+ now you ,ust add or "reate your sour"e files ,anually% T#e 6CJJ dll will send ba"7 t#e out&ut to 68 environ,ent% 'nstructions: T#e students ,ay ado&t any ,et#od and alternative for t#is &arti"ular assign,ent% )ou are given a fle1ibility to "#oose t#e fun"tions for w#i"# u are going to "reate t#e D % /en"e u are e1&e"ted to illustrate all assu,&tions and ne"essary t#eoreti"al details% !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ! FA7s 9% -#at do you ,ean by D ] 2% -#i"# are different ty&es of D ] -----------------------------------------------------------------------------------------------------------

Assignment 0L
De&art,ent of Infor,ation Te"#nology

$ad% Dr%D%)%$atil Institute *f Te"#nology+ $i,&ri

Aim: Objective: Theory:

Study of Re"ursive des"ent &arser and s#ift Redu"e &arser To understand t#e to& down and botto, u& &arsing

To+- o$n +arsing: T#e to& down "onstru"tion of a &arse tree is done by starting wit# t#e root +labeled wit# t#e starting non!ter,inal +and re&eatedly &erfor,ing t#e following two ste&s! 9% at node n+ labeled wit# non!ter,inal .+sele"t one of t#e &rodu"tions for . and "onstru"t "#ildren at n for t#e sy,bols on t#e rig#t side of t#e &rodu"tion 2% 0ind t#e ne1t node at w#i"# t#e subtree is "onstru"ted% 0or so,e gra,,ars+ t#e above ste&s "an be i,&le,ented during a single left to rig#t s"an of t#e in&ut string% T#e "urrent to7en being s"anned on t#e in&ut is often "alled as t#e lookahead sy,bol % Initially t#e loo7a#ead sy,bol is t#e first i%e t#e left,ost to7en of t#e in&ut string% et us "onsider t#e following gra,,ar% . !5 8. Ta T aa 8 !5 88 Tb and now "onsider t#e in&ut string Q bbaaR T#e to&!down &arsing would loo7 li7e t#is in different ste&s! 9% .

in&ut : bbaa + t#e #ig#lig#ted "#ara"ter indi"ates t#e loo7a#ead%

2%

De&art,ent of Infor,ation Te"#nology

$ad% Dr%D%)%$atil Institute *f Te"#nology+ $i,&ri

8 =% 8 8 ?% 8 8 b >% in&ut: bbaa . 8 8 b <% in&ut: bbaa . 8 8 b 8 b a 8 b 8 8 . .

. a

/ere we #ave assu,ed t#at + at t#e first atte,&t t#e &arser would 7now w#i"# &rodu"tion to use to get t#e rig#t out&ut+ but in general+ t#e sele"tion of a &rodu"tion of a non!ter,inal ,ay involve trial and error+ t#at is we ,ay #ave to try a &rodu"tion and ba"7tra"7 to try anot#er &rodu"tion if t#e first is found to be unsuitable% . &rodu"tion is unsuitable +if after using t#e &rodu"tion+ we "annot "o,&lete t#e tree to ,at"# t#e in&ut string %we will dis"uss t#is &arsing in t#e ne1t se"tion%

,&)/,!'0&-#&!)&.T "A,!'.1

De&art,ent of Infor,ation Te"#nology

$ad% Dr%D%)%$atil Institute *f Te"#nology+ $i,&ri

T#is is general for, of to&!down &arsing + "alled re"ursive des"ent &arsing w#ere ba"7tra"7ing ,ay be involved% T#is is a bad ty&e of &arsing w#i"# involves re&eated trying to get t#e "orre"t out&ut%T#is "an also be ter,ed as brute!for"e ty&e of &arsing% $resently+ t#is ty&e of &arsing is outdated +(ust be"ause t#ere are ,u"# better ,et#ods of &arsing w#i"# we will be dis"ussing later% Consider t#e gra,,ar: S !5 ".d T bd . !5 ab T a and t#e in&ut string is Q"adR% To "onstru"t t#e tree + we "reate an initial tree of (ust one node S% T#e in&ut &ointer &oints to c + and we use t#e first &rodu"tion+ for s to get t#e e1&anded tree% S " . d

T#e left,ost leaf labeled c ,at"#es t#e fist sy,bol of t#e in&ut and #en"e we advan"e t#e &ointer to t#e se"ond sy,bol of t#e in&ut w#i"# is a% we now e1&and . by its first &rodu"tion to obtain t#e following tree% S " a . d b

now we #ave a ,at"# for t#e se"ond sy,bol of t#e in&ut and #en"e advab"e t#e &ointer to d + and "o,&are it wit# t#e ne1t leaf b, w#i"# does not ,at"# + we re&ort failure and go ba"7 to see w#et#er t#ere is an alternative &rodu"tion for .% In going ba"7 to . +we ,ust ba"7tra"7 t#e in&ut &ointer to a% finding anot#er &rodu"tion + we try out t#e ne1t "onfiguration% S " . a :ow t#e leaf a ,at"#es wit# t#e se"ond sy,bol of t#e in&ut and t#e t#e t#ird leaf d ,at"#es wit# t#e t#ird sy,bol of t#e in&ut% .nd be"ause t#e in&ut string is "onsu,ed + we #alt and denote t#e su""essful "o,&letion of &arsing% d

De&art,ent of Infor,ation Te"#nology

$ad% Dr%D%)%$atil Institute *f Te"#nology+ $i,&ri

BOTTOM-/" "A,!'.1 In 8otto,!u& &arsing we start wit# t#e senten"e and try to a&&ly t#e &rodu"tion rules in reverse+ in order to finis# u& wit# t#e start sy,bol of t#e gra,,ar% T#is "orres&onds to starting at t#e leaves of t#e &arse tree+ and wor7ing ba"7 to t#e root%It "an be t#oug#t of a &ro"ess of redu"ing t#e string in 2uestion to t#e start sy,bol of t#e gra,,ar 8otto,!u& &arsing is also 7nown as shift-re uce &arsing Su&&ose we #ave a gra,,ar S !5 a.8e . !5.b" T b 8 !5d .nd t#e in&ut string is abb"de T#en an instan"e of botto,!u& &arsing "an be given as a.de !5 a.8e !5 S /ere in t#e first ste& we used t#e derivation 8 !5 d and t#en t#e derivation S !5 a.8e T#us t#is &ro"ess is li7e tra"ing out t#e rig#t ,ost derivations in reverse% ,'1ET #&,'0AT'O. E1&anding t#e rig#t,ost non!ter,inal In "onte1t of t#e above e1a,&le rig#t derivations "an be s#own as S !5 a.8e !5 a.de !5 a.b"de !5 abb"de /ere in ea"# ste& we #ave e1&anded t#e rig#t,ost ter,inal% EA.#L&: . #andle is a substring t#at ,at"#es t#e rig#t #and side of a &rodu"tion and re&la"ing R/S by /S ,ust be ste& in t#e reverse rightmost erivation t#at ulti,ately leads to t#e start sy,bol%%If re&la"ing a string does not ulti,ately lead to t#e start sy,bol it "anDt be a #andle In t#e above e1a,&le Abc is an e1a,&le of a #andle% S !5 a.8e !5 a.de !5 aAbcde !5 abb"de

De&art,ent of Infor,ation Te"#nology

$ad% Dr%D%)%$atil Institute *f Te"#nology+ $i,&ri Consider t#e gra,,ar E !5 E J E T E U E T id Rig#t sentential for, id9 J id2 U id= E J id2 U id= E J E U id= EJEUE EJE E /andle id9 id2 id= EUE EJE $rodu"tion Rule E !5 id E !5 id E !5 id E !5 E U E E !5 E JE

/ere it "an be noted t#at string a&&earing to t#e rig#t of a #andle "ontains only ter,inal sy,bols Sin"e #ere t#e gra,,ar is a,biguous t#e "#oi"es for t#e #andles "an be different de&ending u&on rig#t derivations used% In botto, u& &arsing using #andles t#e ,ain &roble,s are 9% Identifying t#e #andle 2% Identifying t#e rule to redu"e T#is &ro"ess "an be ,ade algorit#,i" using a sta"7 i,&le,entation De"ision 9 shift t#e ne1t in&ut sy,bol onto t#e to& of t#e sta"7 De"ision 2 ,e uce t#e sy,bol at t#e to& of t#e sta"7 %/ere t#e &arser 7nows t#at t#e rig#t end of t#e #andle is at t#e to& t#e sta"7% It ,ust t#en lo"ate t#e left end of t#e #andle wit#in t#e sta"7 and re&la"e it wit# t#e non!ter,inal T#us it is be"ause of t#is a&&roa"# t#at it is also "alled botto,!u& &arsing% T#e ,ain two &roble,s stated above are ter,ed as shift-re uce and re uce-re uce "onfli"ts% /ere are t#e a"tions of a s#ift!redu"e &arsers for t#e string id9 J id2 U id= for t#e gra,,ar defined above ST.CL 9% f 2% fid= =% fE ?% fE J >% fE J id2 <% fE J E ;% fE J E U 8% fE J E U id= H% fE J E U E 90%fE J E I:$AT id9 J id2 U id=f J id2 U id= f J id2 U id= f id2 U id= f U id= f U id= f id= f f f f De&art,ent of Infor,ation Te"#nology .CTI*: s#ift Redu"e(E !5 id) S#ift S#ift Redu"e (E!5id) S#ift S#ift Redu"e (E!5id) Redu"e (E!5E U E) Redu"e (E!5E J E)

$ad% Dr%D%)%$atil Institute *f Te"#nology+ $i,&ri 99%fE f a""e&t

:ote in < E J E "ould #ave been redu"ed to E instead of s#ifting ( a s#ift!redu"e "onfli"t) 8ut we "#ose to s#ift be"ause it would #ave &rodu"ed t#e start sy,bol(99) even if t#e w#ole in&ut was not "onsu,ed T#ere are C0GDs for w#i"# s#ift!redu"e &arsing "annot be used% 0or su"# gra,,ars t#e S#ift!redu"e or redu"e!redu"e "onfli"ts "annot be resolved !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ! FA7s 9% -#at is &arsing] 2% Differentiate between To& down and botto, uo &arsing% =% -#at do you ,ean by left,ost and rig#t,ost derivation ] !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !

De&art,ent of Infor,ation Te"#nology

$ad% Dr%D%)%$atil Institute *f Te"#nology+ $i,&ri

Assignment 0M
Aim:-rite a &rogra, to i,&le,ent a Tower of /anoi (Re"ursive i,&le,entation) in C FCJJ language% Objective:To understand and i,&le,ent Re"ursive algorit#, using t#e Tower of /anoi &roble, and study Divide and Con2uer strategy% Theory:#ivi e an con3uer strategy: #ivi e: Divide t#e &roble, into a nu,ber of sub&roble, t#at are s,aller instan"es of t#e sa,e &roble,% )on3uer: Con2uer t#e sub&roble, by solving t#e, re"ursively% If t#e sub&roble, si3es are s,all enoug#+ (ust solve t#e sub&roble,s in a straig#t forward ,anner% )ombine: Co,bine t#e solutions to t#e sub &roble,s into t#e solutions for t#e original &roble,% -#en t#e sub&roble,s are large enoug# to solve re"ursively+ we "all t#at t#e re"ursive "ase% *n"e t#e sub&robles be"o,e s,all enoug# t#at we no longer re"urse+ re"ursion Qbotto,s outR and t#at we #ave gone down to t#e base "ase% In "o,bine ste&+ we solve sub&roble,s t#at are 2uite not sa,e as original% Re"urren"es go #and in #and wit# divide!and!"on2uer &aradig,+ be"ause t#ey give us a natural way to "#ara"teri3e t#e running ti,es of divide!and!"on2uer algorit#,s% Algorithm #An )5"6 [ If s,all(($) t#en return S($)^ Else [ Divide $ into s,aller instan"es say $9+$2+$=VVV%$7 + 75E9^ .&&ly D.ndC to ea"# of t#se sub&roble,s Return Co,bine(D.ndC($9)+D.ndC($2)+VVD.ndC($7))^ \ \ To$er of Eanoi +roblem: .ssu,e t#at t#ere are CnD nu,ber of dis7s on tower .% T#e dis7s are of de"reasing si3e and are sta"7ed on t#e tower in de"reasing order of si3e botto, to to&%

De&art,ent of Infor,ation Te"#nology

$ad% Dr%D%)%$atil Institute *f Te"#nology+ $i,&ri 8esides t#is tower+ t#ere are two ot#er towers labeled 8 and C% T#e dis7s fro, tower . are to be ,oved to tower 8 using tower C as inter,ediate storage% T#e dis7s are to be ,oved one at a ti,e% In addition+ at no ti,e "an a dis7 be on to& of a s,aller dis7% E1a,&le:

% % %

Tower .

Tower 8

Tower C

.ssu,e t#at t#ere are n nu,ber of dis7s on tower .% To get t#e largest dis7 to t#e botto, of tower 8+ we ,ove t#e re,aining n!9 dis7s to tower C and t#en ,ove t#e largest dis7 to tower 8% :ow +we are left wit# t#e tas7 of ,oving t#e dis7s fro, tower C to tower 8% To do t#is+ we #ave tower . and 8 available% T#e fa"t t#at t#e tower 8 #as a dis7 "an be ignored as t#e dis7 is larger t#an t#e dis7s being ,oved fro, tower C and so any dis7 "an be &la"ed on to& of it% Algorithm To$erofEanoi5n+1+y+3) FF'ove t#e to& n dis7s fro, tower 1 to tower y [ If(n 5E 9)t#en [ Towerof/anoi(n!9+1+3+y)^ -rite(Q,ove to& dis7 fro, towerR+1+Rto to& of towerR+y)^ Towerof/anoi(n!9+3+y+1)^ \ \ T#e re"ursive nature of t#e solution is a&&arent fro, t#e above algorit#,% *ur solution for an n!dis7 &roble, is for,ulated in ter,s of solutions to two(n!9)dis7 &roble,s% Analysis ,ecurrence relation: T(n)Eg(n)VV%% n is s,all ET(n9)JT(n2)JVVJT(n7)Jf(n) In general+ T(n)ET(9) EaT(nFb)Jf(n)% nE27 &(am+le:

De&art,ent of Infor,ation Te"#nology

$ad% Dr%D%)%$atil Institute *f Te"#nology+ $i,&ri tn E 0 Et(n!9) J 9Jt(n!9) -#ere first t(n!9) E,ove,ent of (n!9) dis7s fro, tower . to tower C% ,ove,ent of t#e botto,,ost dis7 fro, tower . to tower 8% Se"ond t(n!9) E ,ove,ent of t#e (n!9) dis7s fro, tower C to tower 8% tn E 2t(n!9) J 9

9 is for t#e

T#us+ tn ! 2tn!9 E 9 bE9 &(n)E9 (1!2) (1!9)

C#ara"teristi" &olyno,ial is tn E "9 9n J "2 2n

t9E 2t0 J 9 t0 E 0 V%%*riginal re"ursive relation t9 E 9 w#en nE0 tn E "9 J "2 E 0+ t0 is 0 -#en nE9 tn E "9 J 2"2 E9+ t9 is 0 !"2 E !9 "2 E 9 "9 E !9 tn E 2n!9

VVtower of #anoi

tn E nu,ber of dis7 ,ove,ents%

'n+ut an Out+ut: 'n+utNnNno9of is*s9 Out+utNt5n6Nno of is* movements9 &3uation: tn N 2n-19 9) -#en nE2: t(2)N 22 -1 N 89

De&art,ent of Infor,ation Te"#nology

$ad% Dr%D%)%$atil Institute *f Te"#nology+ $i,&ri

T To$er A To$er B To$er ) 'n this figure; t$o is*s are *e+t on to$er A

To$er A

To$er B

To$er )

'n this figure; the to+most is* from to$er A is move to to$er ) 5 1 movement6

To$er A

To$er B

To$er )

'n this figure; the is* from A is move to to$er B an then the is* from to$er ) is move to to$er B ; total 2 movements Thus; $hen there are t$o is*s to be move an the no of is* movements is 51H26 89 26-#en nE=: T(=)E2=!9 E;% Jhen there are 8 is*s to be move ; the no of is* movements is G9 1ra+hical out+ut is not e(+ecte ; if one most $elcomeO )onclusion: T#e Divide and "on2uer strategy of t#e Re"ursive .lgorit#, using Tower of /anoi is studied and i,&le,ented% ----------------------------------------------------------------------------------------------------------FA7s : 9% E1&lain divide and "on2uer strategy% 2% E1&lain #ow t#e re"urren"e relations #el& in doing t#e analysis of algorit#,s =% Solve t#e re"urren"e relation of tower of /anoi using re&etitive substitution%

De&art,ent of Infor,ation Te"#nology

$ad% Dr%D%)%$atil Institute *f Te"#nology+ $i,&ri !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !

Assignment 10
Aim - -rite a &rogra, in CFCJJ to i,&le,ent 7rus7alDs algorit#, and $ri,^s algorit#,% Objective: - To understand t#e role of Lrus7al and $ri,Ds algorit#, in finding out t#e ,ini,u, "ost s&anning tree% Theory : Lrus7alDs algorit#, is anot#er greedy algorit#, for t#e ,ini,u, s&anning tree &roble, t#at also always yields an o&ti,al solution% It is na,ed Lrus7alDs algorit#, OLru><P+ after aose&# Lrus7al+ w#o dis"overed t#e algorit#, w#en #e was a se"ond!year graduate student% Lrus7alDs algorit#, loo7s at t#e ,ini,u, s&anning tree for a weig#ted "onne"ted gra&# G E [6+ E\ as an a"y"li" subgra&# wit# T6T!9 edges for w#i"# t#e su, of t#e edges weig#ts is t#e s,allest% T#e algorit#, begins by sorting t#e gra&#Ds edges in non de"reasing order of t#eir weig#ts% T#en+ starting wit# t#e e,&ty subgra&#+ it s"ans t#is sorted list adding t#e ne1t edge on t#e list to t#e "urrent subgra&# if su"# an in"lusion does not "reate a "y"le and si,&ly s7i&&ing t#e edge ot#erwise% #ata !tructures use : 9% Ase array reorientation of sets w#ere value in t#e array is eit#er a lin7 to &arent field or for root node t#e value is total no of "#ildren wit# negative sign% 2% -e use #ea& to store t#e edge infor,ation% "seu oco e for ?rus*al: FF Cost B 2 di,ensional ,atri1+ "ostOuPOvP is "ost of edge (u+v)^ FF n no% of verti"es% FFCtD set of edges in ,ini,u, s&anning tree B it us a 2D array% tO9V%%nPO9V%2P FF initially ea"# verte1 is in different set% 9% Constru"t a ,ini,u, #ea& out of edge "osts using #ea&ify% 2% 0or( i E 9 to n) do &arentOiP E !9^ FF ea"# verte1 is in different set% =% IE0^ ,in"ostE0^ ?% -#ile(i4n!9) II (#ea& not e,&ty) to >% [

De&art,ent of Infor,ation Te"#nology

$ad% Dr%D%)%$atil Institute *f Te"#nology+ $i,&ri <% ;% 8% H% 90% 99% 92% 9=% 9?% \ .lgorit#, Si,&leAnion (I+ () [ &OiP E (^ \ .lgorit#, Si,&le0ind (i ) [ w#ile (&OiP 5E0) do iE&OiP^ return I^ \ FF i I ( are roots of sets% \ Delete ,ini,u, "ost edge (u+v) fro, #ea& Re#ea&ify using ad(ust% aEfind(u)^ 7Efind(v)^ If ((hE7) t#en` [ IJJ^ tOiPO9PEu+tOiPO2PEv^ Anion((+7)^ ,in"ost E ,in"ostJ"ostOuPOvP^

FF return root of CiD

If we want to &erfor, n!9 unions t#en "an be &ro"essed in inear ti,e *(n) T#e ti,e re2uired to &ro"ess a find for an ele,ent at level CiD of a tree is *(i)+ so total ti,e needed to &ro"ess n finds is O5nP26 *ne "an i,&rove t#e &erfor,an"e of our union I find by avoiding "reation of degenerating trees% -e a&&ly weig#ting rule for Anion+ w#i"# says t#at if t#e nu,ber of nodes in t#e tree wit# root i is less t#an t#e nu,ber of nodes in t#e tree wit# root (+ t#en ,a7e ( as t#e &arent of i+ ot#erwise ,a7e i+ as t#e &arent of (% T#us we avoid degeneratedFs7ewed trees% Si,ilarly we use Colla&se0ind + to i,&rove t#e &erfor,an"e of find% In Colla&sing0ind+ we are re2uired to go u& to rea"# t#e root node and t#en reset t#e lin7s% Every first find #as to go t#roug# inter,ediate nodes to go u& to root node+ but every ne1t find on si,ilar node will re2uire only one lin7 to go u&+ t#us redu"ing t#e "ost "ost of find% Colla&sing rule says t#at+ if ( is a node on t#e &at# fro, i to its root node and &OiP is not rootOiP+ t#en set &O(P to rootOiP% -eig#ted union(I+() [ Te,&E&OiPJ&O(P^ If(&OiP5&O(P) FF i #as fewer nodes [ De&art,ent of Infor,ation Te"#nology

$ad% Dr%D%)%$atil Institute *f Te"#nology+ $i,&ri &OiPE(+ &O(PEte,&^ \ Else [ &O(PEI^ FF ( #as fewer nodes &OiPEte,&^ \ \ Colla&se 0ind(i) [ rEI^ -#ile(&OrP5E0) rE&OrP^ w#ile(iEr) [ sE&OiP^ &OiPEr^ iEs^ \ \ Analysis of ?rus*al algorithm : Ti,e re2uired to "onstru"t t#e initial #ea& is log E% Re,oval of edge fro, #ea& re2uires again log E and t#e edge is re,oved in t#e w#ile loo& w#i"# runs for E ti,es+ #en"e t#e ti,e "o,&le1ity of algorit#, is *(E log E)% /ere sin"e we #ave used sets to re&resent trees and we #ave used effi"ient find and union algorit#,s w#i"# ta7e al,ost *(9) ti,e% In&ut B *ut&ut : -rite t#is as &er your i,&le,entation

"rimAs algorithm for finding minimum cost of spanning tree


Objective of +rimAs algorithm is to understand the .reedy method to o tain a minimum cost spanning tree edge y edge. 1ree y metho : 9% It is t#e ,ost straig#t forward design te"#ni2ue% 'ost of t#e &roble,s #ave n in&uts I re2uire us to obtain a subset t#at satisfies so,e "onstraints% 2% .ny subset t#at satisfies t#ese "onstraints is "alled a feasible solution% =% -e find a feasible solution t#at eit#er ,a1i,i3es or ,ini,i3es a given ob(e"tive fun"tion% ?% . feasible solution t#at does t#is is "alled an C*&ti,al SolutionD % 1ree y algorithm :

De&art,ent of Infor,ation Te"#nology

$ad% Dr%D%)%$atil Institute *f Te"#nology+ $i,&ri It wor7s in stages + "onsiders one in&ut at a ti,e % In&uts are "onsidered in an order deter,ined by so,e sele"tion &ro"edure% .t ea"# stage+ de"ision is ,ade regarding w#et#er a &arti"ular in&ut is in o&ti,al solution% In"lusion of ne1t in&ut into &artially "onstru"ted o&ti,al solution s#ould always result into feasible solution% If not t#en+ t#is in&ut is not "onsidered% Sele"tion &ro"edure is based on so,e o&ti,i3ation ,easure^ w#i"# ,ay be ob(e"tive fun"tion% Greedy algorit#, t#at ,a7es use of o&ti,i3ation ,easures to generate subo&ti,al solution is "alled CSubset $aradig,D%

1eneral gree y algorithm : .lgorit#, Greedy (a+n) FF aO9VnP "ontains n in&uts% [ solutionE0^ FFinitialise t#e solution for( iE9 to n ) do [ 1E sele"t(a)^ if(feasible(solution+ 1)) t#en solutionE union(solution + 1)^ \ return solution^ \ 0un"tion sele"t + sele"ts an in&ut fro, aO P and re,oves it% T#e sele"ted in&utDs value is assigned to 1% 0un"tion feasible is 8oolean valued fun"tion t#at deter,ines w#et#er 1 "an be in"luded into solution ve"tor% 0un"tion union "o,bines 1 wit# solution and u&dates t#e ob(e"tive fun"tion% Lna&sa"7 &roble, + (ob se2uen"ing + ,ini,u, s&anning trees "o,es under t#e subset &aradig,% S#ortest &at# + /uff,an "odes "o,es under ordering &aradig,% Minimum cost s+anning trees : Definition : S&anning trees B et G E (6 + E) be an undire"ted "onne"ted gra&#% . subgra&# tE ( 6 + ED) of G is a s&anning tree if and only if t is a tree% (:o "y"le e1ists%) In &ra"ti"al situations+ t#e edges #ave weig#ts assigned to t#e,+ weig#ts are &ositive% T#ese weig#ts ,ay re&resent t#e "ost of "onstru"tion+ lengt#s of lin7s et"% Given su"# weig#ted gra&# one would li7e to sele"t "ities (verti"es) to #ave ,ini,u, total "ost F ,ini,u, total lengt#% So one "an find a s&anning tree wit# ,ini,u, "ost%

De&art,ent of Infor,ation Te"#nology

$ad% Dr%D%)%$atil Institute *f Te"#nology+ $i,&ri Sin"e identifi"ation of ,ini,u, s&anning tree involves sele"tion of a subset of t#e edges+ t#is &roble, fits into t#e Subset $aradig,% "rims algorithmAs outline : Greedy ,et#od "onstru"ts t#e tree by in"luding edge by edge% C#oose an edge t#at results in a ,ini,u, in"rease in su, of "ost of edges so far in"luded% .lso set of edges so far sele"ted for, a tree% (:o "y"le%) "seu o co e for "rimAs algorithm : $ri,() FF "ostO9%%nPO9%%P is a "ost ad(a"en"y ,atri1% FF n: nu,ber of verti"es in t#e gra&#% FF "ost O i PO ( P Ei + if t#ere is no edge between I and (% FF "ost O i PO ( P Ei + if iE( FF "ost O i PO ( P E "ost O i PO ( P E &ositive nu,ber if it is edge% FF t O9%%n!9PO9%%2P #as all t#e edges of ,ini,u, s&anning tree FF ,in"ost is ,ini,u, "ost FF nearO P is an array w#i"# stores verte1 in tree FF su"# t#at "ost O ( PO nearO ( P P is ,ini,u, a,ong all "#oi"es for nearO ( P% 9% ,in"ost E0^ 2% 0or (iE2 to n) do nearO i PE9^ =% :earO9PE0 FFverte1 9 is initially in CtD % ?% 0or(iE9 to n!9) do >% [ FF find n!9 edges of tree% <% et ( be an inde1 su"# t#at ;% :earO ( P hE0 I "ostO ( PO nearO ( P P is ,ini,u,% 8% FF "o,&utation of ( re2uires linear loo& H% FF not s#own above% 90% tO i PO9P E (^ tO i PO2PE nearO ( P^ 99% ,in"ostE,in"ost J "ostO ( POnear O ( P P^ 92% :earO ( P E0^ 9=% 0or( 7E 9 to n ) do FFu&date nearO P 9?% If(nearO 7 P hE0) II ("ostO 7 PO nearO 7 P P 5 "ostO 7 PO ( P) 9>% :earO 7 PE(^ 9<% \ 9;% return ,in"ost% !tages in "rimAs algorithm : )om+le(ity : T#e ti,e re2uired by algorit#, $ri, is O5nP26 w#ere n is nu,ber of verti"es in gra&# G% Ea"# iteration of for loo& ta7es *(n) ti,e% So t#e total ti,e is *(nj2)%

De&art,ent of Infor,ation Te"#nology

$ad% Dr%D%)%$atil Institute *f Te"#nology+ $i,&ri If we store nodes not yet in"luded in tree as a red!bla"7 tree t#en algorit#, ta7es *(log n) ti,e% Disadvantages of $ri,Ds algorit#,: 9% ist of edges #as to be sear"#ed fro, beginning as new edge gets added% 2% If t#ere are ,ore t#an one edges #aving sa,e weig#t t#en all &ossible s&anning trees are re2uired to be found for final ,ini,al tree% !am+le 'n+ut - Out+ut : -rite t#is as &er your i,&le,entation )onclusion : T#us we #ave understood t#e role of using Lrus7al algorit#, in finding t#e ,ini,u, "ost s&anning tree% .lso we understood t#e role of &ri,Ds algorit#, wit# res&e"t to greedy a&&roa"#% !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ! FA7s 9% E1&lain t#e weig#ted union and "olla&se find rule wit# e1a,&le% 2% /ow t#e above rules i,&rove t#e total "o,&le1ity of Lrus7al algorit#,% =% -#y t#e edge infor,ation is stored in /ea&] ?% Can we use any ot#er sort te"#ni2ue to get sorted edges+ if so "o,,ent on ti,e "o,&le1ity% >% E1&lain #ea&ify F ad(ust algorit#,s in ,aintaining #ea& &ro&erty% <% -#at is a red bla"7 tree] ;% Distinguis# between $ri,s and Lrus7al algorit#,% 8% E1&lain t#e greedy strategy used in bot# algorit#,s%

De&art,ent of Infor,ation Te"#nology

$ad% Dr%D%)%$atil Institute *f Te"#nology+ $i,&ri

Assignment 11
Aim: I,&le,entation of 8 2ueen &roble,( :on re"ursive i,&le,entation) using ba"7tra"7ing strategy Objective: To study and understand t#e "on"e&t of ba"7tra"7ing% Theory: Bac*trac*ing General ,et#od! b Aseful te"#ni2ue for o&ti,i3ing sear"# under so,e "onstraints b E1&ress t#e desired solution as an n!tu&le (19+ % % % + 1n) w#ere ea"# 1i 2 Si+ Si being a finite set b T#e solution is based on finding one or ,ore ve"tors t#at ,a1i,i3e+ ,ini,i3e+ or satisfy a "riterion fun"tion $(19+ % % % + 1n) b Sorting an array aOnP B 0ind an n!tu&le w#ere t#e ele,ent 1i is t#e inde1 of it# s,allest ele,ent in a B Criterion fun"tion is given by aO1iP M aO1iJ9P for 9 M i 4 n B Set Si is a finite set of integers in t#e range O9+nP b 8rute for"e a&&roa"# B et t#e si3e of set Si be ,i B T#ere are , E ,9,2 k k k,n n!tu&les t#at satisfy t#e "riterion fun"tion $ B In brute for"e algorit#,+ you #ave to for, all t#e , n!tu&les to deter,ine t#e o&ti,al solutions b 8a"7tra"7 a&&roa"# B Re2uires less t#an , trials to deter,ine t#e solution B 0or, a solution (&artial ve"tor) and "#e"7 at every ste& if t#is #as any "#an"e of su""ess B If t#e solution at any &oint see,s not!&ro,ising+ ignore it B If t#e &artial ve"tor (19+ 12+ % % % + 1i) does not yield an o&ti,al solution+ ignore ,iJ9 k k k,n &ossible test ve"tors even wit#out loo7ing at t#e, b .ll t#e solutions re2uire a set of "onstraints divided into two "ategories: e1&li"it and i,&li"it "onstraints Definition 9 E1&li"it "onstraints are rules t#at restri"t ea"# 1i to ta7e on values only fro, a given set% B E1&li"it "onstraints de&end on t#e &arti"ular instan"e I of &roble, being solved B .ll tu&les t#at satisfy t#e e1&li"it "onstraints define a &ossible solution s&a"e for I B E1a,&les of e1&li"it "onstraints M 1i M 0+ or all nonnegative real nu,bers M 1i E [0+ 9\ De&art,ent of Infor,ation Te"#nology

$ad% Dr%D%)%$atil Institute *f Te"#nology+ $i,&ri M li M 1i M ui Definition 2 I,&li"it "onstraints are rules t#at deter,ine w#i"# of t#e tu&les in t#e solution s&a"e of I satisfy t#e "riterion fun"tion% B I,&li"it "onstraints des"ribe t#e way in w#i"# t#e 1is ,ust relate to ea"# ot#er% Q L-3ueens +roblem B $la"e eig#t 2ueens on an 8 l 8 "#essboard so t#at no 2ueen atta"7s anot#er 2ueen 8a"7tra"7ing 9 2 = ? > < ; 8 9 @ 2 @ = @ ? @ > @ < @ ; @ 8 @

B Identify data stru"tures to solve t#e &roble, M 0irst &ass: Define t#e "#essboard to be an 8 l 8 array M Se"ond &ass: Sin"e ea"# 2ueen is in a different row+ define t#e "#essboard solution to be an 8!tu&le (19+ % % % + 18)+ w#ere 1i is t#e "olu,n for it# 2ueen B Identify e1&li"it "onstraints M E1&li"it "onstraints using 8!tu&le for,ulation are Si E [9+ 2+ =+ ?+ >+ <+ ;+ 8\+ 9 M i M 8 M Solution s&a"e of 88 8!tu&les B Identify i,&li"it "onstraints M :o two 1i "an be t#e sa,e+ or all t#e 2ueens ,ust be in different "olu,ns k .ll solutions are &er,utations of t#e 8!tu&le (9+ 2+ =+ ?+ >+ <+ ;+ 8) k Redu"es t#e si3e of solution s&a"e fro, 88 to 8h tu&les M :o two 2ueens "an be on t#e sa,e diagonal B T#e solution above is e1&ressed as an 8!tu&le as ?+ <+ 8+ 2+ ;+ 9+ =+ > 'n+ut: as s&e"ifies nu,ber of 2ueens (#ere 8) Out+ut: for 8 2ueens H2 ste&s% De&art,ent of Infor,ation Te"#nology

$ad% Dr%D%)%$atil Institute *f Te"#nology+ $i,&ri

----------------------------------------------------------------------------------------------------------FA7: 9%-#at is ba"7tra"7ing% 2%-#at is t#e ti,e "o,&le1ity for 8 2ueens &roble, using ba"7tra"7ing% !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !

De&art,ent of Infor,ation Te"#nology

S-ar putea să vă placă și