Vous êtes sur la page 1sur 51

LAB MANUAL

COMPILER LAB
VIII SEM BTECH (CSE/IT)
CP 452 COMPILER LAB C(L,T,P) = 2 (0,0,2+2)

1,2 Write a Program to identify data storage statements in an 8086 assembly language program and
estimate the size of data segment.

1. Write a program to identify macro definitions in an assembly language program.

4,5. Extend the above program to implement simple and recursive macro expansion.

6. Write a program to process ‘include’ and ‘define’ macro in C language.

7, 8 Write a program to parse source code string of C-language and identify token in terms of keywords
and identifiers.

9. Construct parse tree of arithmetic statements in C language program.

10. Write a program to optimize the source program for ’operator strength reduction’, ‘dead code
elimination’ and frequency reduction’ transformation.

11, 12 Design a simple high level language containing arithmetic and logic operations pointers, branch
and loop instructions. Write its lexical analyzer using lex.
PROGRAM1

AIM: Write a program to identify data storage statements in an 8086 assembly language
program.

// Source.C

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

struct source
{
char label[10],instr[10],operand[10];
}src;

struct pseudo
{
char code[10];
}ps;

struct result
{
intaddr;
char label[10],instr[10],operand[10];
}rst;

int detect(char search[10])


{
FILE *p;
p=fopen("pseudo.txt","r");
while(!feof(p))
{
fscanf(p,"%s",ps.code);
if(strcmp(search,ps.code)==0)
{
fclose(p);
return 1;
}
}
fclose(p);
return 0;
}
void main()
{
FILE *s,*r;
int d=0,addr;
clrscr();
s=fopen("source.txt","r");
r=fopen("result.txt","w");

while(!feof(s))
{
fscanf(s,"%s%s%s",src.label,src.instr,src.operand);
d=detect(src.instr);

if(d==1)
{
if(strcmp(src.instr,"START")==0)
addr=atoi(src.operand);

else if(strcmp(src.instr,"WORD")==0)
addr=rst.addr+3;

else if(strcmp(src.instr,"RESW")==0)
addr=rst.addr+(atoi(src.operand)*3);

else if(strcmp(src.instr,"RESB")==0)
addr=rst.addr+atoi(src.operand);

else if(strcmp(src.instr,"BYTE")==0)
addr=rst.addr+(strlen(src.operand)-3);

else
addr=rst.addr+3;

strcpy(rst.label,src.label);
strcpy(rst.instr,src.instr);
strcpy(rst.operand,src.operand);
fprintf(r,"%d\t%s\t%s\t%s\n",rst.addr,rst.label,rst.instr,rst.operand);
rst.addr=addr;
}
else
{
printf("Error Encounted");
fclose(s);
fclose(r);
getch();
}
}
fclose(s);
fclose(r);
getch();
}

Output

Input File 1: Pseudo.txt

START

READ

WORD

BYTE

RESW

RESB

END

Input File 2: Source.txt

NULL START 1000

A RESB 100

LOOP READ A

B BYTE C"LOOP"

C WORD 100

G RESW 5

NULL END NULL

Output File: result.txt

0 NULL START 1000

1000 A RESB 100


1100 LOOP READ A

1103 B BYTE C"LOOP"

1107 C WORD 100

1110 G RESW 5

1125 NULL END NULL


PROGRAM 2

AIM: Write a program to identify data storage statements in an 8086 assembly language
program and estimate the size of data segment.

// Source.C

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

struct source
{
char label[10],instr[10],operand[10];
}src;

struct pseudo
{
char code[10];
}ps;

struct result
{
intaddr;
char label[10],instr[10],operand[10];
}rst;

int detect(char search[10])


{
FILE *p;
p=fopen("pseudo.txt","r");
while(!feof(p))
{
fscanf(p,"%s",ps.code);
if(strcmp(search,ps.code)==0)
{
fclose(p);
return 1;
}
}
fclose(p);
return 0;
}

