0% found this document useful (0 votes)
41 views

Compiler Design Progrs

This document contains C code for implementing predictive parsing. It defines tokens and keywords. It includes functions for lexical analysis, symbol table insertion and lookup, error handling, and the main predictive parsing functions. The parser uses a switch statement to sequentially call functions to match tokens, make predictions, and display parsing actions.

Uploaded by

Harika Imaymissu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

Compiler Design Progrs

This document contains C code for implementing predictive parsing. It defines tokens and keywords. It includes functions for lexical analysis, symbol table insertion and lookup, error handling, and the main predictive parsing functions. The parser uses a switch statement to sequentially call functions to match tokens, make predictions, and display parsing actions.

Uploaded by

Harika Imaymissu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 23

SREE DATTHA INSTITUTE OF ENGINEERING &

SCIENCE
Date:_____________
Branch / Class:Computer Science Engineering/3
rd
year
1. Write a C program to implement Leical !naly"er.
#include$string.h%
#include$ctype.h%
#include$stdio.h%
&oid 'ey(ord)char str*1+,-
.
i/)strcmp)0/or01str-22+33strcmp)0(hile01str-22+33strcmp)0do01str-22+33strcmp)0int01str-22+33
strcmp)0/loat01str-22+33strcmp)0char01str-22+33strcmp)0dou4le01str-22+33strcmp)0static01str-22+33
strcmp)0s(itch01str-22+33strcmp)0case01str-22+-
print/)05n6s is a 'ey(ord01str-7
else
print/)05n6s is an identi/ier01str-7
8
main)-
.
9:LE ;/11;/<1;/37
char c1str*1+,1st1*1+,7
int num*1++,1lineno2+1to'en&alue2+1i2+1=2+1'2+7
print/)05nEnter the c program0-7/;gets)st1-7;/
/12/open)0input010(0-7
(hile))c2getchar)-->2E?9-
putc)c1/1-7
/close)/1-7
/12/open)0input010r0-7
/<2/open)0identi/ier010(0-7
/32/open)0specialchar010(0-7
(hile))c2getc)/1-->2E?9-
.
i/)isdigit)c--
.
to'en&alue2c@A+A7
c2getc)/1-7
(hile)isdigit)c--
.
to'en&alue;21+Bc@A+A7
c2getc)/1-7
8
num*iBB,2to'en&alue7
ungetc)c1/1-7
8
else i/)isalpha)c--
.
putc)c1/<-7
c2getc)/1-7
(hile)isdigit)c-33isalpha)c-33c22A_A33c22ACA-
D!EE F G?LL D?.: ________________________________ 1
SREE DATTHA INSTITUTE OF ENGINEERING &
SCIENCE
Date:_____________
Branch / Class:Computer Science Engineering/3
rd
year
.
putc)c1/<-7
c2getc)/1-7
8
putc)A A1/<-7
ungetc)c1/1-7
8
else i/)c22A A33c22A5tA-
print/)0 0-7
else i/)c22A5nA-
linenoBB7
else
putc)c1/3-7
8
/close)/<-7
/close)/3-7
/close)/1-7
print/)05nHhe noAs in the program are0-7
/or)=2+7=$i7=BB-
print/)06d01num*=,-7
print/)05n0-7
/<2/open)0identi/ier010r0-7
'2+7
print/)0Hhe 'ey(ords and identi/iersare:0-7
(hile))c2getc)/<-->2E?9-
.
i/)c>2A A-
str*'BB,2c7
else
.
str*',2A5+A7
'ey(ord)str-7
'2+7
8
8
/close)/<-7
/32/open)0specialchar010r0-7
print/)05nSpecial characters are0-7
(hile))c2getc)/3-->2E?9-
print/)06c01c-7
print/)05n0-7
/close)/3-7
print/)0Hotal no. o/ lines are:6d01lineno-7
8
D!EE F G?LL D?.: ________________________________ <
SREE DATTHA INSTITUTE OF ENGINEERING &
SCIENCE
Date:_____________
Branch / Class:Computer Science Engineering/3
rd
year
Output:
Enter the c program
#include$stdio.h%
&oid main)-
.
print/)0hello third year cse0-7
8
IJ
Hhe 'ey(ords and identi/iers are:
include is an identi/ier
stdio is an identi/ier
h is an identi/ier
&oid is an identi/ier
main is an identi/ier
print/ is an identi/ier
hello is an identi/ier
third is an identi/ier
year is an identi/ier
cse is an identi/ier
Special characters are#$.%)-.)00-78
Hotal no. o/ lines are:K
D!EE F G?LL D?.: ________________________________ 3
SREE DATTHA INSTITUTE OF ENGINEERING &
SCIENCE
Date:_____________
Branch / Class:Computer Science Engineering/3
rd
year
<. Write a C program to implement Sym4ol ta4le.

