P R I N C I P L E S  OF  C O M P I L E R  D E S I G N  ( P C D )

 

1.

Program on Lexical Analyzer
    
  #include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<stdlib.h>
#include<string.h>
#include<fstream.h>

int cb;
char *ch;

void disdash()
{
for(int i=1;i<=80;i++)
cout<<'-';
}

void getpro()
{
cb=1;
ofstream outfile("Temp.txt");
clrscr();
printf("Write program with last line single char as '~' :\n");
while(cb)
{
printf(": ");
while ((*ch = getchar()) != '\n')
{
if (*ch=='~') cb=0;
else outfile.put(*ch);
}
if (cb==1) outfile.put('\n');
}
outfile.close();
}

void putpro()
{
ifstream infile("Temp.txt");
printf("\nYour Program is :\n");
printf(": ");
while( infile )
{
infile.get(*ch);
printf("%c",*ch);
if(*ch=='\n') printf(": ");
}
printf("\n");
infile.close();
}

void analysis()
{
int tbc,i,j,got;
char str[80],*b="";
char kw[5][6] = {"BEGIN","END","IF","THEN","ELSE"};
char op[7][3] = {"<","<=","==","<>",">",">=","="};
char al[26][2] = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
char dg[10][2] = {"0","1","2","3","4","5","6","7","8","9"};
disdash();
printf("Sr.No\t\tToken\t\t\tCode\t\t\tValue\n");
disdash();
ifstream infile("Temp.txt");
tbc=1;
strcpy(str,b);
while(infile)
{
infile.get(*ch);
if( (*ch==' ')||(*ch=='\n') )
{
if (stricmp(str,b))
{
/* Keword Check */
got=0;
for(i=0;i<5;i++)
{
j = stricmp(str,kw[i]);
if(j==0)
{
printf("%d",tbc);
printf("\t\t%s",str);
printf("\t\t\t%d",i+1);
printf("\t\t\t -\n");
strcpy(str,b);
tbc++;
got=1;
break;
}
}

/* Operator Check */
if (got==0)
{
for(i=0;i<7;i++)
{
j = stricmp(str,op[i]);
if(j==0)
{
printf("%d",tbc);
printf("\t\t%s",str);
printf("\t\t\t8");
printf("\t\t\t %d\n",i+1);
strcpy(str,b);
tbc++;
got=1;
break;
}
}
}
/*Identifier Check*/
if (got==0)
{
for(i=0;i<26;i++)
{
if( (str[0]==*al[0,i]) || (toupper(str[0])==*al[0,i]) )
{
printf("%d",tbc);
printf("\t\t%s",str);
printf("\t\t\t6");
printf("\t\t\t%d\n",&str);
strcpy(str,b);
tbc++;
got=1;
break;
}
else if(str[0] == *dg[0,i])
{
printf("%d",tbc);
printf("\t\t%s",str);
printf("\t\t\t7");
printf("\t\t\t%d\n",&str);
strcpy(str,b);
tbc++;
got=1;
break;
}
}
}

/*Error Msg*/
if(got==0)
{
printf("ERROR :: Invalid sysmbol found.");
printf("%s",str);
printf("Plese enter proper code...");
getch();
infile.close();
fflush(stdin);
exit(0);
}
}
}
else
strcat(str,ch);
}
infile.close();
fflush(stdin);
}

void main()
{
clrscr();
getpro();
putpro();
analysis();
getch();
}

2.

program for top down parsing  [ S->cAd A->ad/d input productions ]
 
#include<iostream.h>
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>

char string[30];
int length,ptr=0,temp,flag;

void Advance() //increment the ptr by 1
{
ptr++;
}

int A(void) //function for Production A
{
temp=ptr;
if (string[ptr]=='a')
{
Advance();
if (string[ptr]=='d')
{
Advance();
return 1;
}
}
ptr=temp;
//failure to find ab in given string
if (string[ptr]=='d')
{
Advance();
return 1;
}
else return 0;
}


int S() //function for production S
{
int val;
if (string[ptr]=='c')
{
Advance();
val=A();
if (val)
if (string[ptr]=='d')
{
Advance();
printf("\nThe given string is accepted by the grammer.");
flag=1;
getch();
return 1;
}
}
return 0;
}



void main()
{
clrscr();
puts("Enter the String:");
gets(string); //get the input string
length=strlen(string);
printf("\n %d",length);
S();
if (flag!=1)
printf("\nThe given string is NOT accepted by the grammer. ");
getch();
}

3.

program for generation of symbol table.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<process.h>

struct node
{
char nav[10],info[10];
struct node *next;
}*first;

void add();
void search();
void showtab();
int del();

void add()
{
struct node *navin;
navin = new node;
cout<<"Enter the name\n";
gets(navin->nav);
cout<<"Enter the info\n";
gets(navin->info);
navin->next=first;
first=navin;
}

void search()
{
char tnav[10];
cout<<"Enter the name to be searched\n";
gets(tnav);
struct node *tmp;
tmp=first;
int tru=1;
while(tmp!=NULL)
{
tru=strcmp(tmp->nav,tnav);
if(!tru)
break;
tmp=tmp->next;
}
if(tmp==NULL)
cout<<"No match found";
else
cout<<tmp->nav<<"\t"<<tmp->info<<"\n";
getch();
}

int del()
{
char tnav[10];
cout<<"Enter the name to be deleted\n";
gets(tnav);
int tru=1;
tru=strcmp(first->nav,tnav);
if(!tru)
{
first=first->next;
return 0;
}
struct node *t,*p;
t=first;
p=first->next;
while(p!=NULL)
{
tru=strcmp(p->nav,tnav);
if(!tru)
{
t->next=p->next;
break;
}
else
{
t=t->next;
p=p->next;
}
}
getch();
return 0;
}

void showtab()
{
struct node *tmp;
tmp=first;
cout<<"Name \t"<<"Info \n";
while(tmp!=NULL)
{
cout<<tmp->nav<<"\t"<<tmp->info<<"\n";
tmp=tmp->next;
}
getch();
}

void main()
{
clrscr();
first=NULL;
int ch;
while(1)
{
clrscr();
cout<<"Type \n1. To add\n2. To del\n3. To search\n4. To show table\n5. To exit\n";
cin>>ch;
switch(ch)
{
case 1:
add();
break;
case 2:
del();
break;
case 3:
search();
break;
case 4:
showtab();
break;
case 5:
exit(0);
break;
default:
break;
}
}
getch();
}
Previous << Home >> Next
   Page last modified:
© 2004  k.skumar &Group. 
     All rights reserved.  
 
      March 14, 2004