void main()
{
FILE *s,*r;
int d=0,addr;
clrscr();
s=fopen("source.txt","r");
r=fopen("result.txt","w");

while(!feof(s))
{
fscanf(s,"%s%s%s",src.label,src.instr,src.operand);
d=detect(src.instr);

if(d==1)
{
if(strcmp(src.instr,"START")==0){
addr=atoi(s.operand);
size=0;
}
else if(strcmp(src.instr,"WORD")==0){
addr=rst.addr+3;
size=addr-res.address;
}
else if(strcmp(src.instr,"RESW")==0){
addr=rst.addr+(atoi(src.operand)*3);
size=addr-res.address;
}
else if(strcmp(src.instr,"RESB")==0){
addr=rst.addr+atoi(src.operand);
size=addr-res.address;
}
else if(strcmp(src.instr,"BYTE")==0){
addr=rst.addr+(strlen(src.operand)-3);
size=addr-res.address;
}
else{
addr=rst.addr+3;
size=addr-res.address;
}
strcpy(rst.label,src.label);
strcpy(rst.instr,src.instr);
strcpy(rst.operand,src.operand);
fprintf(r,"%d\t%s\t%s\t%s\n",rst.addr,rst.label,rst.instr,rst.operand);
rst.addr=addr;
}
else
{
printf("Error Encounted");
fclose(s);
fclose(r);
getch();
}
}
fclose(s);
fclose(r);
getch();
}

Input File 1: Pseudo.txt

START

READ

WORD

BYTE

RESW

RESB

END

Input File 2: Source.txt

NULL START 1000

A RESB 100

LOOP READ A

B BYTE C"LOOP"

C WORD 100

G RESW 5

NULL END NULL


Output File :Result.txt

Addr Label Operand Instr Size

0 NULL START 1000 0

1000 A RESB 100 100 100

1100 LOOP READ A 0 0

1103 B BYTE C"LOOP" 4

1107 C WORD 100 3

1110 G RESW 5 15 15

1125 NULL END NULL 0 0


PROGRAM 3

AIM: Write a program to identify macro definitions in an assembly language program.

//Macro.C

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

struct source
{
char label[10], instr[10], operand[20];
}src;

struct result
{
char label[10],instr[10], operand[20];
}rst;

void main()
{
FILE *s,*r;
clrscr();
s=fopen("source1.txt","r");
r=fopen("resultmacro.txt","w");

while(!feof(s))
{
fscanf(s,"%s%s%s",src.label,src.instr,src.operand);

if(strcmp(src.instr,"MACRO")==0)
{
while(strcmp(src.instr,"MEND")!=0)
{
fprintf(r,"%s\t%s\t%s\n",rst.label,rst.instr,rst.operand);
strcpy(rst.label,src.label);
strcpy(rst.instr,src.instr);
strcpy(rst.operand,src.operand);
fscanf(s,"%s%s%s",src.label,src.instr,src.operand);
}
}
}
printf("MACRO Completed");
fclose(s);
fclose(r);
getch();
}

Input File: Source.txt

NULL MACRO INCR&A,&B,&REG2=BREG

NULL MOVER &REG2,&A

NULL SUB &REG2,&B

NULL MOVEM &REG2,&A

NULL MEND NULL

NULL START 100

NULL READ N1

NULL READ N2

NULL INCR N1,N2

NULL END

Output File: Result.txt

NULL MACRO INCR&A,&B,&REG2=BREGNULL

NULL MOVER &REG2,&A

NULL SUB &REG2,&B


PROGRAM 4 and 5

AIM: Write a program to extend the third program to implement simple macro expansion.

//Source.C

#include<stdio.h>

#include<conio.h>

#include<ctype.h>

#include<dos.h>

structmnt

char name[20];

intmdtindex;

intSkipFlag;

}mnt[10];

structmdt

intsrno;

charinst[20];

}mdt[10];

structala

int index;

chararg[20];

}ala[10];
intmntc,mdtc,alac;

char *replace_str(char *str, char *orig, char *rep)

static char buffer[4096];

char *p;

if(!(p = strstr(str, orig))) // Is 'orig' even in 'str'?

returnstr;

