compiler lab manuals
TRANSCRIPT
-
8/13/2019 Compiler Lab Manuals
1/40
PROGRAM - 1
Objective WAP a program to check a given word is akeyword or not .
Theory Keywords are the reserve words of thelanguage. In C , these C keywords have special meaning to
the C compiler , so the pro-grammar must e careful not to
use these words as identifiers such as variale names.
Every C word is classified as either a keyword or an identifier.All keywords have fixed meanins and these meanings cannote changed. Keywords serve as uilding locks for program
statements.
Algorithm 1. Ass!me an array "a #-dimensional$ in which we store the
keywords as A% & % &.#. 'cans the in(!t word which we want to check in )% &.
*. +ow com(are the strin stored in )% & with ivenkeywords of A% & % &.
,. f strin matchedhen Print / t is a keyword 0Else Print / t is not a eyword 0.
Program 2incl!de3stdio.h4
2incl!de3conio.h42incl!de3strin.h4void main"$5 clrscr"$6char a%7&%8&95:;ein:
-
8/13/2019 Compiler Lab Manuals
2/40
gets+*"
puts+*"
for+i&"i/"i00*
1
c&strcmp+a#i%,*"
if"c99>$5 (rintf":?n ' K23W456)*" reak"
!
c&'"
!
if+c&&'*printf+)(n748 A K23W456)*"
getch+*"
!
@9 :ere some words are used , further we enhance itaccording to our re;uirement 9
Output
Enter the strin =f ' EBOR
Enter the strin =)e
+O A EBOR
-
8/13/2019 Compiler Lab Manuals
3/40
PROGRAM 2
Objective - WAP 84 I I6278I?I25 A76 C47>8A78>
Theory dentifiers refer to the names of varia;les< f!nctions and arrays.hese are !ser-defined names and consist of a seD!ence of lettersand diits< with a letter as a first character. )oth !((ercase andlower case letters are (ermitted< altho!h lowercase letters arecommonly !sed. he !nderscore character is also (ermitted in
identifiers. t is !s!ally !sed as a link ;etween two words in lonidentifiers. Constants in C refer to fixed val!es that do not chaned!rin the exec!tion of a (roram. C s!((orts several ty(es ofconstants as = nteer constants< Real constants< 'inle characterconstants< 'trin constants .
Algorithm 1. oad the iven strin in an array " arr%1>& $ .#. Check the first word of the strin &6
-
8/13/2019 Compiler Lab Manuals
4/40
int i9>
-
8/13/2019 Compiler Lab Manuals
5/40
Output
Enter strin = Bas
Constant= #*dentifier== a;s
-
8/13/2019 Compiler Lab Manuals
6/40
PROGRAM 3
Objective-A program for acktrack parser for the grammar-
> -D cAdA -Daa
Theory n o(-down (arser< there is a well-known draw;ack
-
8/13/2019 Compiler Lab Manuals
7/40
Else ret!rn falseEnd
Program
2incl!de3conio.h42incl!de3stdio.h42incl!de3iostream.h42incl!de3strin.h4int (t9>
-
8/13/2019 Compiler Lab Manuals
8/40
5(tr6if"A"$991$
5
if"str%(tr&99d$5(tr6H
Helse
5co!t33:error@n:6H
Helse
5co!t33:error@n:6H
H
int A"$5int (6
(9(tr6if"str%(tr&99a$
5(tr6if"str%(tr&99;$
5(tr6ret!rn 16H
else5
(tr9(6if"str%(tr&99a$
5(tr6ret!rn 16H
else
5
-
8/13/2019 Compiler Lab Manuals
9/40
ret!rn #6H
HH
else5ret!rn #6H
H
Output
Enter strin==cadcadN s!ccessf!lly Acce(ted
enter strin== ca;dca;dN s!ccessf!lly Acce(ted
-
8/13/2019 Compiler Lab Manuals
10/40
PROGRAM 4
Objective -A program to check that the entered numeris Integer, 5eal,and 2Eponential
with the help of numer grammer-
digit -D 'B...F
sign -D 0-G
Integer -D +digit*0
>ignHInteger -D sign+digit*0
5eal -D >ignHInteger.Integer
2Eponential -D 5eal 2 >ignHInteger
Theory n the first (hase of a com(iler < lexical analyer takes in(!t as are!lar ex(ression and ives o!t(!t < a series of tokens< and then inf!rther (hases
-
8/13/2019 Compiler Lab Manuals
11/40
-
8/13/2019 Compiler Lab Manuals
12/40
Helse if"c99N$5
t916
Helse if"c99eTTc99E$5
e9e16(t9(t16if"e99#$5
t9>6Helse5
"$6H
Helse if"c99,8SSe99>$5
(9(16(t9(t16if"(99#$
5t9>6
(t9l6
Helse5
"$6H
Helse5
t9>6H
H
Output
Enter a n!m;er to check inteer< real < ex(onential=
-
8/13/2019 Compiler Lab Manuals
13/40
,.1*,.1*N
+!m;er is Real
Enter a n!m;er to check inteer < real < ex(onential =,U,UN
+!m;er is nteer
-
8/13/2019 Compiler Lab Manuals
14/40
Program -5
Objective A program for 5ecursive descent parser forthe grammar-
2 -D 82
2-D 082 G
8 -D ?8
8-D 9?8 G
? -D +2* id
where symol G is used to represent null
Theory A (arser that !ses a set of rec!rsive (roced!res to reconie itsin(!t with no ;ack trackin is called a rec!rsive descent (arser.Rec!rsive (roced!res are easy to write and are efficient if written ina (ro(er lan!ae. Be can constr!ct the rec!rsive descent (arserfor the rammar of exam(le.. after removal of ;acktrackin !sinleft rec!rsion removal ;y means of m!t!ally rec!rsive (roced!resas follows.
Algorithm Proced!re E"$
)ein
"$ 5 E EK HEPRME"$
End
Proced!re EPRME"$
)ein f in(!t sym;ol 9 JK then)ein
AVA+CE"$6 5 EK EK H"$6EPRME"$6End
End
Proced!re "$)ein
-
8/13/2019 Compiler Lab Manuals
15/40
L"$6 5 LK HPME"$
End
Proced!re PRME"$)einf in(!t sym;ol 9 JIK then)ein
AVA+CE"$6 5 K ILK HL"$6PRME"$62nd
Procedure ?+*
Jegin
If input symol & idL then
A6MA7C2+*"
2lse if input symol & + then
Jegin
A6MA7C2+*
2+*
If input symol & *L then
AVA+CE"$6
ElseERROR"$6
EndElse ERROR"$6end
Program -
2incl!de3conio.h4
2incl!de3stdio.h42incl!de3iostreame.h42incl!de3strin.h4int (t9>&6void E"$6void EPRME"$6void "$6void PRME"$6
void L"$6
-
8/13/2019 Compiler Lab Manuals
16/40
void main"$5co!t33:enter strin==:6cin44str6
len9strlen"str$6str%len&9N6co!t33str6E"$6if"str%(t&99N$5co!t33:s!ccess:6Helse5co!t33:error:6H@@k(=etch"$6Hvoid E"$
5co!t33:e?n:6"$6
EPRME"$6@@co!t33:e?n:6H
void EPRME"$5co!t33:e(?n: 6if"str%(t&99$
5(t9(t16
"$6EPRME"$6H
Hvoid "$
5co!t33:t?n:6L"$6PRME"$6
H
-
8/13/2019 Compiler Lab Manuals
17/40
void PRME"$5co!t33:t(?n:6if"str%(t&99I$
5(t9(t16L"$6PRME"$6H
Hvoid L"$
5co!t33:f?n:6if"str%(t&99d$
5(t9(t16H
else if"str%(t&99"$5
(t9(t16E"$6if"str%(t&99$$
5
(t9(t16H
elseco!t33:error?n:6
Helseco!t33:error?n:6
H
Output Enter strin == dIddIdNe
t
-
8/13/2019 Compiler Lab Manuals
18/40
ft(ft(
e(s!ccess
-
8/13/2019 Compiler Lab Manuals
19/40
Program
Objective WAP to remove a left recursion of a givenproduction.
Theory it is (ossi;le for a rec!rsive descent (arser to loo(forever. A (ro;lem arises with a / left rec!rsive0 (rod!ctions like
Ex(r eEpr 0 termWhere the left-most symol of the ody is the same as the non-
terminal at the head of the production.
A left-recursive production can e eliminated y rewriting the
offending production. Consider a non-terminal A with twoproductions
A Aa N Bhere Q and S are seD!ences of terminals and non-terminals thatdo not start with A.
Algorithm 1. Arrane the non-terminals of G in some order A1
-
8/13/2019 Compiler Lab Manuals
20/40
int i"
clrscr+*"
for+""*
1
if+&&3NN&&y* 1
printf+)(n 2nter the production to remove left recursion)*"
gets+ar*"
gets+str*"
if+ar#%&&ar#B%strQ&7R==*
1
for+i&"str#B0i%Q&7R=="i00*
1
ar'#%&ar#%"
ar'#'%&ar#'%"
ar'#B0i%&str#B0i%"
!
ar'#B0i%&S"
for+i&"ar#0i%Q&7R=="i00*
1
str#%&S"
str#'%&& "
str#B0i%&ar#0i%"
! str#B0i%&S"
i00"
str#B0i%&N"
i00"
str#B0i%&"
!
else
1
printf+)Aove prodection is not left 5ecursive)*" for+i&"ar#i%Q&7R=="i00*
ar'#i%&ar#i%"
!
printf+)(n(n ur resulting productions are given elowT
(n(n)*"
puts+ar'*"
printf+)(n)*"
puts+str*"
-
8/13/2019 Compiler Lab Manuals
21/40
printf+)(n(nif u want to agin then press y)*"
&getchar+*"
!
else 1
reak"
!
!
!
-
8/13/2019 Compiler Lab Manuals
22/40
Program X
Objective WAP to make a 7?A from a regulareEpression .
Theory he re!lar ex(ression is the notation of choice fordescri;in lexical analyers and other (attern-(rocessinsoftware6 im(lementation of that software reD!ires thesim!lation of a LA or (erha(s the sim!lation of +LA .
;eca!se an +LA often has a choice of move on an in(!tsym;ol < or even a choice of makin a transaction on Y
"n!ll$ or on a real in(!t sym;ol< its sim!lation is lessstraihtforward than for a LA . th!s often it is im(ortantto convert an +LA to a LA that acce(ts the samelan!ae.
Algorithm (Thompson algorithm)Method = - ;ein ;y (arsin r into its constituent su-eEpressions. 8he rules for constructing an 7?A consist of
asis rules for handling sueEpressions with no operators ,and inductive rules for constructing larger 7?ALs from the
7?ALs for the immediate su-eEpressions of a given
eEpression .
Induction T- >uppose 7+s* and 7+t* are 7?ALs for regular
eEpression s and t , respectively.
a$ >uppose r & sNt. 8hen 7+r * ,the 7?A for r , isconstructed .
Zere < i and f are new states < the start and acce(tin states of+" r$
-
8/13/2019 Compiler Lab Manuals
23/40
the only acce(tin state of +" r$ . the acce(tin state of +"s$and the start state of 7+t* are merged into a single state ,will all the transitions in or out of either state. A path
from I to f must go first through 7+s*, and therefore its
lael will egin with some string in =+s*.c$ >uppose r & s9. 8hen for r we construct the 7?A 7+r *.:ere , I and f are new states , the start state and lone
accepting state of 7+r *. 8o get from I to f , we can either
follow the introduced path laeled e , which takes care of
the one string in =+s*' , or we can go to the start state of
7+s* , through that 7?A , then from its accepting state
ack to its start state Uero or more times. 8hese options
allow 7+r * to accept all the strings in =+s*', =+s*B, and so
on , so the entire set of strings accepted y 7+r * is =+s*9.
d$ Linally< s!((ose r 9 "s$ . then "r $ 9 "s$ < and we can !sethe +LA +"s$ as +"r $.
Program
2incl!de3stdio.h42incl!de3conio.h42incl!de3stdli;.h4
int n&&&6char t%#>&6char I(tr 9 : :6char Istr 9 : :6
void nfa"void$6
@@ MA+ L[+CO+
void main"$5
clrscr"$6(rintf":?n Enter Re!lar Ex(ression = :$6scanf":\s:
-
8/13/2019 Compiler Lab Manuals
24/40
void nfa"void$5
int i$5
if"I(tr 49 X SS I(tr 39 1##$5
st%F& 9 i16end%F& 9 i#6t%F& 9 I(tr6i6
F6(tr6
H
if"I(tr 99 " SS I(tr ]9 $ SS I"(tr #$ 99 @$5
if"I"(tr U$ 99 I$5st%F& 9 i6end%F& 9 i 16t%F& 9 e6
F6
st%F& 9 i 86end%F& 9 i16
t%F& 9 e6F6st%F& 9 i 86end%F& 9 i X6t%F& 9e6
F6
st%F& 9 i6end%F& 9 i X6t%F& 9 e6
F6
-
8/13/2019 Compiler Lab Manuals
25/40
i6H
st%F& 9 i6end%F& 9 i16
t%F& 9 e6F6
st%F& 9 i6end%F& 9 i*6t%F& 9 e6
F6i6
(tr6
st%F& 9 i6end%F& 9 i16t%F& 9 I(tr6
F6i6
(tr6
st%F& 9 i6end%F& 9i*6
t%F& 9 e6F6i6
(tr6st%F& 9 i6end%F& 9 i16t%F& 9 I(tr6
F6i6
(tr6
st%F& 9 i6end%F& 9 i16t%F& 9 e6
F6i6str 9 (tr6if"I"str 1$ 99 I SS I"str #$ 99?>$
;reak6
if"I"str 1$ 99 I$
-
8/13/2019 Compiler Lab Manuals
26/40
5(tr6i6while"I(tr ]9 ?>$
5 st%F& 9 i6end%F& 9 i16
(tr6t%F& 9 I(tr6
F6i6
(tr6H
;reak6H
(tr6
Hif"I(tr 99 "$5
n9i16x9i#6st%F&9i16
end%F&9i#6t%F& 9 e6
F6i6
(tr6H
if"I"(tr 1$ 99 I$5
st%F& 9 i16end%F& 9 i#6t%F& 9 e6
F6i6
(tr6H
if"I(tr 99I$5st%F& 9 i6
-
8/13/2019 Compiler Lab Manuals
27/40
end%F& 9 x6t%F& 9 e6
F6st%F& 9 n6
end%F& 9 i16t%F& 9 e6F6(tr6H
H(rintf":?n ransition ta;le for +LA = :$6(rintf":?n?n 'AR i@o 'ym;ol E+ ?n:$6(rintf": 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 99 9 ?n?n:$6n 9 F6for"r9>6r3F6r$
(rintf":?n \d\Uc\8d:
-
8/13/2019 Compiler Lab Manuals
28/40
Program /
Objective A program for stack implementation of shift
reduce parser for the grammar-9 2 -D 8:8 -D ?R
: -D 08: e where e & 7R==
R -D 9?R e
? -D i where i& any alphaetic terminal word.
Theory his (arsin method is ;ottom-!( ;eca!se it attem(ts to constr!ct a
(arse tree for an in(!t strin ;einnin at the leaves "the ;ottom $
and workin !( towards the root " the to( $ .here are two (ro;lems that m!st ;e solved if we are to a!tomate
(arsin ;y handle (r!nin. he first is how to locate a handle in ariht-sentential from < and the second is what (rod!ction to choosein case there is more than one (rod!ction with the same riht side.Bhole the (rimary o(erations of the (arser are shift and red!ce6i--6Helse 5state91X6startid9i-#6 i--6H
;reak6@@'tate for whilecase *=if"str%i&99h$ state9,6else5state91X6startid9i-16i--6H
;reak6case ,=if"str%i&99i$ state9U6else5state91X6startid9i-#6i--6H
;reak6case U=if"str%i&99l$ state986else5state91X6startid9i-*6i--6H
;reak6case 8= if"str%i&99e$ state9X6else5state91X6startid9i-,6i--6H
;reak6case X= if"str%i&99"TTstr%i&99+[$
5(rintf":while = eyword?n?n:$6state9>6i--6Helse5state91X6startid9i-U6i--6H
;reak6@@state for docase 7=if"str%i&99o$ state96
else5state91X6startid9i-16i--6H;reak6case = if"str%i&995TTstr%i&99 TTstr%i&99+[TTstr%i&99"$5
(rintf":do = eyword?n?n:$6state9>6i--6H
;reak6
@@state for else
-
8/13/2019 Compiler Lab Manuals
36/40
case 1>=if"str%i&99l$ state9116else5state91X6startid9i-16i--6H
;reak6case 11=if"str%i99s&$ state91#6
else5state91X6startid9i-#6i--6H;reak6case 1#=if"str%i&99e$ state91*6else5state91X6startid9i-*6i--6H
;reak6case 1*=if"str%i&995TTstr%i&99+[$5
(rintf":else = eyword?n?n:$6state9>6i--6Helse5state91X6startid9i-,6i--6H
;reak6@@state for forcase 1,=if"str%i&99o$ state91U6else5state91X6startid9i-16i--6H
;reak6case 1U=if"str%i&99r$ state9186else5state91X6startid9i-#6i--6H
;reak6case 18=if"str%i&99"TTstr%i&99+[$5
(rintf":for =eyword?n?n:$6state9>6i--6Helse5state91X6startid9i-*6i--6H
;reak6
@@'tates for identifierscase 1X=if"isaln!m"str%i&$TTstr%i&99-$5state9176i6Helse if"str%i&99+[TTstr%i&993TTstr%i&994TTstr%i&99"TTstr%i&99$TTstr%i&996TTstr%i&999TTstr%i&99TTstr%i&99-$ state9176i--6
;reak6
-
8/13/2019 Compiler Lab Manuals
37/40
case17=if"str%i&99+[TTstr%i&993TTstr%i&994TTstr%i&99"TTstr%i&99$TTstr%i&996TTstr%i&999TTstr%i&99TTstr%i&99-$5endid9i-16
(rintf"::$6for"F9startid6F39endid6F$(rintf":\c:6else if"isaln!m"str%i&$TTstr%i&99$5
(rintf":3 = Relational o(erator?n?n:$6i--6state9>6H
;reak6case #>= if"isaln!m"str%i&$TTstr%i&99$5
(rintf":39 = Relational o(erator?n?n:$6i--6state9>6H
;reak6@@states for relational o(erator 4S 49case #1= if"str%i&999$ state9##6else if"isaln!m"str%i&$TTstr%i&99$5
(rintf":4 = relational o(erator?n?n:$6i--6state9>6H
;reak6case ##= if"isaln!m"str%i&$TTstr%i&99$5
(rintf":49 =relational o(erator?n?n:$6i--6
state9>6
-
8/13/2019 Compiler Lab Manuals
38/40
H;reak6@@states for relational o(erator 99 S assinment o( 9case #*= if"str%i&999$ state9#,6
else5(rintf": = Assinment o(erator?n?n:$6i--6state9>6H
;reak6case #,= if"isaln!m"str%i&$$5
(rintf":99 =Relational o(erator?n?n:$6state9>6i--6H
;reak6@@states for constantscase #U= if"isal(ha"str%i&$$5
(rintf": =Error=?n?n:$6(!ts"str$6
for"F9>6F3i6F$(rintf"::$6 (rintf":Y:$6 (rintf":Error at (osition \d Al(ha;et can not follow diit?n?n:6i--6H
;reak6
@@states for s(ecial character "
-
8/13/2019 Compiler Lab Manuals
39/40
case #8= (rintf":" ='(ecial character?n?n:$6startid9i6state9>6i--6
;reak6@@state for s(ecial character $case #X= (rintf":$ = '(ecial character?n?n:$6state9>6i--6
;reak6@@state for 6case #7= (rintf":6 ='(ecial character?n?n:$6state9>6i--6
;reak6@@state for case #= (rintf": =O(erator?n?n:$6state9>6i--6
;reak6case *>= (rintf":- =O(erator?n?n:$6state9>6i--6
;reak6@@error state@@case = oto E+6Hi6H
(rintf":End of the (roram:$6@@E+6etch"$6
H
@I his (roram works as a model of lexical analyser" or shows workin $ < which se(erates the syntax andenerates token I@
-
8/13/2019 Compiler Lab Manuals
40/40
Output
IIIIIIIIProram for lexical analysisIIIIIIII
Enter the strin = ifAnalysis
f = eywordEnd of the (roram.
IIIIIIIIProram for lexical analysisIIIIIIII
Enter the strin = Analysis
= O(erator End of the (roram.