#include $stdio.h%
#include$conio.h%
#include$ctype.h%
#include$alloc.h%
#include$string.h%
#include$math.h%
&oid main)-
.
int i2+1=2+12+1n1/lag2+7
&oid ;p1;add*L,7
char ch1srch14*1L,1d*1L,1c7
clrscr)-7
print/)0Epression terminated 4y C : 0-7
(hile))c2getchar)-->2ACA-
.
4*i,2c7
iBB7
8
n2i@17
print/)0Mi&en Epression : 0-7
i2+7
(hile)i$2n-
.
print/)06c014*i,-7
iBB7
8
print/)05n Sym4ol Ha4le5n0-7
print/)0Sym4ol5taddr5ttype0-7
(hile)=$2n-
.
c24*=,7
i/)isalpha)toascii)c---
.
i/)=22n-
.
p2malloc)c-7
add*,2p7
d*,2c7
print/)06c5t6d5tidenti/ier01c1p-7
8
else
.
D!EE F G?LL D?.: ________________________________ N
SREE DATTHA INSTITUTE OF ENGINEERING &
SCIENCE
Date:_____________
Branch / Class:Computer Science Engineering/3
rd
year
ch24*=B1,7
i/)ch22ABA33ch22A@A33ch22A;A33ch22A2A-
.
p2malloc)c-7
add*,2p7
d*,2c7
print/)05n6c5t6d5tidenti/ier5n01c1p-7
BB7
888
=BB7
8
print/)05nEnter the sym4ol is to 4e searched :0-7
srch2getch)-7
print/)06c01srch-7
/or)i2+7i$27iBB-
.
i/)srch22d*i,-
.
print/)05nSym4ol 9ound0-7
print/)05n6c6s6d5n01srch10 Oaddress 01add*i,-7
/lag217
8
8
i/)/lag22+-
print/)05nSym4ol Dot 9ound0-7
getch)-7
8
D!EE F G?LL D?.: ________________________________ L
SREE DATTHA INSTITUTE OF ENGINEERING &
SCIENCE
Date:_____________
Branch / Class:Computer Science Engineering/3
rd
year
Output:
D!EE F G?LL D?.: ________________________________ K
SREE DATTHA INSTITUTE OF ENGINEERING &
SCIENCE
Date:_____________
Branch / Class:Computer Science Engineering/3
rd
year
3. Write a C program to implement Predicti&e parsing.
#include$conio.h%
#include $stdio.h%
#include $ctype.h%
#include $string.h%
#include $stdli4.h%
#de/ine S:JE 1<Q
#de/ine D?DE @1
#de/ine E?S A5+A
#de/ine DRE <LS
#de/ine TEUW?GD <LQ
#de/ine :D <LV
#de/ine D?DE <K+
#de/ine E!W VVV
char leemes*E!W,7
char 4u//er*S:JE,7
int lastchar 2 @ 17
int lastentry 2 +7
int to'en&al 2 D?DE7
int lineno 2 17
int loo'ahead7
struct entry
.
char ;leptr7
int to'en7
8 symta4le*1++,7
struct entry 'ey(ords*, 2
.
0i/01 TEUW?GD1 0else01 TEUW?GD1 0/or01 TEUW?GD1 0int01 TEUW?GD1 0/loat01
TEUW?GD1 0dou4le01 TEUW?GD1 0char01 TEUW?GD1 0struct01 TEUW?GD10return
01TEUW?GD1+1+87
&oid Error_Eessage)char ;m-
.
/print/)stderr1 0line 6d1 6s 5n01 lineno1 m-7
eit)1-7
8 int loo'_up)char s*,-
.
int '7
/or )' 2 lastentry7 ' % +7 '@@-
D!EE F G?LL D?.: ________________________________ S
SREE DATTHA INSTITUTE OF ENGINEERING &
SCIENCE
Date:_____________
Branch / Class:Computer Science Engineering/3
rd
year
i/ )strcmp)symta4le*',.leptr1 s- 22 +-
return '7
return +7
8
int insert)char s*,1 int to'-
.
int len7
len 2 strlen)s-7
i/ )lastentry B 1 %2 E!W-
Error_Eessage)0Sym4pl ta4le is /ull0-7
i/ )lastchar B len B 1 %2 E!W-
Error_Eessage)0Leemes array is /ull0-7
lastentry 2 lastentry B 17
symta4le*lastentry,.to'en 2 to'7
symta4le*lastentry,.leptr 2 Fleemes*lastchar B 1,7
lastchar 2 lastchar B len B 17
strcpy)symta4le*lastentry,.leptr1 s-7
return lastentry7
8 /;&oid :nitiali"e)-
.
struct entry ;ptr7
/or)ptr2'ey(ords7ptr@%to'en7ptrB1-
insert)ptr@%leptr1ptr@%to'en-7
8;/
int leer)-
.
int t7
int &al1 i 2 +7
(hile )1-
.
t 2 getchar)-7
i/ )t 22 A A 33 t 22 A5tA-
7
else i/ )t 22 A5nA-
lineno 2 lineno B 17
else i/ )isdigit)t--
.
ungetc)t1 stdin-7
scan/)06d01 Fto'en&al-7
return DRE7
8
else i/ )isalpha)t--
.
(hile )isalnum)t--
D!EE F G?LL D?.: ________________________________ Q
SREE DATTHA INSTITUTE OF ENGINEERING &
SCIENCE
Date:_____________
Branch / Class:Computer Science Engineering/3
rd
year
.
4u//er*i, 2 t7
t 2 getchar)-7
i 2 i B 17
i/ )i %2 S:JE-
Error_Eessage)0Compiler error0-7
8
4u//er*i, 2 E?S7
i/ )t >2 E?9-
ungetc)t1 stdin-7
&al 2 loo'_up)4u//er-7
i/ )&al 22 +-
&al 2 insert)4u//er1 :D-7
to'en&al 2 &al7
return symta4le*&al,.to'en7
8
else i/ )t 22 E?9-
return D?DE7
else
.
to'en&al 2 D?DE7
return t7
8
8
8
&oid Eatch)int t-
.
i/ )loo'ahead 22 t-
loo'ahead 2 leer)-7
else
Error_Eessage)0Synta error0-7
8
&oid display)int t1 int t&al-
.
i/ )t 22 ABA 33 t 22 A@A 33 t 22 A;A 33 t 22 A/A-
print/)05n!rithmetic ?perator: 6c01 t-7
else i/ )t 22 DRE-
print/)05n Dum4er: 6d01 t&al-7
else i/ )t 22 :D-
print/)05n :denti/ier: 6s01 symta4le*t&al,.leptr-7
else
print/)05n Ho'en 6d to'en&al 6d01 t1 to'en&al-7
8
&oid 9)-
D!EE F G?LL D?.: ________________________________ V
SREE DATTHA INSTITUTE OF ENGINEERING &
SCIENCE
Date:_____________
Branch / Class:Computer Science Engineering/3
rd
year
.
&oid E)-7
s(itch )loo'ahead-
.
case A)A:
Eatch)A)A-7
E)-7
Eatch)A-A-7
4rea'7
case DRE:
display)DRE1 to'en&al-7
Eatch)DRE-7
4rea'7
case :D:
display):D1 to'en&al-7
Eatch):D-7
4rea'7
de/ault:
Error_Eessage)0Synta error0-7
8
8
&oid H)-
.
int t7
9)-7
(hile )1-
.
s(itch )loo'ahead-
.
case A;A:
t 2 loo'ahead7
Eatch)loo'ahead-7
9)-7
display)t1 D?DE-7
continue7
case A/A:
t 2 loo'ahead7
Eatch)loo'ahead-7
display)t1 D?DE-7
continue7
de/ault:
return 7
8
8
8
&oid E)-
D!EE F G?LL D?.: ________________________________ 1+
SREE DATTHA INSTITUTE OF ENGINEERING &
SCIENCE
Date:_____________
Branch / Class:Computer Science Engineering/3
rd
year
.
int t7
H)-7
(hile )1-
.
s(itch )loo'ahead-
.
case ABA:
t 2 loo'ahead7
Eatch)loo'ahead-7
H)-7
display)t1 D?DE-7
continue7
case A@A:
t 2 loo'ahead7
Eatch)loo'ahead-7
H)-7
display)t1 D?DE-7
continue7
de/ault:
return 7
8
8
8
&oid parser)-
.
loo'ahead 2 leer)-7
(hile )loo'ahead >2 D?DE-
.
E)-7
Eatch)A7A-7
8
8
main)-
.
char ans*1+,7
clrscr)-7
print/)05n Program /or predicti&e parsing 0-7
print/)05n Enter the epression 0-7
print/)0!nd place 7 at the end5n0-7
print/)0Press Ctrl@J to terminate5n0-7
parser)-7
return +7
8
D!EE F G?LL D?.: ________________________________ 11
SREE DATTHA INSTITUTE OF ENGINEERING &
SCIENCE
Date:_____________
Branch / Class:Computer Science Engineering/3
rd
year
Output:
Program /or predicti&e parsing
Enter the epression !nd place 7 at the end
Press Ctrl@J to terminate
aB4;c7
:denti/ier: a
:denti/ier: 4
:denti/ier: c
!rithmetic ?perator: ;
!rithmetic ?perator: B
<;37
Dum4er: <
Dum4er: 3
!rithmetic ?perator: ;
D!EE F G?LL D?.: ________________________________ 1<
SREE DATTHA INSTITUTE OF ENGINEERING &
SCIENCE
Date:_____________
Branch / Class:Computer Science Engineering/3
rd
year
N. Write a C program to implement concept o/ recursi&e descent parser.
#include$stdio.h%
#include$conio.h%
#include$ctype.h%
#include$string.h%
#include$stdli4.h%
#de/ine S:JE 1<Q
#de/ine D?DE <LS
#de/ine E?S A5+A
#de/ine DRE <LS
#de/ine TEUW?GD <LQ
#de/ine :D <LV
#de/ine D?DE <K+
#de/ine E!W VVV
char le*E!W,7
char 4u//er*S:JE,7
int lastchar2@17
int lastentry2+7
int to'en&al2D?DE7
int lno217
int la7
struct entry.
char ;leptr7
int to'en7
8symta4*1++,7
struct entry
'ey(ord*,2.0i/01TEUW?GD10else01TEUW?GD10/or01TEUW?GD10int01TEUW?GD10/loat01
TEUW?GD10dou4le01TEUW?GD10char01TEUW?GD10struct01TEUW?GD1
0return01TEUW?GD1+1+87
&oid error)char ;m-
.
/print/)stderr10Line 6d 6s 5n01lno1m-7
eit)+-7
D!EE F G?LL D?.: ________________________________ 13
SREE DATTHA INSTITUTE OF ENGINEERING &
SCIENCE
Date:_____________
Branch / Class:Computer Science Engineering/3
rd
year
8
int loo'_up)char s*,-
.
int '7
/or)'2lastentry7'%+7'2'@1-
i/)strcmp)symta4*',.leptr1s-22+-
return '7
return +7
8
int insert)char s*,1int to'-
.
int len7
len2strlen)s-7
i/)lastentryB1%2E!W-
error)0Sym4ol ta4le is /ull0-7
i/)lastcharBlenB1%2E!W-
error)0Leemes array is /ull0-7
lastentryBB7
symta4*lastentry,.to'en2to'7
symta4*lastentry,.leptr2Fle*lastcharB1,7
lastchar2lastcharBlenB17
strcpy)symta4*lastentry,.leptr1s-7
return lastentry7
8
&oid init)-
.
struct entry ;ptr7
/or)ptr2'ey(ord7ptr@%to'en7ptrBB-
insert)ptr@%leptr1ptr@%to'en-7
8
int leer)-
.
int t7
int &al1i2+7
(hile)1-
.
t2getchar)-7
i/)t22A A33 t22A5tA-7
else i/)t22A5nA-
lnoBB7
else i/)isdigit)t--
.
ungetc)t1stdin-7
scan/)06d01Fto'en&al-7
return DRE7
8
else i/ )isalpha)t--
.
D!EE F G?LL D?.: ________________________________ 1N
SREE DATTHA INSTITUTE OF ENGINEERING &
SCIENCE
Date:_____________
Branch / Class:Computer Science Engineering/3
rd
year
(hile)isalnum)t--
.
4u//er*i,2t7
t2getchar)-7
iBB7
i/)i%2S:JE-
error)0Compiler Error0-7
8
4u//er*i,2E?S7
i/)t>2E?9-
ungetc)t1stdin-7
&al2loo'_up)4u//er-7
i/)&al22+-
&al2insert)4u//er1:D-7
to'en&al2&al7
return symta4*&al,.to'en7
8
else i/)t22E?9-
return D?DE7
else
.
to'en&al2D?DE7
return t7
8
8
8
&oid match)int t-
.
i/)la22t-
la2leer)-7
else
error)0Synta error0-7
8
&oid disp)int t1int t&al-
.
i/)t22ABA33t22A@A33t22A;A33t22A/A-
print/)05narithmetic operators:6c01t-7
else i/)t22:D-
print/)05nidenti/ier:6s01symta4*t&al,.leptr-7
else
print/)05nto'en 6dto'en&al6d01t1to'en&al-7
8
&oid 9)-
.
&oid E)-7
s(itch)la-
.
case A)A:
D!EE F G?LL D?.: ________________________________ 1L
SREE DATTHA INSTITUTE OF ENGINEERING &
SCIENCE
Date:_____________
Branch / Class:Computer Science Engineering/3
rd
year
match)A)A-7
4rea'7
case DRE:
disp)DRE1to'en&al-7
match)DRE-7
4rea'7
case :D:
disp):D1to'en&al-7
match):D-7
4rea'7
de/ault:
error)0Synta error0-7
88
&oid H)-
.
int t7
9)-7
(hile)1-
.
s(itch)la-
.
case A;A:
t2la7
match)la-7
9)-7
disp)t1D?DE-7
continue7
de/ault:
return7
8
8
8
&oid E)-
.
int t7
H)-7
(hile)1-
.
s(itch)la-
.
caseABA:
t2la7
match)la-7
H)-7
disp)t1D?DE-7
continue7
caseA@A:
t2la7
D!EE F G?LL D?.: ________________________________ 1K
SREE DATTHA INSTITUTE OF ENGINEERING &
SCIENCE
Date:_____________
Branch / Class:Computer Science Engineering/3
rd
year
match)la-7
H)-7
disp)t1D?DE-7
continue7
de/ault:
return7
8
8
8
&oid parser)-
.
la2leer)-7
(hile)la>2D?DE-
.
E)-7
match)A7A-7
8
8
&oid main)-
.
char ans7
clrscr)-7
print/)0GD_P!GSEG0-7
init)-7
print/)05nEnter the epression1placeA7Aat the end:0-7
print/)05n press control@" to terminate......5n0-7
parser)-7
8
D!EE F G?LL D?.: ________________________________ 1S
SREE DATTHA INSTITUTE OF ENGINEERING &
SCIENCE
Date:_____________
Branch / Class:Computer Science Engineering/3
rd
year
Output:
D!EE F G?LL D?.: ________________________________ 1Q
SREE DATTHA INSTITUTE OF ENGINEERING &
SCIENCE
Date:_____________
Branch / Class:Computer Science Engineering/3
rd
year
L. Write a C program to implement :denti/iers.
#include$stdio.h%
#include$conio.h%
#include$String.h%
&oid main)-
.
int i1n11t2+7
char 'eys*3<,*1L,2.0auto0104rea'010case010char010const010continue010de/ault010do01
0dou4le010else010enum010etern010/loat010/or010goto010i/010int010long010re
gister010return010short010signed010si"eo/010static010struct010s(itch010typede/01
0union010unsigned010&oid010&olatile010(hile087
char str*1L,7
clrscr)-7
print/)05n Enter the String :0-7
gets)str-7
print/)05n Hhe String is :0-7
puts)str-7
/or)i2+7i$3<7iBB-
.
2strcmp)str1'eys*i,-7
i/)22+-
t217
8
//print/)05n Hhe &alue /or t is :6d01t-7
i/)t22+-
D!EE F G?LL D?.: ________________________________ 1V
SREE DATTHA INSTITUTE OF ENGINEERING &
SCIENCE
Date:_____________
Branch / Class:Computer Science Engineering/3
rd
year
.
//print/)05n :ts not a 'ey(ord0-7
n2strlen)str-7
i/))str*+,22A1A-33)str*+,22AXA-33)str*+,22A>A-33)str*+,22AOA-33)str*+,22A#A-33
)str*+,22ACA-33)str*+,22A6A-33)str*+,22AIA-33)str*+,22AFA-33)str*+,22A;A-33
)str*+,22ABA-33)str*+,22A_A-33)str*+,22A+A-33)str*+,22A1A-33)str*+,22A<A-33
)str*+,22A3A-33)str*+,22ANA-33)str*+,22ALA-33)str*+,22AKA-33)str*+,22ASA--
.
print/)0in&alid0-7
8
else i/)n%1Q-
.
print/)0out o/ 4ound0-7
8
else
print/)0it is ideni/ier0-7
8
else
.
print/)05n :t is a 'ey(ord0-7
8
getch)-7
8