strncpy(buffer, str, p-str); // Copy characters from 'str' start to 'orig' st$

buffer[p-str] = '\0';

sprintf(buffer+(p-str),"%s%s",rep,p+strlen(orig));

if(!(p = strstr(buffer, orig))) // Is 'orig' even in 'buffer'?

return buffer;

else

replace_str(buffer,orig,rep);

voiddispALA()

int i;

printf("\nIndex\tArgument");

printf("\n------------------");
for(i=0;i<alac;i++)

printf("\n %d %s",ala[i].index,ala[i].arg);

voiddispMDT()

int i;

printf("\nIndex\tArgument");

printf("\n------------------");

for(i=0;i<mdtc;i++)

printf("\n %d %s",mdt[i].srno,mdt[i].inst);

void pass1()

char ch,ch1;

int flag=1;

int i=0,j=0,k=0,l=0;

char token[10],token1[10];

char temp[5],temp1[5];

chartline[80],line[80];

FILE *src;

FILE *dest;

clrscr();

strcpy(temp,"#");

src = fopen("MACRO.txt","r");
dest = fopen("op.c","w");

do{

do{

ch = fgetc(src);

token[i] = ch;

i++;

}while(ch!=EOF && !isspace(ch));

token[i-1] = '\0';

if(!strcmp(token,"macro"))

do{

ch1 = fgetc(src);

token1[j] = ch1;

j++;

}while(ch1!=EOF && !isspace(ch1));

token1[j-1] = '\0';

strcpy(mnt[mntc].name,token1);

mnt[mntc].mdtindex = mdtc;

mntc++;

if(ch1=='\n')

mnt[mntc-1].SkipFlag = 1;

goto Mid;
}

//-------------------------------------

//Create ALA

//-------------------------------------

do{ //ALA Content

ch1 = fgetc(src);

if(ch1=='&')

flag=1;

if(ch1==','||ch1=='\n')

token1[k]='\0';

flag=0;

ala[alac].index = alac;

strcpy(ala[alac].arg,token1);

alac++;

k=0;

if(flag==1)

token1[k++] = ch1;

}while(ch1!=EOF && ch1!='\n');

//-----------------------------------------

Mid:

flag=1;

j=0;

do

line[0]='\0';

do{ //MDT
ch1 = fgetc(src);

line[j++]=ch1;

while(ch1!=EOF && ch1!='\n');

line[j-1]='\0';

for(l = 0 ;l<alac;l++)

sprintf(temp1,"%d",l); //0

strcat(temp,temp1); //#0

sprintf(tline,"%s",replace_str(line,ala[l].arg,temp));

// if(strcmp(tline,""))

strcpy(line,tline);

strcpy(temp,"#");

strcpy(mdt[mdtc].inst,line);

mdt[mdtc].srno = mdtc;

mdtc++;

j=0;

}while(strcmp(line,"mend"));

ch = ' ';

}//end if

else

fprintf(dest,"%s",token);

//---------------------------------------------------------------

if(ch=='\n')

fprintf(dest,"\n");

if(ch==' ')
fprintf(dest," ");

//----------------------------------------------------------------

if(isspace(ch))

token[0]='\0';

i=0;

else

token[0]= ch;

token[1]= '\0';

i=1;

}while(ch!=EOF); //Outer od

fclose(src);

fclose(dest);

//----------------------------------------------------------------

voiddisp()

int i;

charch;

FILE *src;

src = fopen("op.c","r");

do{

ch = fgetc(src);

printf("%c",ch);

}while(ch!=EOF);
}

//----------------------------------------------------------------

void pass2()

charch;

int counter=0;

intstart_index;

char ch1,ch2,pch; //comment validation

intlen,flag=0,sflag=0;

int i = 0,j=0,k=0,l=0;

char token[10];

char token1[10];

char temp[5],temp1[5];

chartline[80];

char line[80];

charrepl[10];

FILE *src;

alac = 0;

strcpy(temp,"#");

src = fopen("op.c","r");

do

do{ //For Seperate Token

ch = fgetc(src);

token[i] = ch;

i++;
}while(ch!=EOF && !isspace(ch));

