You are on page 1of 40

PROGRAM - 1 Objective WAP a program to check a given word is a

keyword or not .

Theory Keywords are the reserve words of the


language. 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 varia le names. Every C word is classified as either a keyword or an identifier. All keywords have fixed meanin s and these meanings cannot e 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 iven keywords of A% & % &. -f strin matched .hen Print / -t is a keyword 0 Else Print / -t is not a 1eyword 0.

Program
2incl!de3stdio.h4 2incl!de3conio.h4 2incl!de3strin .h4 void main"$ 5 clrscr"$6 char a%7&%8&95:;e in:<:end:<:if:<:then:<:else:<:while:<:do: !" char #$%" int c&',i" (rintf":Enter the strin = (n)*"

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 it according to our re;uirement 9,

Output
Enter the strin = -f -. -' 1EABORC Enter the strin = )e +O. A 1EABORC

PROGRAM 2 Objective WAP 84 I<P=2<278 A >CA7725 W:IC: I6278I?I2> 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 letters and di its< with a letter as a first character. )oth !((ercase and lower case letters are (ermitted< altho! h lowercase letters are commonly !sed. .he !nderscore character is also (ermitted in identifiers. -t is !s!ally !sed as a link ;etween two words in lon identifiers. Constants in C refer to fixed val!es that do not chan e d!rin the exec!tion of a (ro ram. C s!((orts several ty(es of constants as = -nte er constants< Real constants< 'in le character constants< 'trin constants .

Algorithm
1. Eoad the iven strin in an array " arr%1>& $ . #. Check the first word of the strin <

-f it is an al(ha;et .hen / it is an identifier0 If it is a digit 8hen @it is constantA until an alpha et is not found If an alpha et is found 8hen @it is an identifierA.

Program
2incl!de3iostream.h4 2incl!de3conio.h4 2incl!de3cty(e.h4 2incl!de3strin .h4 2incl!de3stdio.h4 void main"$ 5 clrscr"$6 char arr%1>&6

int i9><l6 (rintf":E+.ER '.R-+G= :$6 ets"arr$6 l9strlen"arr$6 i9>6 int F9>6 while"i3l$ 5 if"isal(ha"arr%i&$$ 5 F9i6 while"isaln!m"arr%i&$$ iGG6 (rintf":?n -dentifier== :$6 while"F3i$ 5 co!t33arr%F&6 FGG6 H co!t33endl6 H else if"isdi it"arr%i&$$ 5 F9i6 while"isdi it"arr%i&$$ iGG6 co!t33:?n Constant= :6 while"F3i$ 5 co!t33arr%F&6 FGG6 H co!t33endl6 H @@ else iGG6 etch"$6 H H @I .his is only for the constants And Identifiers 9,

Output
Enter strin = BCa s Constant= #* -dentifier== a;s

PROGRAM 3 Objective - A program for


> -D cAd A -Da,a acktrack parser for the grammar-

Theory
-n .o(-down (arser< there is a well-known draw;ack <named )acktrackin . -f we have more than one o(tion for the same (rod!ction erroneo!s ex(ansion ca!ses a mismatch< so ;acktrackin is done < ;!t !ndoin the semantic actions reD!ires s!;stantial overhead< as the entries made into sym;ol ta;le need to ;e erased. )acktrackin can ;e removed ;y the left factorin .

Algorithm
Proced!re s"$6 )e in -f in(!t sym;ol 9 JcK then )e in Advance"$6 -f a"$ then -f in(!t sym;ol 9 JdK then )e in advance"$6 ret!rn tr!e end End6 Ret!rn false End Proced!re a"$6 ;e in isave =9 in(!t-(ointer 6 if in(!t sym;ol 9 JaK then ;e in advance"$6 if in(!t sym;ol 9 J;K then ;e in advance"$6ret!rn tr!e end end in(!t-(ointer =9 isave6 @I fail!re to find a; I@ -f in(!t sym;ol 9 JaK then )e in advance"$6ret!rn tr!e end

Else ret!rn false End

Program
2incl!de3conio.h4 2incl!de3stdio.h4 2incl!de3iostream.h4 2incl!de3strin .h4 int (t9><len<(tr6 char str%1>&6 void '"$6 int A"$6 @@void ."$6 @@void .PR-ME"$6 @@void L"$6 void main"$ 5 co!t33:enter strin ==:6 cin44str6 len9strlen"str$6 str%len&9MNM6 co!t33str6 '"$6 if"str%(t&99MNM$ 5 co!t33:s!ccess:6 H else 5 co!t33:error:6 H @@k(= etch"$6 H void '"$ 5 @@co!t33:e?n:6 if"str%(tr&99McM$

5 (trGG6 if"A"$991$ 5 if"str%(tr&99MdM$ 5 (trGG6 H H else 5 co!t33:error@n:6 H H else 5 co!t33:error@n:6 H H int A"$ 5 int (6 (9(tr6 if"str%(tr&99MaM$ 5 (trGG6 if"str%(tr&99M;M$ 5 (trGG6 ret!rn 16 H else 5 (tr9(6 if"str%(tr&99MaM$ 5 (trGG6 ret!rn 16 H else 5

ret!rn #6 H H H else 5 ret!rn #6 H H

Output
Enter strin ==cad cadN s!ccessf!lly Acce(ted enter strin == ca;d ca;dN s!ccessf!lly Acce(ted

PROGRAM 4 Objective - A program to check that the entered num


is Integer, 5eal,and 2Eponential with the help of num er grammerdigit -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 er

Theory

-n the first (hase of a com(iler < lexical analyOer takes in(!t as a re !lar ex(ression and ives o!t(!t < a series of tokens< and then in f!rther (hases <it is decided that these tokens are inte er< real and etc .

Algorithm
1. Eoad the iven strin in an array " arr%1>& $ . #. Match the (rod!ctions with the strin which is stored in the

array di it -4 >@1@#...@P si n -4 G@-@Q -nte er -4 "di it$G 'i nR-nte er -4 si n"di it$G Real -4 'i nR-nte er.-nte er Ex(onential -4 Real E 'i nR-nte er *. Print the ty(e of strin .

Program
2incl!de3conio.h4 2incl!de3iostreame.h4 2incl!de3strin .h4 void -"$6 int (t9><c<t9><l<(9><e9>6 char arr%1>&6

void main"$ 5 clrscr"$6 co!t33:enter a n!m;er to check inte er<real<ex(onential=:33:?n:6 cin44arr6 l9strlen"arr$6 arr%l&9MNM6 co!t33arr33:?n:6 -"$6 if"t991$ 5 if"(991SSe991$ 5 co!t33:+!m;er is Ex(onential:6 H else if"(991SSe99>$ 5 co!t33:+!m;er is Real:6 H else if"(99>SSe991$ 5 co!t33:+!m;er is Ex(onential:6 H else 5 co!t33:+!m;er is -nte er:6 H H else 5 co!t33:Bron +!m;er:6 H etch"$6 H void -"$ 5 c9arr%(t&6 if"""c99,*$TT"c99,U$$TT""c49,7$SS"c3U7$$$ 5 (t9(tG16 -"$6

H else if"c99MNM$ 5 t916 H else if"c99MeMTTc99MEM$ 5 e9eG16 (t9(tG16 if"e99#$ 5 t9>6 H else 5 -"$6 H H else if"c99,8SSe99>$ 5 (9(G16 (t9(tG16 if"(99#$ 5 t9>6 (t9l6 H else 5 -"$6 H H else 5 t9>6 H H

Output
Enter a n!m;er to check inte er< real < ex(onential=

,.1* ,.1*N +!m;er is Real Enter a n!m;er to check inte er < real < ex(onential = ,U ,UN +!m;er is -nte er

Program -5 Objective A program for 5ecursive descent parser for


the grammar2 -D 82I 2I-D 082I, G 8 -D ?8I 8I-D 9?8I, G ? -D +2* , id where sym ol IGI is used to represent null

Theory
A (arser that !ses a set of rec!rsive (roced!res to reco niOe its in(!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 in a (ro(er lan !a e. Be can constr!ct the rec!rsive descent (arser for the rammar of exam(le.. after removal of ;acktrackin !sin left rec!rsion removal ;y means of m!t!ally rec!rsive (roced!res as follows.

Algorithm
Proced!re E"$ )e in ."$ 5E EPR-ME"$ End .EK H

Proced!re EPR-ME"$ )e in -f in(!t sym;ol 9 JGK then )e in ACVA+CE"$6 5 EK G.EK H ."$6 EPR-ME"$6 End End Proced!re ."$ )e in

L"$6 .P-ME"$ End

5 . L.K H

Proced!re .PR-ME"$ )e in -f in(!t sym;ol 9 JIK then )e in ACVA+CE"$6 5 .K IL.K H L"$6 .PR-ME"$6 2nd Procedure ?+* Jegin If input sym ol & KidL then A6MA7C2+*" 2lse if input sym ol & K+K then Jegin A6MA7C2+* 2+* If input sym ol & K*L then ACVA+CE"$6 Else ERROR"$6 End Else ERROR"$6 end

Program 2incl!de3conio.h4 2incl!de3stdio.h4 2incl!de3iostreame.h4 2incl!de3strin .h4 int (t9><len6 char str%1>&6 void E"$6 void EPR-ME"$6 void ."$6 void .PR-ME"$6 void L"$6

void main"$ 5 co!t33:enter strin ==:6 cin44str6 len9strlen"str$6 str%len&9MNM6 co!t33str6 E"$6 if"str%(t&99MNM$ 5 co!t33:s!ccess:6 H else 5 co!t33:error:6 H @@k(= etch"$6 H void E"$ 5 co!t33:e?n:6 ."$6 EPR-ME"$6 @@co!t33:e?n:6 H void EPR-ME"$ 5 co!t33:e(?n: 6 if"str%(t&99MGM$ 5 (t9(tG16 ."$6 EPR-ME"$6 H H void ."$ 5 co!t33:t?n:6 L"$6 .PR-ME"$6 H

void .PR-ME"$ 5 co!t33:t(?n:6 if"str%(t&99MIM$ 5 (t9(tG16 L"$6 .PR-ME"$6 H H void L"$ 5 co!t33:f?n:6 if"str%(t&99MdM$ 5 (t9(tG16 H else if"str%(t&99M"M$ 5 (t9(tG16 E"$6 if"str%(t&99M$M$ 5 (t9(tG16 H else co!t33:error?n:6 H else co!t33:error?n:6 H

Output
Enter strin == dId dIdNe t

f t( f t( e( s!ccess

Program Objective
production. WAP to remove a left recursion of a given

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 term Where the left-most sym ol of the ody is the same as the nonterminal at the head of the production. A left-recursive production can e eliminated y rewriting the offending production. Consider a non-terminal A with two productions A Aa N Bhere Q and S are seD!ences of terminals and non-terminals that do not start with A.

Algorithm
1. #.

Arran e the non-terminals of G in some order A1<A#< WW<An. Lor i =9 1 to n do )e in Lor F =9 1 to i-1 do Re(lace each (rod!ction of the form Ai AF y ;y the (rod!ctions AF '1 T '# T WW T 'k are all the c!rrent Ai- productions 2nd

Program
Oinclude.stdio.hD Oinclude.conio.hD Oinclude.string.hD void main+* 1 char ar#'-%,ar'#'-%,str#'-%, &I3I"

int i" clrscr+*" for+""* 1 if+ &&I3INN &&IyI* 1 printf+)(n 2nter the production to remove left recursion)*" gets+ar*" gets+str*" if+ar#-%&&ar#B%PPstrQ&7R==* 1 for+i&-"str#B0i%Q&7R=="i00* 1 ar'#-%&ar#-%" ar'#'%&ar#'%" ar'#B0i%&str#B0i%" ! ar'#B0i%&ISI" for+i&-"ar#C0i%Q&7R=="i00* 1 str#-%&ISI" str#'%&I&I " str#B0i%&ar#C0i%" ! str#B0i%&ISI" i00" str#B0i%&INI" i00" str#B0i%&I.I" ! else 1 printf+)A ove 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*"

printf+)(n(nif u want to agin then press y)*" &getchar+*" ! else 1 reak" ! ! !

Program
eEpression .

Objective WAP to make a 7?A from a regular Theory


.he re !lar ex(ression is the notation of choice for descri;in lexical analyOers and other (attern-(rocessin software6 im(lementation of that software reD!ires the sim!lation of a CLA or (erha(s the sim!lation of +LA . ;eca!se an +LA often has a choice of move on an in(!t sym;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 less strai htforward than for a CLA . th!s often it is im(ortant to convert an +LA to a CLA that acce(ts the same lan !a e.

Algorithm (Thompson algorithm)


Method = - ;e in ;y (arsin r into its constituent su eEpressions. 8he rules for constructing an 7?A consist of asis rules for handling su eEpressions 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 , is constructed . Zere < i and f are new states < the start and acce(tin states of +" r$ <res(. .here are e-transitions from - to the start states of +"s$ and +"t$< and each of their acce(tin states have etransitions to the acce(tin state f. +ote that the acce(tin states of +"s$ and +"t$ are not acce(tin in +"r$ . 'ince any (ath from - to f m!st (ass thro! h either +"s$ or +"t$ excl!sively< and since the la;el of that (ath is not chan ed ;y the eKs leavin - or enterin f < we concl!de that +"r$ acce(ts E"s$ !nion E"t$< which is the same as E"r $ . ;$ '!((ose r 9 st . then constr!ct +"r $. .he start state of +"s$ ;ecomes the start of +" r $ < and the acce(tin state of +"t$ is

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 la el 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 la eled 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 E"r $ 9 E"s$ < and we can !se the +LA +"s$ as +"r $.

Program
2incl!de3stdio.h4 2incl!de3conio.h4 2incl!de3stdli;.h4 int n<st%#>&<end%#>&<tstate%*>&6 char t%#>&6 char I(tr 9 : :6 char Istr 9 : :6 void nfa"void$6 @@ MA-+ L[+C.-O+ void main"$ 5 clrscr"$6 (rintf":?n Enter Re !lar Ex(ression = :$6 scanf":\s:<(tr$6 nfa"$6 H @@ IIIIIIIIIII L!nction of +LA IIIIIIIIII

void nfa"void$ 5 int i<F<x<r6 for"x9>6x3#>6xGG$ 5 st%x&9end%x&9t%x&9>6 H i9F9>6 while"I(tr ]9 M?>M$ 5 if"I(tr 49 PX SS I(tr 39 1##$ 5 st%F& 9 iG16 end%F& 9 iG#6 t%F& 9 I(tr6 iGG6 FGG6 (trGG6 H if"I(tr 99 M"M SS I(tr ]9 M$M SS I"(tr G#$ 99 M@M$ 5 if"I"(tr GU$ 99 MIM$ 5 st%F& 9 i6 end%F& 9 i G 16 t%F& 9 MeM6 FGG6 st%F& 9 i G 86 end%F& 9 iG16 t%F& 9 MeM6 FGG6 st%F& 9 i G 86 end%F& 9 i GX6 t%F& 9MeM6 FGG6 st%F& 9 i6 end%F& 9 i G X6 t%F& 9 MeM6 FGG6

iGG6 H st%F& 9 i6 end%F& 9 iG16 t%F& 9 MeM6 FGG6 st%F& 9 i6 end%F& 9 iG*6 t%F& 9 MeM6 FGG6 iGG6 (trGG6 st%F& 9 i6 end%F& 9 iG16 t%F& 9 I(tr6 FGG6 iGG6 (trGG6 st%F& 9 i6 end%F& 9iG*6 t%F& 9 MeM6 FGG6 iGG6 (trGG6 st%F& 9 i6 end%F& 9 iG16 t%F& 9 I(tr6 FGG6 iGG6 (trGG6 st%F& 9 i6 end%F& 9 iG16 t%F& 9 MeM6 FGG6 iGG6 str 9 (tr6 if"I"str G 1$ 99 MIM SS I"str G#$ 99M?>M$ ;reak6 if"I"str G 1$ 99 MIM$

5 (trGG6 iGG6 while"I(tr ]9 M?>M$ 5 st%F& 9 i6 end%F& 9 iG16 (trGG6 t%F& 9 I(tr6 FGG6 iGG6 (trGG6 H ;reak6 H (trGG6 H if"I(tr 99 M"M$ 5 n9iG16 x9iG#6 st%F&9iG16 end%F&9iG#6 t%F& 9 MeM6 FGG6 iGG6 (trGG6 H if"I"(tr G1$ 99 MIM$ 5 st%F& 9 iG16 end%F& 9 iG#6 t%F& 9 MeM6 FGG6 iGG6 (trGG6 H if"I(tr 99MIM$ 5 st%F& 9 i6

end%F& 9 x6 t%F& 9 MeM6 FGG6 st%F& 9 n6 end%F& 9 iG16 t%F& 9 MeM6 FGG6 (trGG6 H H (rintf":?n .ransition ta;le for +LA = :$6 (rintf":?n?n '.AR. i@o 'ym;ol E+C ?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:$6 n 9 F6 for"r9>6r3F6rGG$ (rintf":?n \d\Uc\8d:<st%r&<t%r&<end%r&$6 etch"$6 H

Output
Enter the Re !lar Ex(ression =a;a;a; .ransaction ta;le +LA = '.AR. i@o 'ym;ol End 99999999999999999999 1 a # # ; * * a , , ; U U a 8 8 ; X X a

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 alpha etic terminal word.

Theory
.his (arsin method is ;ottom-!( ;eca!se it attem(ts to constr!ct a (arse tree for an in(!t strin ;e innin 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 a ri ht-sentential from < and the second is what (rod!ction to choose in case there is more than one (rod!ction with the same ri ht side. Bhole the (rimary o(erations of the (arser are shift and red!ce< there are act!ally fo!r (ossi;le actions a shift-red!ce (arser can make = 1. 'hift #. Red!ce *. Acce(t ,. Error.

Algorithm
1. Enter the strin which is a((end with N si n < if not then ret!rn #.

*. ,. U.

/syntax error0. Match the strin first letter with the (rod!ction and then !se the (rod!ction accordin to necessity < and P['Z the ri ht side of (rod!ction into 'tack. if letter matches with the .OP of the 'tack < then POP the sym;ol. Re(eat the ste(s !ntil finally we et < N in ;oth the strin as well as in the 'tack. Also write the (rod!ction !sed and actions (erformed.

Program
2incl!de3stdio.h4 2incl!de3strin .h4 2incl!de3conio.h4 char a%1>&6 int to(9-1<i6 void error"$5 (rintf":'yntax Error:$6 H void (!sh"char k%&$ @@P!shes .he 'et Of Characters on to the 'tack 5 for"i9>6k%i&]9M?>M6iGG$ 5 if"to(3P$ a%GGto(&9k%i&6 H H char .O'"$ @@Ret!rns .OP of the 'tack 5 ret!rn a%to(&6 H void (o("$ @@Po(s 1 element from the 'tack 5 if"to(49>$ a%to(--&9M?>M6 H void dis(lay"$ @@Cis(lays Elements Of 'tack 5 for"i9>6i39to(6iGG$ (rintf":\c:<a%i&$6 H void dis(lay1"char (%&<int m$ @@Cis(lays .he Present -n(!t 'trin 5 int l6 (rintf":?t:$6 for"l9m6(%l&]9M?>M6lGG$ (rintf":\c:<(%l&$6

H charI stack"$5 ret!rn a6 H int main"$ 5 char i(%#>&<r%#>&<st<an6 int ir<ic<F9><k6 char t%U&%8&%1>&95:N:<:N:<:.Z:<:N:<:.Z:<:N:< :G.Z:<:N:<:e:<:e:<:N:<:e:< :N:<:N:<:L[:<:N:<:L[:<:N:< :e:<:IL[:<:e:<:e:<:N:<:e:< :N:<:N:<:"E$:<:N:<:i:<:N:H6 clrscr"$6 (rintf":?nEnter any 'trin "A((end with N$ ?n:$6 ets"i($6 (rintf":?n?n'tack?t-n(!t?tProd!ction !sed?n?n:$6 (!sh":NE:$6 dis(lay"$6 (rintf":?t\s?n:<i($6 for"F9>6i(%F&]9M?>M6$ 5 if".O'"$99an$ 5 (o("$6 dis(lay"$6 dis(lay1"i(<FG1$6 (rintf":?tPOP?n:$6 FGG6 H an9i(%F&6 st9.O'"$6 if"st99MEM$ir9>6 else if"st99MZM$ir916 else if"st99M.M$ir9#6 else if"st99M[M$ir9*6 else if"st99MLM$ir9,6 else 5 error"$6 ;reak6 H if"an99MGM$ic9>6

else if"an99MIM$ic916 else if"an99M"M$ic9#6 else if"an99M$M$ic9*6 else if""an49MaMSSan39MOM$TT"an49MAMSSan39M^M$$ 5ic9,6an9MiM6H else if"an99MNM$ic9U6 strc(y"r<strrev"t%ir&%ic&$$6 strrev"t%ir&%ic&$6 (o("$6 (!sh"r$6 if".O'"$99MeM$ 5 (o("$6 dis(lay"$6 dis(lay1"i(<F$6 (rintf":?t\c-4\c?n:<st<#*7$6 H else5 dis(lay"$6 dis(lay1"i(<F$6 (rintf":?t\c-4\s?n:<st<t%ir&%ic&$6 H if".O'"$99MNMSSan99MNM$ ;reak6 if".O'"$99MNM$5 error"$6 ;reak6 H H etch"$6 ret!rn >6 H

Output _

Enter any 'trin " A((end with N $ iIiN 'tack NE NZ. NZ[L NZ[i NZ[ NZ[LI NZ[L NZ[i NZ[ NZ N -n(!t iIiN iIiN iIiN iIiN IiN IiN iN iN N N Prod!ction !sed E .Z . L[ L i POP [IL[ POP Li POP [e N

Ze

Program !" Objective Program for leEical analysis Theory


.he lexical analyOer is the interface ;etween the so!rce (ro ram and the com(iler. .he lexical analyOer reads the so!rce (ro ram one character at a time< carvin the so!rce (ro ram into a seD!ence of atomic !nits called .O1E+'. Each token re(resents a seD!ence of characters that can ;e treated as a sin le lo ical entity. -dentifiers < 1eywords < Constants < O(erators and P!nct!ation sym;ols s!ch as commas and (arenthesis are ty(ical tokens.

Algorithm 1. Eoad the iven word "which we want to check !sin Eex.

AnalyOer$ in an array str. #. +ow analyOe the first sym;ol of a str. -f it is an al(ha;et then Check it is reserved word i.e. a keyword .hen ret!rn / 1eyword0 Else ret!rn / -dentifier0 Else if it is a n!merical val!e then Check for /Real0 and /Ex(onential0 val!e Else if check for the sym;ol < it is a O(erator or not . *. .hen after check the second sym;ol of the strin str and oto states as (er reD!irement . ,. Re(eat ste(s !ntil strin str not ends .

Program
@I Pro ram for lexical analysis I@ 2incl!de3stdio.h4 2incl!de3conio.h4 2incl!de3strin .h4 2define MA` *> void main"$ 5 char str%MA`&6

int state9>6 int i9><F<startid9><endid<startcon<endcon6 clrscr"$6 for"F9>6F3MA`6FGG$ str%F&9+[EE6 (rintf":IIIIIPro ram for lexical analysisIIIII?n?n:$6 (rintf":Enter the strin =:$6 ets"str$6 str%strlen"str$&9M M6 (rintf":Analysis=?n?n:$6 while"str%i&]9+[EE$ 5 while"str%i&99M M$ iGG6 switch"state$ 5 case >=if"str%i&99MiM$ state916 else if"str%i&99MwM$ state9*6 else if"str%i&99MdM$ state976 else if"str%i&99MeM$ state91>6 else if"str%i&99MfM$ state91,6 else if"isal(ha"str%i&$TTstr%i&99MRM$ 5 state91X6 startid9i6 H else if"str%i&99M3M$ state91P6 else if"str%i&99M4M$ state9#16 else if"str%i&99M9M$ state9#*6 else if"isdi it"str%i&$$ 5 state9#U6 startcon9i6 H else if"str%i&99M"M$ state9#86 else if"str%i&99M$M$ state9#X6 else if"str%i&99M6M$ state9#76 else if"str%i&99MGM$ state9#P6 else if"str%i&99M-M$ state9*>6 ;reak6 @@'tates for MifM case 1=if"str%i&99MfM$ state9#6 else 5state91X6 startid9i-16 i--6H

;reak6 case #=if"str%i&99M"MTTstr%i&99+[EE$ 5 (rintf":if = 1eyword?n?n:$6 state9>6 i--6 H else 5state91X6startid9i-#6 i--6H ;reak6 @@'tate for MwhileM case *=if"str%i&99MhM$ state9,6 else5state91X6startid9i-16i--6H ;reak6 case ,=if"str%i&99MiM$ state9U6 else5state91X6startid9i-#6i--6H ;reak6 case U=if"str%i&99MlM$ state986 else5state91X6startid9i-*6i--6H ;reak6 case 8= if"str%i&99MeM$ state9X6 else5state91X6startid9i-,6i--6H ;reak6 case X= if"str%i&99M"MTTstr%i&99+[EE$ 5 (rintf":while = 1eyword?n?n:$6 state9>6 i--6 H else5state91X6startid9i-U6i--6H ;reak6 @@state for MdoM case 7=if"str%i&99MoM$ state9P6 else5state91X6startid9i-16i--6H ;reak6 case P= if"str%i&99M5MTTstr%i&99M MTTstr%i&99+[EETTstr%i&99M"M$ 5 (rintf":do = 1eyword?n?n:$6 state9>6 i--6 H ;reak6 @@state for MelseM

case 1>=if"str%i&99MlM$ state9116 else5state91X6startid9i-16i--6H ;reak6 case 11=if"str%i99MsM&$ state91#6 else5state91X6startid9i-#6i--6H ;reak6 case 1#=if"str%i&99MeM$ state91*6 else5state91X6startid9i-*6i--6H ;reak6 case 1*=if"str%i&99M5MTTstr%i&99+[EE$ 5 (rintf":else = 1eyword?n?n:$6 state9>6 i--6 H else5state91X6startid9i-,6i--6H ;reak6 @@state for MforM case 1,=if"str%i&99MoM$ state91U6 else5state91X6startid9i-16i--6H ;reak6 case 1U=if"str%i&99MrM$ state9186 else5state91X6startid9i-#6i--6H ;reak6 case 18=if"str%i&99M"MTTstr%i&99+[EE$ 5 (rintf":for =1eyword?n?n:$6 state9>6 i--6 H else5state91X6startid9i-*6i--6H ;reak6 @@'tates for MidentifiersM case 1X=if"isaln!m"str%i&$TTstr%i&99M-M$ 5 state9176 iGG6 H else if"str%i&99+[EETTstr%i&99M3MTTstr%i&99M4MTTstr%i&99M"MTTstr%i&99M$MTT str%i&99M6MTTstr%i&99M9MTTstr%i&99MGMTTstr%i&99M-M$ state9176 i--6 ;reak6

case17=if"str%i&99+[EETTstr%i&99M3MTTstr%i&99M4MTTstr%i&99M"MTTstr%i&99M$MTT str%i&99M6MTTstr%i&99M9MTTstr%i&99MGMTTstr%i&99M-M$ 5 endid9i-16 (rintf"::$6 for"F9startid6F39endid6FGG$ (rintf":\c:<str%F&$6 (rintf":= -dentifier?n?n:$6 state9>6 i--6 H ;reak6 @@states for relational o(erator case 1P= if"str%i&99M9M$ state9#>6 else if"isaln!m"str%i&$TTstr%i&99MRM$ 5 (rintf":3 = Relational o(erator?n?n:$6 i--6 state9>6 H ;reak6 case #>= if"isaln!m"str%i&$TTstr%i&99MRM$ 5 (rintf":39 = Relational o(erator?n?n:$6 i--6 state9>6 H ;reak6 @@states for relational o(erator M4MS M49M case #1= if"str%i&99M9M$ state9##6 else if"isaln!m"str%i&$TTstr%i&99MRM$ 5 (rintf":4 = relational o(erator?n?n:$6 i--6 state9>6 H ;reak6 case ##= if"isaln!m"str%i&$TTstr%i&99MRM$ 5 (rintf":49 =relational o(erator?n?n:$6 i--6 state9>6

H ;reak6 @@states for relational o(erator 99 S assi nment o( 9 case #*= if"str%i&99M9M$ state9#,6 else 5 (rintf": = Assi nment o(erator?n?n:$6 i--6 state9>6 H ;reak6 case #,= if"isaln!m"str%i&$$ 5 (rintf":99 =Relational o(erator?n?n:$6 state9>6 i--6 H ;reak6 @@states for constants case #U= if"isal(ha"str%i&$$ 5 (rintf": =Error=?n?n:$6 (!ts"str$6 for"F9>6F3i6FGG$ (rintf"::$6 (rintf":Y:$6 (rintf":Error at (osition \d Al(ha;et can not follow di it?n?n:<i$6 state9PP6 H else if"str%i&99M"MTTstr%i&99M$MTTstr%i&99M3MTTstr%i&99M4MTTstr%i&99+[EETT str%i&99M6MTTstr%i&99M9M$ 5 endcon9i-16 (rintf"::$6 for"F9startcon6F39endcon6FGG$ (rintf":\c:<str%F&$6 (rintf": = Constant?n?n:$6 state9>6 i--6 H ;reak6 @@states for s(ecial character M"M

case #8= (rintf":" ='(ecial character?n?n:$6 startid9i6 state9>6 i--6 ;reak6 @@state for s(ecial character M$M case #X= (rintf":$ = '(ecial character?n?n:$6 state9>6 i--6 ;reak6 @@state for M6M case #7= (rintf":6 ='(ecial character?n?n:$6 state9>6 i--6 ;reak6 @@state for MGM case #P= (rintf":G =O(erator?n?n:$6 state9>6 i--6 ;reak6 case *>= (rintf":=O(erator?n?n:$6 state9>6 i--6 ;reak6 @@error state @@case PP= oto E+C6 H iGG6 H (rintf":End of the (ro ram:$6 @@E+C6 etch"$6 H

@I .his (ro ram works as a model of lexical analyser " or shows workin $ < which se(erates the syntax and enerates token I@

Output
IIIIIIIIPro ram for lexical analysisIIIIIIII Enter the strin = if Analysis -f = 1eyword End of the (ro ram. IIIIIIIIPro ram for lexical analysisIIIIIIII Enter the strin = G Analysis G = O(erator End of the (ro ram.

You might also like