D!EE F G?LL D?.: ________________________________ <+
SREE DATTHA INSTITUTE OF ENGINEERING &
SCIENCE
Date:_____________
Branch / Class:Computer Science Engineering/3
rd
year
Output:
Enter the String :a4c
Hhe String is :a4c
it is identi/ier
Enter the String :1<3
Hhe String is :1<3
:n&alid
D!EE F G?LL D?.: ________________________________ <1
SREE DATTHA INSTITUTE OF ENGINEERING &
SCIENCE
Date:_____________
Branch / Class:Computer Science Engineering/3
rd
year
K. Write a C program to implement Parser.
#include$stdio.h%
#include$conio.h%
#include$string.h%
#include$stdli4.h%
int !)-7
&oid disp)-7
&oid error)-7
char s*<+,7
int i7
&oid main)-
.
clrscr)-7
print/)0S @% c!d5n0-7
print/)0! @% a4/a5n0-7
print/)0Enter the String:5n0-7
scan/)06s01Fs-7
i2+7
i/)s*iBB,22AcAFF!)--
.
i/)s*BBi,22AdAFFs*iB1,22DRLL-
disp)-7
else
D!EE F G?LL D?.: ________________________________ <<
SREE DATTHA INSTITUTE OF ENGINEERING &
SCIENCE
Date:_____________
Branch / Class:Computer Science Engineering/3
rd
year
error)-7
8
else
error)-7
8
int !)-
.
i/)s*iBB,22AaAFFs*i,22A4A-
return)1-7
else i/)s*@@i,22AaA-
return)1-7
else
return)+-7
8
&oid disp)-
.
print/)05nstring is &alid5n0-7
getch)-7
eit)+-7
8
&oid error)-
.
print/)05nstring is in&alid5n0-7
getch)-7
eit)+-7
8
Output:
S @% c!d
! @% a4/a
Enter the String:
ca4d
string is &alid
S @% c!d
! @% a4/a
Enter the String:
Ca4D
string is in&alid
D!EE F G?LL D?.: ________________________________ <3

You might also like