token[i-1] = '\0';

//--------------------------------------------

for(j=0;j<mntc;j++)

if(!strcmp(token,mnt[j].name)) //if token="mac1"

sflag = 1;

start_index = mnt[j].mdtindex;

if(mnt[j].SkipFlag==1)

goto Next;

do{ //Modify ALA Content

ch1 = fgetc(src);

flag=1;

if(ch1==','||ch1=='\n')

token1[k]='\0';

flag=0;

ala[alac].index = alac;

strcpy(ala[alac].arg,token1);

alac++;

k=0;

counter++;

if(flag==1)

token1[k++] = ch1;

}while(ch1!=EOF && ch1!='\n');

//-----------------------------------------

Next:
k = counter;

do

strcpy(line,mdt[start_index].inst);

if(!strcmp(line,"mend"))

break;

for(l=0;l<k;l++)

strcpy(temp,"#");

sprintf(temp1,"%d",l); //temp1 = "0"

strcat(temp,temp1); //temp = "#" before now temp = "#0"

strcpy(repl,ala[l].arg); //repl = 10

sprintf(tline,"%s",replace_str(line,temp,repl));

if(strcmp(tline,""))

strcpy(line,tline);

printf("\n%s",line);

start_index++;

}while(strcmp(line,"mend"));

printf("\n");

sflag = 1;

}//end if

if(sflag==0)

printf("%s",token);

if(ch=='\n')

printf("\n");
if(ch==' ')

printf(" ");

if(isspace(ch))

token[0]='\0';

i=0;

else

token[0]= ch;

token[1]= '\0';

i=1;

sflag = 0;

counter = 0;

alac = 0;

token1[0] = '\0';

k = 0;

}while(ch!=EOF);

fclose(src);

//-----------------------------------

void main()

clrscr();

pass1();

printf("\n---------------------------");
printf("\nInput Program : ");

printf("\n---------------------------\n");

disp();

printf("\n---------------------------");

printf("\nContent of ALA : ");

printf("\n---------------------------\n");

dispALA();

printf("\n---------------------------");

printf("\nContent of MDT : ");

printf("\n---------------------------\n");

dispMDT();

printf("\n----------------------------\n");

pass2();

getch();

Input File:OP.C

MACRO

TEST &X, &Y, &Z

AIF (&Y EQ &X) .ONLY

MOVER AREG,&Y

AGO .LAST

.ONLYMOVER AREG, &Z

.LASTMEND

Output File:Macro.txt

MACRO
TEST &X, &Y, &Z

AIF (&Y EQ &X) .ONLY

MOVER AREG,&Y

AGO .LAST

.ONLY MOVER AREG, &Z

.LAST MEND

PROGRAM 7and 8

AIM: Write a program to parse source code string of C-language and identify token in
terms ofkeywords and identifiers.

