SYSTEM PROGRAMMING & COMPILER CONSTRUCTION
Mahatma Education Society’s PILLAI HOC COLLEGE OF ENGINEERING AND TECHNOLOGY, RASAYANI
La
Manua!
System P"o#"ammin# & Com$i!e" Const"uction
P"o#"am
%
TE
Semeste"
%
'I
Su(ect Name
%
System P"o#"ammin# & Com$i!e" Const"uction
P"actica! In)Cha"#e Name & Si#natu"e Department of Computer Engineering
I*C +O, P"o- Sandee$ Ras.a"
SYSTEM PROGRAMMING & COMPILER CONSTRUCTION
Mahatma Education Society’s PILLAI HOC COLLEGE OF ENGINEERING AND TECHNOLOGY ESAY 2014-2015
Department
Com$ute" En#inee"in#
C!a""
TE
S#$%e&t
System P"o#"ammin# & Com$i!e" Const"uction
E'per(ment L("t
Se"ia! No 0
Name o- e/$e"iment
6
Study o- 123*143 $"ocesso" 5ith inst"uctions Assem!e"s% 6 $ass Assem!e"
1
Mac"o $"ocesso"% 6 $ass Mac"o $"ocesso"
7
Le/ica! ana!y8e" too! % -!e/
9
Pa"se" #ene"ato" #ene"a to" too!% Yacc Yacc
2
:ind -i"st;< = -o!!o5;< set o- #i>en #"amma"
4
C"eate you" !i"a"y in Linu/ en>i"onment and usin# it
?
Code Gene"ation a!#o"ithm
@
Code O$timi8ation techniues
03
Study !d command in Uni/*Linu/
E'per(ment N)* 01 St#+ ) ./0.0 pr)&e"")r (t3 (n"tr#&t()n"
Department of Computer Engineering
SYSTEM PROGRAMMING & COMPILER CONSTRUCTION A(m Study o- 123*143 $"ocesso" 5ith inst"uctions
T3e)r
C)n&!#"()n
Department of Computer Engineering
SYSTEM PROGRAMMING & COMPILER CONSTRUCTION
Experiment No. 2 A""em$!er" 2 pa"" A""em$!er* A""em$!er*
Title: Design and implementation of 2 pass assemblers for X86 machine Theory: Assembly Language Syntax:
Each line of a program is one of the following:
an instruction
an assembler directive (or pseudo-op)
a comment
hitespace (between s!mbols) and case are ignored" #omments (beginning with $%&) are also ignored"
'n instruction has the following format: 'E *+#*DE *+E,'D. % #*//E0.
E1ample: 'DD,3,3,4 'DD,3,354 D,63/E, ,7**+ Schemes of translation:
Department of Computer Engineering
SYSTEM PROGRAMMING & COMPILER CONSTRUCTION
I. Two pass translation:
t can handles forward reference easil!"
ocation counter(#) processing is done in pass and the s!mbols defined in the program are entered into s!mbol table" t able"
0he second pass uses this address information to generate target program"
II. Single pass translation:
9orward reference is handled b! bac patching"
ata structure of assembler
.!m .!mbol bol 0able(. le(.0) 0)
" /nemonics /nemonics table or /achin /achine-*pe e-*peration ration table (/*0)" (/*0)" 2" +seudo +seudo-op -opcod codee table table (+*0) (+*0) 4" ite itera rall tab table le (0) (0) ;" oca ocati tion on coun counte terr"
.!mbol table:
/nemonic opcode table:
+seudo-opcode table (+*0)
iteral table(0)
#:
*pcode fields: i
'ddress
.tatement class
Department of Computer Engineering
*pcode
*perand
SYSTEM PROGRAMMING & COMPILER CONSTRUCTION
i"
code
*perand-2 fields i" *perand class:
.:s!mbol
:literals
ii" code
2-+ass 'ssembler: !ass"I
.epa .eparat ratee s! s!mb mbol ol33 mne mnemo moni nicc and and oper operan and d fie field lds" s"
"
uild the s!mbol table"
" "
#onst onstru rucct inte interm rmeediate iate co code" de"
!ass"II
Eval Evalua uate te fiel fields ds and and gene genera rate te code code""
"
+ro +roces cess pseu seudo-o do-opc pco odes des"
Algorithm: !ass"I
.tep: nitiali7e all variables:
oc
%
ittab%
.tep2: ,ead line()% rea-words()% .tep4: hile(opcode?=end) hile(opcode?=end) ,ead line()% rea-words()% case: if opcode=start or opcode=small model Department of Computer Engineering
SYSTEM PROGRAMMING & COMPILER CONSTRUCTION
oc
nsert
#ase4: if opcode="data
#ode=search
.i7e=search
oc
Benerate
nsert
#aseC: else when opcode is imperative i
code code=s =sea earc rch< h
i"
eng ength th=s =sea earc rch< h
ii" ii"
#hec hec
f !es:
nsert
f no:
9ind operand enr! in ./0' Benerate #(.3code)(.3entr!-no)% oc
code code=s =sea earc rch< h
i"
eng ength th=s =sea earc rch< h
Department of Computer Engineering
statement
SYSTEM PROGRAMMING & COMPILER CONSTRUCTION
ii" ii"
oc
.tep;: stop"
Pr)ram Pr)ram )r 2 pa"" A""em$!er" (n&!#+e6"t+()*37 #include #include void chk_label(); void chk_opcode(); void READ_!"E(); struct optab char
code$%&'obcode$%&'; code$%&'obcode$%&';
*+,optab$-'
/DA//&&/*
/012//&%/*
/34A//&5/* *;
struct s,+tab char s,+bol$%&'; int addr;
*+,s,+tab$%&';
int startaddrlocctrs,+count& startaddrlocctrs,+count&length; length; char line$5&'label$6'opcode$6' line$5&'label$6'opcode$6'operand$6'prog operand$6'progra+na+e$%&'; ra+na+e$%&'; 78A33E19ER 2A33 % 87 void 2A33%()
Department of Computer Engineering
SYSTEM PROGRAMMING & COMPILER CONSTRUCTION :!E 8input8inter; inputopen(/input.tt//r/); interopen(/inter.tt//=/); print(/?A4!" print(/?A4!" A9E@t2ERA"D@t2?DE@n/); A9E@t2ERA"D@t2?DE@n/);
print(/_____________________________________/); gets(line5&input);
READ_!"E();
i(strc+p(opcode/34AR4/))
startaddratoi(operand);
locctrstartaddr; strcp,(progra+na+elabel);
print(inter/Bs/line);
gets(line5&input); *
else
progra+na+e$&'C@&C;
startaddr&;
locctr&; * print(/@n Bd@t Bs@tBs@t Bs/locctrlabelopcodeoperan Bs/locctrlabelopcodeoperand); d);
=hile(strc+p(line/E"D/)&)
READ_!"E(); print(/@n Bd@t Bs @tBs@t Bs/locctrlabelopcodeopera Bs/locctrlabelopcodeoperand); nd);
Department of Computer Engineering
SYSTEM PROGRAMMING & COMPILER CONSTRUCTION
i(label$&'C@&C)chk_label();
chk_opcode(); print(inter/Bs print(inter/Bs Bs Bs@n/labelopcodeoperand); Bs@n/labelopcodeoperand);
gets(line5&input); *
print(/@n Bd@t@tBs/locctrline); Bd@t@tBs/locctrline);
print(inter/Bs/line);
close(inter); close(input); * 78Asses+bler pass 5 87 void 2A335() :!E 8inter8output; 8inter8output; char record$-&'part$6'value$' record$-&'part$6'value$';782art ;782art arra, =as deined as part$' previousl,87 int currttlen&oundopcodeou currttlen&oundopcodeoundoperandchk ndoperandchkoperandaddrreca operandaddrrecaddr&; ddr&;
interopen(/inter.tt//r/);
outputopen(/output.tt//=/);
gets(line5&inter);
READ_!"E(); i(strc+p(opcode/34AR4/)) i(strc+p(opcode/34AR4/)) gets(line5&inter); gets(line5&inter); print(/@n@n?orresponding print(/@n@n?orresponding bect code is..@n/); print(/@nFG Bs G Bd G Bd /progra+na+estartaddrlen /progra+na+estartaddrlength); gth); print(output/@nFG print(output/@nFG Bs G Bd G Bd /progra+na+estartaddrleng /progra+na+estartaddrlength); th); recaddrstartaddr; recaddrstartaddr; record$&'C@&C; record$&'C@&C;
=hile(strc+p(line/E"D/)&)
Department of Computer Engineering
SYSTEM PROGRAMMING & COMPILER CONSTRUCTION
operandaddroundoperandoundopcode&; value$&'part$&' value$&'part$&' C@&C;
READ_!"E();
or(chk&;chk<-;chkHH)
i(strc+p(opcode+,optab$chk'.code)) oundopcode%;
strcp,(part+,optab$chk'.obcode);
i(operand$&'C@&C)
or(chk&;chk
i(strc+p(+,s,+tab$chk'.s,+boloperand))
itoa(+,s,+tab$chk'.addrvalue%&);
strcat(partvalue);
oundoperand%; *
i(oundoperand)strcat(part/err/); * * *
i(oundopcode)
i(strc+p(opcode/9I4E/)& i(strc+p(opcode/9I4E/)& JJ strc+p(opcode/KRD/)JJ strc+p(opcode/KRD/)JJ strc+p(opcode/RE39/))
strcat(partoperand); * *
Department of Computer Engineering
SYSTEM PROGRAMMING & COMPILER CONSTRUCTION i((currttlenHstrlen(part))< i((currttlenHstrlen(part))<6) 6) 784his step =as having buer overlo= issue since part$' =as deined previousl, =hich i corrected to part$6'. 9ecause o this irst t=o b,tes o stack are getting lost87
strcat(record/G/);
strcat(recordpart);
currttlenHstrlen(part); *
else print(/@n4G Bd GBd Bs/recaddrcurrttlenrecord) Bs/recaddrcurrttlenrecord); ; print(output/@n4G print(output/@n4G Bd GBd Bs/recaddrcurrttlenrecord Bs/recaddrcurrttlenrecord); );
recaddrHcurrttlen;
currttlenstrlen(part);
strcp,(recordpart); *
gets(line5&inter); * print(/@n4G Bd GBd Bs/recaddrcurrttlenrecord); Bs/recaddrcurrttlenrecord); print(output/@n4G print(output/@n4G Bd GBd Bs/recaddrcurrttlenrecord Bs/recaddrcurrttlenrecord); ); print(/@nEG Bd@n/startaddr); Bd@n/startaddr); print(output/@nEG print(output/@nEG Bd@n/startaddr); Bd@n/startaddr);
close(inter);
close(output);
*
void READ_!"E() char bu$6'=ord%$6'=ord5$6'= bu$6'=ord%$6'=ord5$6'=ord-$6'; ord-$6';
Department of Computer Engineering
SYSTEM PROGRAMMING & COMPILER CONSTRUCTION int i&count&; label$&'opcode$&'operand$&' label$&'opcode$&'operand$&'=ord%$&'=ord5 =ord%$&'=ord5$&'=ord-$&'C@& $&'=ord-$&'C@&C; C; or(i&;line$i'C@&C;iHH) or(i&;line$i'C@&C;iHH) i(line$i'C i(line$i'C C) bu$HH'line$i'; else bu$'C@&C; strcp,(=ord-=ord5); strcp,(=ord5=ord%); strcp,(=ord%bu); &; countHH; * * bu$L%'C@&C; strcp,(=ord-=ord5); strcp,(=ord5=ord%); strcp,(=ord%bu); s=itch(count) case &Mstrcp,(opcode=ord%); &Mstrcp,(opcode=ord%); break; case %Mstrcp,(opcode=ord5);strcp %Mstrcp,(opcode=ord5);strcp,(operand=ord ,(operand=ord%);* %);* break; case 5M strcp,(label=ord-);strcp,(o strcp,(label=ord-);strcp,(opcode=ord5);s pcode=ord5);strcp,(operand trcp,(operand=ord%);* =ord%);* break; * * Department of Computer Engineering
SYSTEM PROGRAMMING & COMPILER CONSTRUCTION
void chk_label() int kdups,+&;
or(k&;k
i(strc+p(label+,s,+tab$k'.s,+bol))
+,s,+tab$k'.addrL%;
dups,+%;
break; *
i(dups,+)
strcp,(+,s,+tab$s,+count'.s,+bollabel);
+,s,+tab$s,+countHH'.addrlocctr; *
*
void chk_opcode() int k&ound&;
or(k&;k<-;kHH)
i(strc+p(opcode+,optab$k'.code))
locctrH-;
ound%;
break; *
i(ound)
Department of Computer Engineering
SYSTEM PROGRAMMING & COMPILER CONSTRUCTION i(strc+p( opcode/KRD/)) opcode/KRD/)) locctrH-; else i (strc+p(opcode/RE3K/))locc (strc+p(opcode/RE3K/))locctrH(-8atoi(oper trH(-8atoi(operand)); and)); else i(strc+p(opcode/RE39/))l i(strc+p(opcode/RE39/))locctrHatoi(op occtrHatoi(operand); erand); * *
int +ain()
2A33%();
lengthlocctrLstartaddr;
2A335(); getch();
F
#onclusion: 0hus we have implemented 2 pass assemblers for X86 machine"
Experiment No.$
8a&r) pr)&e"")r 2 pa"" 8a&r) pr)&e"")r* pr)&e"")r*
Title: Design and mplementation of /acro +rocessor" Theory: ' macro instruction is a notational convenience for the programmer" t allows the programmer to write shorthand version of a program ( module programming )" 0he macro processor processor replaces replaces each each macro macro invocation invocation with the correspon corresponding ding seGuence seGuence of
Department of Computer Engineering
SYSTEM PROGRAMMING & COMPILER CONSTRUCTION
statements (expanding)" /acro processor processor recogni7e recogni7ess macro macro definition definitions3 s3 save the macro macro definition3 recogni7e macro calls3 e1pand macro calls"
%acro !rocessor
Source Code (with macro)
obj Macro ProcessorExpanded C Code ompiler or Assembler
,ecogni7e macro definitions
.ave the macro definition
,ecogni7e macro calls
E1pand macro calls
Examples: /acro
.tart of the definition
ncr
/acro name
/ov a13>
.eGuence to be abbreviated
'dd a13> /end
End of definition
!arameteri&e' %acro macro Hlable incr Harg3 Harg2
Department of Computer Engineering
SYSTEM PROGRAMMING & COMPILER CONSTRUCTION
mov a13Harg Hlable add a13 Harg2 sub arg23 In7 Hlable /end
#alll to this macro definition will be: loop incr var3var2
#on'itional %acro macro @ar! Hcount3 Harg3Harg23Harg4 'dd a13 Harg '9 (Hcount EJ )"9
0est if Hcount=
'dd a13Harg2 '9 (Hcount EJ 2)"9
0est if Hcount=
'dd a13Harg4 "9 mend
abel start with (") and do not appear in the output"
'B* is unconditional branch
'9 is conditional branch
f #all to this macro is: @ar! 43var3var23var4 E1pansion will be:
Department of Computer Engineering
SYSTEM PROGRAMMING & COMPILER CONSTRUCTION
'dd a13var 'dd a13var2 'dd a13var4
f call is: @ar! 23 var3var2 E1pansion will be: 'dd a13var 'd a13var2
Neste' %acro
/acro calls within macro /acro 'dd Harg /ov a13Harg2 'dd a13 /end
/acro 'dds Harg3Harg2 'dd Harg 'dd Harg2 " /acro nstruction defining macros: macro
Department of Computer Engineering
SYSTEM PROGRAMMING & COMPILER CONSTRUCTION
define Hsub macro Hsub H! mov a13H! sub a13> mend mend
(ecursi)e %acro macro ncr Harg /ov a13 Harg 'dd a13 '9 (Harg EJ >) " ncr arg " mend
esign of simple macro processor "small model %acro
'# Ha3 Ha2 /ov a13 Ha 'dd a13Ha2 %en'
"data Department of Computer Engineering
SYSTEM PROGRAMMING & COMPILER CONSTRUCTION
@ar db > "code A*# >32>
end
Expan'e' co'e will be: "small model "data @ar db > "code /ov a13> 'dd a132> end
Algorithm !ass +:
oth oth counter counterss for /D0 /D0 and /0 /0 i"e" i"e" /D0# /D0# and and /0# /0# are are initial initiali7e i7ed d first" " /acro /acro process process in first pass pass reads reads each and and ever! line line of source source code" code" 2" f it is macro macro pseudo opcode3 opcode3 the entire entire macro definition definition that follows is saved in /D0 4" 0he macr macro o name name is ente entered red in /0 /0" ;" hen /ED /ED pseudo pseudo opcode opcode is encounte encountered red that that means means all macro macro definition definition have been processed3 so transfer control to pass2"
Department of Computer Engineering
SYSTEM PROGRAMMING & COMPILER CONSTRUCTION
!ass 2:
" ,ead source source code code supplied supplied b! passto passto see opcode opcode mnemonic mnemonic of of each line line is present in /0 or not" 2" hen call call is found3 found3 the /D0+ /D0+ is is set to correspond corresponding ing macro macro definition definition stored in /D0" /D0" 4" 0he positional positional vs actual actual paramete parameterr list is prepared prepared"" Each line line from /D0 /D0 is read and positional parameter is replaced b! actual parameter" parameter" ;" .cannin .canning g of input input file file contin continues" ues" C" hen /ED /ED pseudo pseudo output output is countered countered33 /D0 terminates terminates e1pansio e1pansion n of 6" macro" K" hen ED ED pseudo pseudo output is encountered encountered33 e1pansion e1pansion code code is transfer transferred red to assembler for further processing"
Pr)ram Binc!udestdiohD Binc!udeconiohD Binc!udest"in#hD Binc!udestd!ihD >oid main;< :ILE F-0=F-6=F-1cha" mneH63=o$ndH63=!aH63c!"sc";< -0J-o$en;Kmin$6t/tK=K"K<
Department of Computer Engineering
SYSTEM PROGRAMMING & COMPILER CONSTRUCTION -6J-o$en;Knta6t/tK=K5K< -1J-o$en;Kdta6t/tK=K5K< -scan-;-0=KsssK=!a=mne=o$nd< 5hi!e;st"cm$;mne=KMEN,K<J3<i-;st"cm$;mne=KMACROK
Inp#t (!e minp2.txt
EX
/'#,* H'3H
-
D'
H'
-
.0'
H
Department of Computer Engineering
SYSTEM PROGRAMMING & COMPILER CONSTRUCTION
minp2.txt
-
/ED
-
.'/+E .0',0
>>>
-
EX
32
,E.
2
,E.
-
ED
-
O#tp#t (!e" 'tab2.txt
EX
H'3H
D'
H'
.0'
H
/ED
ntab2.txt
EX
#onclusion: 0hus we have implemented /acro +rocessor"
E9PERI8ENT NO* 4
Le'(&a! ana!:er t))! !e'
T(t!e De"(n a Le'(&a! Ana!:er )r a !an#ae 3)"e rammar (" ;n)n*
Department of Computer Engineering
SYSTEM PROGRAMMING & COMPILER CONSTRUCTION
T3e)r Str#&t#re ) a !e' (!e
The st"uctu"e o- a !e/ -i!e is intentiona!!y simi!a" to that o- a yacc -i!e -i!es a"e di>ided u$ into th"ee sections= se$a"ated y !ines that contain on!y t5o $e"cent si#ns= as -o!!o5s%
Definition section
%% Rules
section
%% C code section
•
•
•
The +e(n(t()n section is the $!ace to de-ine mac"os mac"os and and to im$o"t heade" -i!es 5"itten in C It is a!so $ossi!e to 5"ite any C code he"e= 5hich 5i!! e co$ied >e"atim into the #ene"ated sou"ce -i!e The r#!e" section is the most im$o"tant section it associates $atte"ns 5ith C statements Patte"ns a"e sim$!y "e#u!a" e/$"essions statements e/$"essions Qhen the !e/e" sees some te/t in the in$ut matchin# a #i>en $atte"n= it e/ecutes the associated C code This is the asis o- ho5 !e/ o$e"ates The C &)+e section contains C statements and -unctions that a"e co$ied >e"atim to the #ene"ated sou"ce -i!e These statements $"esuma!y contain code ca!!ed y the "u!es in the "u!es section In !a"#e $"o#"ams it is mo"e con>enient to $!ace this code in a se$a"ate -i!e and !in. it in at com$i!e com$i!e time time
Pr)ram
<<
Binc!ude stdiohD int cha"countJ3=5o"dcntJ3=s$acecntJ3=!inecntJ3 Department of Computer Engineering
SYSTEM PROGRAMMING & COMPILER CONSTRUCTION QOR,Htn=% *F-o" 5o"dF* EOLHn *F -o" end o- !ine F* LANH *F -o" 5o"d F*
*FFF The "u!e section to identi-y 5o"ds=s$ace= and !inesFFF* *F matches 5o"d and counts 5o"d and cha"acte"s F* 5o"d5o"dcntcha"cntJcha"cntyy!en# LAN s$acecnt *Fidenti-y end o- !ine and count !ine F* EOL!inecnt *F i#no"e a!! othe" and count cha"acte" F* cha"cnt *F ca!!ed y !e/ 5hen in$ut is e/haustedRetu"n 0 i- you a"e done o" 3 i- mo"e $"ocessin# is "eui"edF* int yy5"a$;< "etu"n 0 *F The C code sectionF* int main;int a"#c=cha" Fa"#>H< *F i- on!y one a"#ument not $assed to the $"o#"amF* i-;a"#cJ6< *F out$ut usa#e -o"mat o- the $"o#"am and e/itF* $"int ;Usa#e%*aoutDsou"ce-i!eDnV< e/it;3< Department of Computer Engineering
SYSTEM PROGRAMMING & COMPILER CONSTRUCTION yyinJ-o$en;a"#>H0=V"V<*F"ead in$utF* yy!e/;< *F ca!! !e/e" F* *F $"ime counts F* $"int;tota! nume" o- cha"acte"sJd Tota! Tota! Nume" o- 5o"dJdn Tota! Tota! nume" os$acesJdn Tota! Nume" o- !inesJdV "etu"n 3
#onclusion: 0hus we have implemented simple parser using !acc and le1"
Department of Computer Engineering
SYSTEM PROGRAMMING & COMPILER CONSTRUCTION
E9PERI8ENT NO* 5
Par"er enerat)r t))! Ya&&*
T(t!e De"(n an+ Imp!ementat()n ) "(mp!e Par"er #"(n Le' Ya&&* T3e)r Yacc Yacc is the Uti!ity 5hich #ene"ates the -unction Wyy$a"seW 5hich is indeed the Pa"se" Yacc Yacc desc"ies a conte/t -"ee = LALR;0< #"amma" and su$$o"ts oth ottom)u$ and to$)do5n $a"sin#The #ene"a! -o"mat -o" the YA YACC -i!e is >e"y simi!a" to that o- the Le/ -i!e 0 ,ec!a"ations 6 G"amma" Ru!es 1 Su"outines >a"(& Spe&((&at()n" Names "e-e" to eithe" to.ens o" nonte"mina! symo!s Yacc Yacc "eui"es to.en names to e dec!a"ed as such In addition= -o" "easons discussed in Section 1= it is o-ten desi"a!e to inc!ude the !e/ica! ana!y8e" as $a"t o- the s$eci-ication -i!e it may e use-u! to inc!ude othe" $"o#"ams as 5e!! Thus= e>e"y s$eci-ication -i!e consists o- th"ee sections% the dec!a"ations= ;#"amma"< "u!es= and $"o#"ams The sections a"e se$a"ated y dou!e $e"cent XXWW ma".s ;The $e"cent XXWW is #ene"a!!y used in Yacc s$eci-ications as an esca$e cha"acte"< cha"ac te"<
In othe" 5o"ds= a -u!! s$eci-ication -i!e !oo.s !i.e
declarations %%
rules %%
programs
Department of Computer Engineering
SYSTEM PROGRAMMING & COMPILER CONSTRUCTION The dec!a"ation section may e em$ty Mo"eo>e"= i- the $"o#"ams section is omitted= the second ma". may e omitted a!so thus= the sma!!est !e#a! Yacc Yacc s$eci-ication is %%
Rules
Pr)ram /+E/E0 /+ E/E0 ' '0, '0, 'B'BE 'B' BE +',.E, .B EX H '## EX 9E LM N,ecognising parts of speechN 5include O!"tab"hO LF @E, OisOPOamOPOareOPOwereOPOwasOPObeOPObeingOPObeenOPOdoOPOdoesOPOdidOPOwillOPOhasOPOhaveOPOha dO 'D@E, Over!OPOsimpl!OPOgentl! Over!OPOsimpl!OPOgentl!OPOGuietl!OPOcalml!OPOangril!O OPOGuietl!OPOcalml!OPOangril!O +,** OsheOPOweOPOthe!OPOOPO!ouOPOheOPOitO ',0#E ',0#E OaOPOanOPOtheO LL M@E,F
Mprintf(OQnverbO)% return(@E,)% F
M'D@E,F Mprintf(OQnadverbO)% return('D@E,)% F M+,**F Mprintf(OQnpronounO)%
Department of Computer Engineering
SYSTEM PROGRAMMING & COMPILER CONSTRUCTION
return(+,**)% F M',0# M',0#EF EF Mprint Mprintf(OQ f(OQnar nartic ticleO leO)% )% return(',0#E)% F Ra-7' -7'-STA
Mprintf(OQn OQnnounO)% return(*)% F
LL '## '## 9E 9 E LM N0o recognise english sentencesN 5includeUstdio"hV LF Ltoen * +,** @E, 'D@E, ',0#E LL sentence: subIect @E, obIect Mprintf(OQn.entence is validO)%F % subIect: ',0#E * P * P ',0#E +,** P +,** % obIect: ',0#E * P * %
Department of Computer Engineering
SYSTEM PROGRAMMING & COMPILER CONSTRUCTION
LL e1tern 9E N!!in% main() M doM !!parse()% Fwhile(?feof(!!in))% F !!error(s) M fprintf(stderr3OLsQnO3s)%F
*0+0 RrootWlocalhost assign4T5 le1 natural"l RrootWlocalhost assign4T5 !acc -d natural"! RrootWlocalhost assign4T5 gcc le1"!!"c !"tab"c -lfl RrootWlocalhost assign4T5 "a"out ,am is a bo! noun verb article noun
Department of Computer Engineering
SYSTEM PROGRAMMING & COMPILER CONSTRUCTION
.entence is valid
#onclusion: 0hus we have implemented simple parser using !acc and le1"
Department of Computer Engineering
SYSTEM PROGRAMMING & COMPILER CONSTRUCTION
Experiment No.,
F(n+ (r"t?@ , )!!)?@ "et ) (=en rammar
T(t!e F(n+ (r"t?@,)!!)?@ "et ) (=en rammer* rammer*
Theory:
'utoma 'utomatic tic techni techniGue Guess for constru constructi cting ng parser parserss start start with with comput computing ing some basic functions for s!mbols in the grammar" 0hese functions are usef us eful ul in unde unders rsta tand ndin ing g both both recu recurs rsiv ivee descen descentt and and bott bottom om-u -up p , parsers" First(a) 9irst(a) is the set of terminals that begin strings derived from a3 which can include epsilon"
" 2" 4" ;"
9irst(X) 9irst(X) starts starts with with the empt! set" if X is a terminal3 terminal3 9irst(X 9irst(X)) is MXF" MXF" if X -V epsilon is a producti production3 on3 add epsilo epsilon n to 9irst(X)" 9irst(X)" if X is a non-term non-terminal inal and and X -V -V 2 """ is a production3 add 9irst( ) to 9irst(X)"
. or (i %; i Ii can derive epsilon; iHH) add :irst(IiH%) to :irst(N)
Department of Computer Engineering
SYSTEM PROGRAMMING & COMPILER CONSTRUCTION Follow(A) 9ollow(') for nonterminal ' is the set of terminals that can appear immediatel! to the right of ' in some sentential form . -V a'1""" 0o compute 9ollow3 appl! these rules to all nonterminals in the grammar:
" 'dd to 9ollow 9ollow(.) (.) 2" if ' -V ab then then add 9irst(b) 9irst(b) - epsilon epsilon to 9ollow() 9ollow() 4" if ' -V a or ' -V ab where epsilon epsilon is in 9irst(b)3 then add add 9ollow(') to 9ollow()"
!rogram: # code to 9ind 9irst and 9ollow in a given Brammar N # program to find 9irst and 9ollow in a given Brammar" N 5includeUstdio"hV 5includeUstring"hV int i3I3l3m3n=>3o3p3nv37=>31=>% i3I3l3m3n=>3o3p3nv37=>31=>% char strR>T3temp3temp2R>T3temp4R2>T3Nptr% strR>T3temp3temp2R>T3temp4R2>T3Nptr% struct prod M char lhsR>T3rhsR>TR>T3ftR>T3folR>T% int n% FproR>T% void findter() M int 3t% for(=>%Un%AA) M if(temp==proRT"lhsR>T) M for(t=>%tUproRT"n%tAA) M if( proRT"rhsRtTR>TU6C PP proRT"rhsRtTR>TVY> ) proRiT"ftRstrlen(proRiT"ft)T=proRT"rhsRtTR>T% else if( proRT"rhsRtTR>TV=6C HH proRT"rhsRtTR>TU=Y> ) M temp=proRT"rhsRtTR>T% if(temp==Z.Z) proRiT"ftRstrlen(proRiT"ft)T=Z5Z% findter()% F F brea% F F Department of Computer Engineering
SYSTEM PROGRAMMING & COMPILER CONSTRUCTION
void findfol() M int 3t3p3o3ch% char Nptr% for(=>%Un%AA) M ch=>% for(t=>%tUproRT"n%tAA) M ptr=strchr(proRT"rhsRtT3temp)% if( ptr ) M p=ptr-proRT"rhsRtT% if(proRT"rhsRtTRpATV=6C HH proRT"rhsRtTRpATU=Y>) M for(o=>%oUn%oAA) if(proRoT"lhsR>T==proRT"rhsRtTRpAT) M strcat(proRiT"fol3proRoT"ft)% chAA% F F else if(proRT"rhsRtTRpAT==ZQ>Z) M temp=proRT"lhsR>T% if(proRlT"rhsRITRpT==temp) continue% if(temp==Z.Z) strcat(proRiT"fol3OO)% findfol()% chAA% F else M proRiT"folRstrlen(proRiT"fol)T=proRT"rhsRtTRpAT% chAA% F F F if(chV>) brea% F F void main() M 9E Nf% clrscr()% for(i=>%iU>%iAA) Department of Computer Engineering
SYSTEM PROGRAMMING & COMPILER CONSTRUCTION
proRiT"n=>% f=fopen(OtabC"t1tO3OrO)% while(?feof(f)) M fscanf(f3OLsO3proRnT"lhs)% if(nV>) M if( strcmp(proRnT"lhs3proRn-T"lhs) == > ) M proRnT"lhsR>T=ZQ>Z% fscanf(f3OLsO3proRn-T"rhsRproRn-T"nT)% proRn-T"nAA% continue% F F fscanf(f3OLsO3proRnT"rhsRproRnT"nT)% proRnT"nAA% nAA% F printf(OQnQn0[E B,'//', . '. 9**.QnQnO)% for(i=>%iUn%iAA) for(I=>%IUproRiT"n%IAA) printf(OLs -V LsQnO3proRiT"lhs3proRiT"rhsRIT)% proR>T"ftR>T=Z5Z% for(i=>%iUn%iAA) M for(I=>%IUproRiT"n%IAA) M if( proRiT"rhsRITR>TU6C PP proRiT"rhsRITR>TVY> ) M proRiT"ftRstrlen(proRiT"ft)T=proRiT"rhsRITR>T% F else if( proRiT"rhsRITR>TV=6C HH proRiT"rhsRITR>TU=Y> ) M temp=proRiT"rhsRITR>T% if(temp==Z.Z) proRiT"ftRstrlen(proRiT"ft)T=Z5Z% findter()% F F F printf(OQnQn9,.0QnO)% for(i=>%iUn%iAA) M printf(OQnLs -V O3proRiT"lhs)% for(I=>%IUstrlen(proRiT"ft)%IAA) M for(l=I-%lV=>%l--) Department of Computer Engineering
SYSTEM PROGRAMMING & COMPILER CONSTRUCTION
if(proRiT"ftRlT==proRiT"ftRIT) brea% if(l==-) printf(OLcO3proRiT"ftRIT)% F F for(i=>%iUn%iAA) temp2RiT=proRiT"lhsR>T% proR>T"folR>T=ZZ% for(i=>%iUn%iAA) M for(l=>%lUn%lAA) M for(I=>%IUproRiT"n%IAA) M ptr=strchr(proRlT"rhsRIT3temp2RiT)% if( ptr ) M p=ptr-proRlT"rhsRIT% if(proRlT"rhsRITRpATV=6C HH proRlT"rhsRITRpATU=Y>) M for(o=>%oUn%oAA) if(proRoT"lhsR>T==proRlT"rhsRITRpAT) strcat(proRiT"fol3proRoT"ft)% F else if(proRlT"rhsRITRpAT==ZQ>Z) M temp=proRlT"lhsR>T% if(proRlT"rhsRITRpT==temp) continue% if(temp==Z.Z) strcat(proRiT"fol3OO)% findfol()% F else proRiT"folRstrlen(proRiT"fol)T=proRlT"rhsRITRpAT% F F F F printf(OQnQn9**QnO)% for(i=>%iUn%iAA) M printf(OQnLs -V O3proRiT"lhs)% for(I=>%IUstrlen(proRiT"fol)%IAA) M for(l=I-%lV=>%l--) if(proRiT"folRlT==proRiT"folRIT) Department of Computer Engineering
SYSTEM PROGRAMMING & COMPILER CONSTRUCTION
brea% if(l==-) printf(OLcO3proRiT"folRIT)% F F getch()% F
Input -ile -or -irst an' -ollow !rogram:
. 'E .a 'p 't 'G .f 'w
#onclusion: 0hus we have implemented 9ind 9irst and 9ollow in a given Brammar
E9PERI8ENT NO*
Code Gene"ation a!#o"ithm
T(t!e St#+ ) &)+e enrat()n a!)r(t3m* T3e)r In computing= code #ene"ation is the $"ocess y 5hich a com$i!e"Ws code #ene"ato" con>e"ts some inte"mediate "e$"esentation o- sou"ce code into a -o"m ;e#= machine code< that can e "eadi!y e/ecuted y a machine
.ophisticated compilers t!picall! perform multiple passes over various intermediate forms" 0his multi-stage process is used because man! algorithms for code optimi7ation are easier to appl! one at a time3 or because the input to one optimi7ation relies on the completed processing performed b! another optimi7ation" 0his organi7ation organi7ation also facilitates the creation of a single compiler that can target multiple architectures3 as onl! the last of the code generation stages (the backend) needs to change from target to target" (9or more information on compiler design3 see #ompiler") #ompiler") Department of Computer Engineering
SYSTEM PROGRAMMING & COMPILER CONSTRUCTION
0he input to the code generator t!picall! consists of a parse tree or an abstract s!nta1 tree" 0he tree is converted into a linear seGuence of instructions3 usuall! in an intermediate language such as three address code" 9urther stages of compilation ma! or ma! not be referred to as Ocode generationO3 depending on whether the! involve a significant change in the representation of the program" (9or e1ample3 a peephole optimi7ation pass would not liel! be called Ocode generationO3 although a code generator might incorporate a peephole optimi7ation pass")
8a%)r ta";" (n &)+e enerat()n n addition to the basic conversion from an intermediate representation into a linear seGuence of machine instructions3 a t!pical code generator tries to optimi7e the generated code in some wa!" 0ass which are t!picall! part of a sophisticated compilerZs Ocode generationO phase include: • • • •
nstruction selection: which instructions to use" nstruction scheduling: in which order to put those instructions" .cheduling is a speed optimi7ation that can have a critical effect on pipelined machines" ,egister allocation: the allocation of variables to processor registersRT Debug data generation if reGuired so the code can be debugged"
nstruction selection is t!picall! carried out b! doing a recursive postorder traversal on the abstract s!nta1 tree3 matching particular tree configurations against templates% for e1ample3 the tree KM ADD(N1O(IP)) might be transformed into a linear seGuence of instructions b! recursivel! generating the seGuences for t%M N and t5M 1O(IP)3 and then emitting the instruction ADD K t% t5" n a compiler that uses an intermediate language3 there ma! be two instruction selection stages \ one to convert the parse tree into intermediate code3 and a second phase much later to convert the intermediate code into instructions from the instruction set of the target machine" 0his second phase does not reGuire a tree traversal% it can be done linearl!3 linearl!3 and t!picall! involves a simple replacement of intermediate-language operations with their corresponding opcodes" opcodes" [owever3 if the compiler is actuall! a language translator (for e1ample3 one that converts Eiffel to #)3 then the second code-generation code-generation phase ma! involve building a tree from the linear intermediate code"
(ole of co'e generator in a compiler
0he final phase in our compiler model is the code generator" t taes as input an intermediate representation of the source program and produces as output an eGuivalent target program"0he reGuirements traditionall! imposed on a code generator are severe" 0he output code must be correct and of high Gualit!3 Gualit!3 meaning that it should mae effective use of the resources of the target machine" /oreover3 the code generator itself should run efficientl!"
Department of Computer Engineering
SYSTEM PROGRAMMING & COMPILER CONSTRUCTION
ISSBES IN THE DESIGN OF CODE GENRATION GENRATION Input
]three address presentations presentations (Guadruples3 (Guadruples3 triples3 """) ]@irtual ]@irtual machine presentations presentations (b!tecode3 (b!tecode3 stac-machine3 stac-machine3 """) ]inear presentation presentation (postfi13 """) ]Braphical presentation presentation (s!nta1 trees3 trees3 D'Bs3""") Taret Taret pr)ram
-nstruction set architecture (,.#3 #.#) ]+roducing absolute absolute machine-anguage machine-anguage program ]+roducing relocatable relocatable machine-language machine-language program ]+roducing assembl! assembl! language programs Instruction Instruction Selection
0he comple1it! of mapping , program into code-seGuence for target machine depends on: ]evel of , (high-level (high-level or low-level) ]ature of instruction set set (data t!pe support) support) ]Desired Gualit! of generated generated code (speed (speed and si7e) si7e) (egister Allocation
^.electing the set of variables that will reside in registers at each point in the program (egister Assignment
^+icing the specific register that a variable will reside in E)aluation r'er
.electing the order in which computations are performed / .electing
Department of Computer Engineering
SYSTEM PROGRAMMING & COMPILER CONSTRUCTION
]'ffects the efficienc! efficienc! of the target target code ]+icing a best best order is +-complete ].ome orders reGuire reGuire fewer registers than others others
C)n&!#"()n Thus 5e ha>e studied code #ene"ation a!#o"ithm
E9PERI8ENT NO*
C)+e Opt(m(:at()n te&3n(#e"*
T(t!e Implementation Implementation of co'e optimi&ation techni0ues *
T3e)r PEEPHOLE OPTI8IATION
A stateme statement) nt)y) y)sta stateme tement nt code)# code)#ene ene"ati "ations ons st"ate#y st"ate#y o-ten o-ten $"oduc $"oducee ta"#et ta"#et code code that that contains "edundant inst"uctions inst"uctions and suo$tima! suo$tima! const"ucts The ua!ity ua!ity o- such ta"#et ta"#et code can e im$"o>ed y a$$!yin# o$timi8in#V t"ans-o"mations to the ta"#et $"o#"am A sim$!e ut e--ecti>e techniue -o" im$"o>in# the ta"#et code is peephole optimization= optimization = a method -o" t"yin# to im$"o>in# the $e"-o"mance o- the ta"#et $"o#"am y e/aminin# a sho"t seuence o- ta"#et inst"uctions ;ca!!ed the $ee$ho!e< and "e$!acin# these inst"uctions y a sho"te" o" -aste" seuence= 5hene>e" $ossi!e The $ee$ho!e is a sma!!= mo>in# 5indo5 on the ta"#e ta"#ett $"o#"a $"o#"am m The The code code in the $ee$ho $ee$ho!e !e need need not conti#uou conti#uous= s= a!thou a!thou#h #h some some im$!em im$!ement entatio ations ns do "eui" "eui"ee this this Qe sha!! sha!! #i>e #i>e the -o!!o5 -o!!o5in# in# e/am$!e e/am$!ess o- $"o#"a $"o#"am m t"ans-o"mations that a"e cha"acte"istic o- $ee$ho!e o$timi8ations% Redundant)inst"uctions e!imination :!o5)o-)cont"o! o$timi8ations A!#e"aic sim$!i-ications Use o- machine idioms
RE,UNTANT LOA,S AN, STORES Department of Computer Engineering
SYSTEM PROGRAMMING & COMPILER CONSTRUCTION I- 5e see the inst"uctions seuence ;0< MO' R3=a ;6< ;6e" ;6< is e/ecuted ;0< 5i!! ensu"e that the >a!ue o- a is a!"eady in "e#iste" R3I- ;6< had a !ae! 5e cou!d not e su"e that ;0< 5as a!5ays e/ecuted immediate!y e-o"e ;6< and so 5e cou!d not "emo>e ;6<
UNREAC+ALE CO,E% Anot Anothe he"" o$$o o$$o"t "tun unity ity -o" -o" $ee$ $ee$ho ho!e !e o$tim o$timi8a i8atio tions ns is the the "emo "emo>a! >a! o- un"ea un"each cha a!e !e inst"uctions An un!ae!ed inst"uction immediate!y -o!!o5in# an unconditiona! (um$ may e "emo>ed This o$e"ation can e "e$eated to e!iminate a seuence o- inst"uctions :o" e/am$!e= -o" deu##in# $u"$oses= a !a"#e $"o#"am may ha>e 5ithin it ce"tain se#ments that a"e e/ecuted on!y i- a >a"ia!e +e$# is 0In C= the sou"ce code mi#ht !oo. !i.e%
+e(ne +e$# 0 * I ? +e$# @ Pr(nt +e$#(n (n)rmat()n
In the inte"mediate "e$"esentations the i-)statement may e t"ans!ated as%
I +e$# 1 )t) L2 G)t) L2 L1 pr(nt +e$#(n (n)rmat()n L2?a@
One o>ious $ee$ho!e o$timi8ation is to e!iminate ( um$s o>e" (um$s Thus no matte" 5hat the >a!ue o- +e$#= ;a< can e "e$!aced y% I +e$# J1 )t) Pr(nt +e$#(n (n)rmat()n
Department of Computer Engineering
SYSTEM PROGRAMMING & COMPILER CONSTRUCTION L2?$@
As the a"#ument o- the statement o- ;< e>a!uates to a constant tr#e it can e "e$!aced y I +e$# J0 )t) L2 Pr(nt +e$#(n (n)rmat()n L2 ?&@
As the a"#ument o- the -i"st statement o- ;c< e>a!uates to a constant t"ue= it can e "e$!aced y #oto 2" 0hen a!! the statement that $"int deu##in# aids a"e mani-est!y un"eacha!e and can e e!iminated one at a time
C)n&!#"()n Thus 5e ha>e im$!emented code o$timi8ation
Pr)ram &!a"" CO p#$!(& "tat(& =)(+ ma(n?Str(nK ar"@
(nt (,%,$,;0M Str(n )pKK=)(+,ma(n?@, , , , , , , , , , , , , , , p(,,.*14,,,r,,5,M, A,,p(,<,r,< A,,p(,<,r,<,r, ,r, , , , , , , , , M Str(n r"KKne Str(nK10K10M Str(n =M&3ar aM S"tem*)#t*pr(nt!n?pr)ram S"tem*)#t*pr(nt!n?pr)ram &)+e ("@M )r?(0M(65M(@
Department of Computer Engineering
SYSTEM PROGRAMMING & COMPILER CONSTRUCTION )r?%0M%6M%@ S"tem*)#t*pr(nt?)pK(K% S"tem*)#t*pr(nt?)pK(K% @M S"tem*)#t*pr(nt!n?@M
)r?(0M(65M(@ )r?%0M%6M%@ =)pK(K%M (?=*&)mpareT)?@0@ a)pK(K%1*&3arAt?0@M (?a74 a65@
r"K;K0)pK(K%-1M r"K;K1)pK(K%1M
S"tem*)#t*pr(nt!n?I+ent((erQt S"tem*)#t*pr(nt!n?I+ent((erQta!#e a!#e@M @M )r?(0M(6;M(@ )r?%0M%62M%@
Department of Computer Engineering
SYSTEM PROGRAMMING & COMPILER CONSTRUCTION S"tem*)#t*pr(nt?r"K(K%Qt@M S"tem*)#t*pr(nt!n?@M )r?(0M(65M(@ )r?%0M%6M%@ )r?$0M$6;M$@ K0*&)mpareT)?)pK(K%@0)pK(K%1*&)mpareT)?@0@ )pK(K%r"K$K1M S"tem*)#t*pr(nt!n?pr)ram S"tem*)#t*pr(nt!n?p r)ram &)+e ater &)+e )pt(m(:at()n@M )r?(0M(65M(@ )r?%0M%6M%@ S"tem*)#t*pr(nt?)pK(K% S"tem*)#t*pr(nt?)pK(K% @M S"tem*)#t*pr(nt!n?@M Department of Computer Engineering
(?r"K$
SYSTEM PROGRAMMING & COMPILER CONSTRUCTION
OBTPBT
pr)ram pr)ram &)+e (" =)(+ ma(n?@ p( .*14 , r 5 M A p( < r < r M I+ent((er
a!#e
p(
.*14
r
5
pr)ram pr)ram &)+e ater &)+e )pt(m(:at()n =)(+ ma(n?@ p( .*14 , r 5 M A .*14 < 5 < 5 M
C)n&!#"()n Thus 5e ha>e im$!emented o- code o$timi8ation techniues
Department of Computer Engineering
SYSTEM PROGRAMMING & COMPILER CONSTRUCTION
Department of Computer Engineering
SYSTEM PROGRAMMING & COMPILER CONSTRUCTION
E9PERI8ENT NO* 10
St#+ ) !+ &)mman+ (n Bn('L(n#'*
T(t!e St#+ ) !+ &)mman+ (n Bn('L(n#'*
T3e)r l' combines a number of obIect and archive files3 relocates their data and ties up s!mbol references" suall! the last step in compiling a program is to run l'" l' accepts iner #ommand anguage files written in a superset of '0H0Zs in Editor #ommand anguage s!nta13 to provide e1plicit and total control over the lining process"
0his man page does not describe the command language% see the l' entr! in /ino/3 or the manual ld: the B liner3 for full details on the command language and on other aspects of the B liner" 0his version of l' uses the general purpose 9D libraries to operate on obIect files" 0his allows l' to read3 combine3 and write obIect files in man! different formats---for e1ample3 #*99 or /a.out/" Different formats ma! be lined together to produce an! available ind of obIect file" 'side from its fle1ibilit!3 fle1ibilit!3 the B liner is more helpful than other liners l iners in providing diagnostic information" /an! liners abandon e1ecution immediatel! upon encountering an error% whenever possible3 l' continues e1ecuting3 allowing !ou to identif! other errors (or3 in some cases3 to get an output file in spite of the error)"
Department of Computer Engineering
SYSTEM PROGRAMMING & COMPILER CONSTRUCTION
0he B liner l' is meant to cover a broad range of situations3 and to be as compatible as possible with other liners" 's a result3 !ou have man! choices to control its behavior" behavior" OPTIONS
0he liner supports a plethora of command-line options3 but in actual practice few of them are used in an! particular conte1t" 9or instance3 a freGuent use of l' is to lin standard ni1 obIect files on a standard3 supported ni1 s!stem" *n such a s!stem3 to lin a file /hello.o/: ld Lo