Compiler Design Exp. File (1-5)
Compiler Design Exp. File (1-5)
INDEX
SNo Date M N T Page Remaurks
Write a prograum waing Lex to alculate l-2
Hhe no. of characters, numbar of werds cud
the no. of ineA in the given text file as ip
Wite a C qrogram to implement th 3-4
nite automatio.
Wnte C program to ompute fRsT S-9
haloslea Ound FoLLOw ot he nan terminals of
gven grammar
Wite a C program to hack the ginen
b3lba r0mmr Às LetB Recusive 3nd remaue
st ursian
Wite a progrm waing Lex to implement
Hhe set ot reguar expresien and inictt L2
sle44e acceptance of a given sting fer
a partieular resulr expresin
EXPERIMENT 1
bjective: Write a program using
Lex to caleulade the no.
ot char acters, mum
ber ot wrda and no.
rogram:
#incude <stdio.h
int char count = 0, wrdcount = 0, ine-count - 0;
crg-info C3){
int main(nt org- ceud, char * represnts the number of
lorg ceunt (orument count)from the " Command ino"
( passed to the program of
string
(ugument Informtion) is
l arg ìnto
l containing argumanta lxrogram.l
aurgvlo) i Hhe nam ot the progcum --’
7 user-’
Hhe Ale name passed by the
fILE * ile;
/Er handling it chack Ahot no. ot arguments (program me
deAs Hhan 2
land ile nam) rm cemmand ine i
I/es, it means hane missed for arguman
any re
M.M.M.UNIVERSITY OF TECHNOLOGY,GKP.
fext. txt
Hello
Welcome to Flex|
Qutput
flex cdexpl.2
9cc Rex.yyc
\a,exe text.txt
Nunber of haacters 2L
Num ber of weT ds 4
Nenber of lines:D
TUTORIAL/PRACTICAL NO.
I/ and it pints a messag sho how to wse
it (arg-count < 2) {
print.f(eu hawe enter s 9nlay Please enter
Ahe Ale name olson, curginfo Co3
retrn L,
/Open he Ale
file = fopen (arg-info C1], "r"),
felose (file);
return 0,
int yywrap){
rehurn / Sndicate end of input
EXPERIMENT2
sbje ctive Write a C pruqrm to inplemnd ihe
tho cowesin
of
egutar expreasten to non deterministic tinite
dutmatian,
ypedef strut i
Edge edyes [MAX],
t edgeluint
NEA;
vid add Edge (NFA nta, t sBart, int end, char transiti)
hfa-> edges Lnta->edgeleunt ), start = siart;
nfa -> edges (nta -> edge lountJ. end end;
nfa -> edges [nta> edse Caunt]. trans ition- fransition;
nta -> edgo leutt+;
h - b--> 2
y2
73--a-->
-> 44
TUTORIAL/ PRACTICAL NO.
for (int i- , ic strlen(ieye); itt){
retun nfa,
EXPERIMENT 3
bjective Write a C prognm to Compute FIRST and
FoLLoW of the non- terminals of given
rammar,
ioram
ttincile <stdio.h>
4*inclde <stdib,h
#indude í string.h>
t de fine MAX 10
t define MAX fROD 20
# define MAX- SYMBoL 20
char prvductin s [MAXPRODI[MAX_ SYMBOLI;
char first CMAX] [MAx. SYM BoL3
*har follow [MAX] CMAX SYMBOLJ,
char nonTerminals CMAX];
int n
void compte First0;
Vid cempute Follow();
int isTerminal (char c);
int isInolow (ch c,jt index),
Vbid nddTo First(char nanTerminal, char ymbol);
vojd addTo follow (char non Terminal, chir symbo)
int main C)i
phntt(Evter the niumbtr of poductibns. )
SCanf ( 7A , Xn);
printf ("Enter the prodactions (e-g EA6); \n"');
forlint i- 0} i<n,it+)f
fullow [i[o]
Compute rst);
Compute followi);
/Display HRST sets
printt ("\n FIRST set\n");
for (int i-0, ic strlen (enTer mina),
itt){
printf ("FIRST (%.c) = {", nonlermin als
(0):
for (int i-0; fivst liJLj) |=\o';
pintf(.e, firstJC;]);
itt){
printt("} \n'');
IDi splay FoLLoW sets
printt"\nfoLeow setsn'):
tor (int i: C; ickten{nonTermina); itt){
pritf( FoLLoW (l.e)- ", nanTermnals LiJ):
for int 4- 0, follow (iJlj) |= '\o'; t+) {
printf("7.e", foll owfiJG);
printf (3\n"),
return 0,
void Computefollow()
for (int i 0, i< str len Cnen Ter
minals), itt) {
addTo follodoTer minals (i3, $))
7 the end marker
/Assuming $ is
fur (int i: 0; i<n; itt)
for (int j-3, pro ductins (Jlj] (= \o'; t+){
if (isTerminal Cproductionsl;1))
continue ,
M.M.M. UNIVI OF TECHNOLOGY, GKP. 7
TUTORIALI PRACTICAL NO.
char namTerminl =
if (productions productieslilly,
CLi+1] |= 1o')1
char nextSymbol = produ ctien i)Gt);
if(isTerminai (next Sywmbot)
dddTofollow(nonerinal, next Synbot);
tor (int k=0; first
(strchr(nonTermina,nextymbo)
han Terminaly][k] |= \0'; ktt){
add Tofollow(nonlerminal, first[strch
hon[erminals, next symbet )- nonlerm)
return 0,
fiRST setii
FRSTs) = 4a, b, e}
fIRSTCD) = b, }
CoLLoN sets .
foLLow(A) = 4b,s)
foLLOwCe) = $}
TUTORIAL/ PRACTICAL NO.
adTofo llow (chr nonlerminal, char
ind index = strchr (nonTermi als nomTeinal
nonTermina
ifCisn follow (symbot, index)){
follow Cinderylstrlen(folow Cindex3)]= sybot,
follow lindex}[stlen(fullow Lindex])] = \o';
EXPERIMENT4
bectie Wnte a CCProgramn to check tho jiven yrammar
s Let reursive nd remOve Left recusion.
tinclude <stdio.h>
+ incude ístdibh
A aA|e
TUTORIAL/ PRACTICAL NO.
pnntf( Thu 9tamna can't be redued n ,
char
9ramnar (si2EJ,
fnntf( Enter the production (e, E»EIA) ");
Scanf('s" ammar)
emane Lett Recusien (grammar,
retm 0)
EXPERIMENT
obiectiwi Write a
5 the
Pregram uaing lex to implement
the
set of regular expresien and indicates
acceptane of a gven string for a particuar regar
presin,