//Source.C
#include<stdio.h>
#include<conio.h>
#include
#include
FILE *fp;
char delim[14]={' ','\t','\n',',',';','(',')','{','}','[',']','#','<','>'};
char oper[7]={'+','-','*','/','%','=','!'};
char key[21]
[12]={"int","float","char","double","bool","void","extern","unsigned","goto","static","class",
"struct","for","if","else","return","register","long","while","do"};
char predirect[2][12]={"include","define"};
char header[6][15]={"stdio.h","conio.h","malloc.h","process.h","string.h","ctype.h"};
void skipcomment();
void analyze();
void check(char []);
int isdelim(char);
int isop(char);
int fop=0,numflag=0,f=0;
char c,ch,sop;
void main()
{
char fname[12];
clrscr();
printf("\n enter filename:");
scanf("%s",fname);
fp=fopen(fname,"r");
if(fp==NULL)
printf("\n the file does not exist");
else
analyze();
printf("\n end of file \n");
getch();
}
void analyze()
{
char token[50];
int j=0;
while(!feof(fp))
{
c=getc(fp);
if(c=='/')
{
//skip comment();
}
else if(c==’ ‘)
while((c=getc(fp))!="");
else if(isalpha(c))
{
if(numflag==1)
{
token[j]='\0';
check(token);
numflag=0;
j=0;
f=0;
}
else
{
token[j]=c;
j++;
}
if(f==0)
f=1;
}
else if(isalnum(c))
{
if(numflag==0)
numflag=1;
token[j]=c;
j++;
}
else
{
if(isdelim(c))
{
if(numflag==1)
{
token[j]='\0';
check(token);
numflag=0;
}
if(f==1)
{
token[j]='\0';
numflag=0;
check(token);
}
j=0;
f=0;
printf("\n delimiters \t%c",c);
}
else if(isop(c))
{
if(numflag==1)
{
token[j]='\0';
check(token);
numflag=0;
j=0;
f=0;
}
if(f==1)
{
token[j]='\0';
j=0;
f=0;
numflag=0;
check(token);
}
if(fop==1)
{
fop=0;
printf("\n operator \t %c%c",c,sop);
}
else
printf("\n operator \t%c",c);
}
else if(c=='.')
{
token[j]=c;
j++;
}
}
}
}
int isdelim(char c)
{
int i;
for(i=0;i<14;i++)
{
if(c==delim[i])
return 1;
}
return 0;
}
int isop(char c)
{
int i,j;
char ch;
for(i=0;i<7;i++)
{
if(c==oper[i])
{
ch=getc(fp);
for(j=0;j<6;j++)
{
if(ch==oper[j])
{
fop=1;
sop=ch;
return 1;
}
}
ungetc(ch,fp);
return 1;
}
}
return 0;
}
void check(char t[])
{
int i;
if(numflag==1)
{
printf("\n number \t\t%s",t);
return;
}
for(i=0;i<2;i++)
{
if(strcmp(t,predirect[i])==0)
{
printf("\n preprocessor directive %s",t);
return;
}
}
for(i=0;i<6;i++)
{
if(strcmp(t,header[i])==0)
{
printf("\n header file \t%s",t);
return;
}
}
for(i=0;i<21;i++)
{
if(strcmp(key[i],t)==0)
{
printf("\n keyword \t\t%s",key[i]);
return;
}
}
printf("\n identifier \t%s",t);
}
void skipcomment()
{
ch=getc(fp);
if(ch=='/')
{
while((ch=getc(fp))!='\0');
}
else if(ch=='*')
{
while(f==0)
{
ch=getc(fp);
if(c=='/')
f=1;
}
}
f=0;
}
Inpt File:Source.C
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()

int i;
char *p,*r;
charstr[30];
clrscr();

printf("Enter the string:");


gets(&str);
p=str;
r=p;
while(*p!='\0')
p++;

p=p-1;
printf("Reverse is:");
while (p>=r)
{
printf("%c",*p);
p--;

getch();

Output File:Result.txt

delimiters #

preprocessor directive include

delimiters <

header file stdio.h


delimiters >

delimiters

delimiters #

preprocessor directive include

delimiters <

header file conio.h

delimiters >

delimiters #

preprocessor directive include

delimiters <

header file conio.h

delimiters >

delimiters

delimiters #

preprocessor directive include

delimiters <

header file string.h

delimiters >

delimiters

keyword void

delimiters

identifier main

delimiters (

delimiters )

delimiters
delimiters

delimiters {

delimiters

delimiters

delimiters

keyword int

delimiters

identifier i

delimiters ;

delimiters

delimiters

keyword char

delimiters

operator *

identifier p

delimiters ,

operator *

identifier r

delimiters ;

delimiters

delimiters

keyword char
delimiters

identifier str

delimiters [

number 30

delimiters ]

delimiters ;

delimiters

delimiters

identifier clrscr

delimiters (

delimiters )

delimiters ;

delimiters

delimiters

delimiters

identifier printf

delimiters (

identifier Enter

delimiters

identifier the

delimiters

identifier string

delimiters )

delimiters ;

delimiters
delimiters

identifier gets

delimiters (

identifier str

delimiters )

delimiters ;

delimiters

delimiters

delimiters

delimiters

delimiters

identifier p

operator =

identifier str

delimiters ;

delimiters

delimiters

delimiters

delimiters

delimiters

identifier r

operator =

identifier p

delimiters ;

delimiters
delimiters

delimiters

delimiters

delimiters

keyword while

delimiters (

operator *

identifier p

operator !=

operator !

number 0

delimiters )

delimiters

delimiters

delimiters

identifier p

operator ++

operator +

delimiters ;

delimiters

delimiters

delimiters

delimiters

delimiters
delimiters

identifier p

operator =

identifier p

operator -

number 1

delimiters ;

delimiters

delimiters

delimiters

delimiters

delimiters

identifier printf

delimiters (

identifier Reverse

delimiters

identifier is

delimiters )

delimiters ;

delimiters

delimiters

delimiters

delimiters

delimiters

keyword while

delimiters
delimiters (

identifier p

delimiters >

operator =

identifier r

delimiters )

delimiters

delimiters

delimiters

delimiters

delimiters

delimiters {

delimiters

delimiters

delimiters

delimiters

delimiters

delimiters

identifier printf

delimiters (

operator %

identifier c

delimiters ,

operator *

identifier p

delimiters )
delimiters ;

delimiters

delimiters

delimiters

delimiters

delimiters

delimiters

identifier p

operator --

operator -

delimiters ;

delimiters

delimiters

delimiters

delimiters

delimiters

delimiters

delimiters }

delimiters

delimiters

delimiters

delimiters
delimiters

delimiters

identifier getch

delimiters (

delimiters )

delimiters ;

delimiters

delimiters

delimiters

delimiters }
Program 9
Aim :Lexical Analyzer

#include<string.h>

#include<ctype.h>

#include<stdio.h>

#include<conio.h>

void keyword(char str[10])

{ if(strcmp("for",str)==0||strcmp("while",str)==0||strcmp("do",str)==0||

strcmp("int",str)==0||strcmp("float",str)==0||strcmp("char",str)==0||

strcmp("double",str)==0||strcmp("static",str)==0||strcmp("switch",str)==0||

strcmp("case",str)==0)

printf("\n%s is a keyword",str);

else

printf("\n%s is an identifier",str);

main()

FILE *f1,*f2,*f3;

char c,str[10],st1[10];

int num[100],lineno=0,tokenvalue=0,i=0,j=0,k=0;

printf("\nEnter the c program");/*gets(st1);*/

f1=fopen("input","w");

while((c=getchar())!=EOF)

putc(c,f1);

fclose(f1);

f1=fopen("input","r");

f2=fopen("identifier","w");
f3=fopen("specialchar","w");

while((c=getc(f1))!=EOF)

if(isdigit(c))

tokenvalue=c-'0';

c=getc(f1);

while(isdigit(c))

tokenvalue*=10+c-'0';

c=getc(f1);

num[i++]=tokenvalue;

ungetc(c,f1);

} else if(isalpha(c))

putc(c,f2);

c=getc(f1);

while(isdigit(c)||isalpha(c)||c=='_'||c=='$')

putc(c,f2);

c=getc(f1);

putc(' ',f2);

ungetc(c,f1);

} else if(c==' '||c=='\t')

printf(" ");

else if(c=='\n')
lineno++;

else

putc(c,f3);

fclose(f2);

fclose(f3);

fclose(f1);

printf("\nThe no's in the program are");

for(j=0;j<i;j++)

printf("%d",num[j]);

printf("\n");

f2=fopen("identifier","r");

k=0;

printf("The keywords and identifiersare:");

while((c=getc(f2))!=EOF)

if(c!=' ')

str[k++]=c;

else

str[k]='\0';

keyword(str);

k=0;

fclose(f2);

f3=fopen("specialchar","r");

printf("\nSpecial characters are");


while((c=getc(f3))!=EOF)

printf("%c",c);

printf("\n");

fclose(f3);

printf("Total no. of lines are:%d",lineno);

getch();

}
Program-10

Predictive parsing

#include <stdio.h>

#include <ctype.h>

#include <string.h>

#include <stdlib.h>

#define SIZE 128

#define NONE -1

#define EOS '\0'

#define NUM 257

#define KEYWORD 258

#define ID 259

#define DONE 260

#define MAX 999

#include<conio.h>

char lexemes[MAX];

char buffer[SIZE];

int lastchar = - 1;

int lastentry = 0;

int tokenval = DONE;

int lineno = 1;

int lookahead;

struct entry

char *lexptr;

int token;

} symtable[100];
struct entry keywords[] =

"if", KEYWORD, "else", KEYWORD, "for", KEYWORD, "int", KEYWORD, "float",

KEYWORD, "double", KEYWORD, "char", KEYWORD, "struct", KEYWORD,"return


",KEYWORD,0,0};

void Error_Message(char *m)

fprintf(stderr, "line %d, %s \n", lineno, m);

exit(1);

} int look_up(char s[])

int k;

for (k = lastentry; k > 0; k--)

if (strcmp(symtable[k].lexptr, s) == 0)

return k;

return 0;

int insert(char s[], int tok)

int len;

len = strlen(s);

if (lastentry + 1 >= MAX)

Error_Message("Symbpl table is full");

if (lastchar + len + 1 >= MAX)

Error_Message("Lexemes array is full");

lastentry = lastentry + 1;

symtable[lastentry].token = tok;

symtable[lastentry].lexptr = &lexemes[lastchar + 1];


lastchar = lastchar + len + 1;

strcpy(symtable[lastentry].lexptr, s);

return lastentry;

} /*void Initialize()

struct entry *ptr;

for(ptr=keywords;ptr->token;ptr+1)

insert(ptr->lexptr,ptr->token);

}*/

int lexer()

int t;

int val, i = 0;

while (1)

t = getchar();

if (t == ' ' || t == '\t')

else if (t == '\n')

lineno = lineno + 1;

else if (isdigit(t))

ungetc(t, stdin);

scanf("%d", &tokenval);

return NUM;

else if (isalpha(t))

{
while (isalnum(t))

buffer[i] = t;

t = getchar();

i = i + 1;

if (i >= SIZE)

Error_Message("Compiler error");

buffer[i] = EOS;

if (t != EOF)

ungetc(t, stdin);

val = look_up(buffer);

if (val == 0)

val = insert(buffer, ID);

tokenval = val;

return symtable[val].token;

else if (t == EOF)

return DONE;

else

tokenval = NONE;

return t;

void Match(int t)

{
if (lookahead == t)

lookahead = lexer();

else

Error_Message("Syntax error");

void display(int t, int tval)

if (t == '+' || t == '-' || t == '*' || t == '/')

printf("\nArithmetic Operator: %c", t);

else if (t == NUM)

printf("\n Number: %d", tval);

else if (t == ID)

printf("\n Identifier: %s", symtable[tval].lexptr);

else

printf("\n Token %d tokenval %d", t, tokenval);

void F()

//void E();

switch (lookahead)

case '(':

Match('(');

E();

Match(')');

break;

case NUM:

display(NUM, tokenval);
Match(NUM);

break;

case ID:

display(ID, tokenval);

Match(ID);

break;

default:

Error_Message("Syntax error");

void T()

int t;

F();

while (1)

switch (lookahead)

case '*':

t = lookahead;

Match(lookahead);

F();

display(t, NONE);

continue;

case '/':

t = lookahead;

Match(lookahead);

display(t, NONE);
continue;

default:

return ;

E()

int t;

T();

while (1)

switch (lookahead)

case '+':

t = lookahead;

Match(lookahead);

T();

display(t, NONE);

continue;

case '-':

t = lookahead;

Match(lookahead);

T();

display(t, NONE);

continue;

default:
return ;

void parser()

lookahead = lexer();

while (lookahead != DONE)

E();

Match(';');

void main()

char ans[10];

printf("\n Program for recursive decent parsing ");

printf("\n Enter the expression ");

printf("And place ; at the end\n");

printf("Press Ctrl-Z to terminate\n");

parser();

getch();