Vous êtes sur la page 1sur 52

/*

Compiler
Author
Report Bugs

: Borland TurboC++ 3.0 IDE/ GCC


: CodeBreakers
: Contact@codebreaker.co.in

*/
#include<string.h>
/*************** STACK ADT ******************/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 50
typedef struct pos
{
int *data;
int n;
}pos;
typedef struct node
{
char data;
int num;
pos fpos,lpos;
struct node *left,*right;
}node;
typedef struct stack
{
int top;
node *data[MAX];
}stack;
void init(stack *a)
{
a->top=-1;
}
void push(stack *a,node *data)
{
a->top++;
a->data[a->top]=data;
}
node* pop(stack *a)
{
if(a->top < 0)
return NULL;
return a->data[a->top--];
}

/*************** STACK ADT END ******************/

#define
#define
#define
#define
#define

isopt(a) (a=='|'|| a=='*' || a=='.')


isunary(a) (a=='*')
isleaf(a) (a->left == NULL && a->right==NULL )
isalpha(a) (a>=65 && a<=90 || a>=97 && a<=122 || a=='#')
present strstr

node * createTree(char *str);


void postfix(node *a);
int isNullable(node *a);
void follpos(node *root);
int Union(int **res,int *arr1,int *arr2,int n1,int n2);
int firstpos(node *n,int **set);
int lastpos(node *n,int **set);
int inStates(pos a);
void re2dfa(node *root);
void printState(pos a);
int inStates(pos a);
node *Tree;
pos *followpos;
char *terminals;
pos *dfa[50];
int leaves,states;
char input[50];
int sym_n;
char *sym;

int main()
{
int i,j;
//
clrscr();
printf("\nPostFix Expression need to be terminated with
#.\n\nExample input : ab|*a.b.b.#.");
printf("\n\nEnter a valid PostFix Regular Expression : ");
fflush(stdin);
scanf("%s",input);
Tree = createTree(input);
printf("\n\n%s\t%s\t%s\t%s\n","No","Node","FirstPos","LastPost");

postfix(Tree);
follpos(Tree);
getch();
printf("\n\n\n\nNo.\tFollowPos\n");
for(i=1;i<=leaves;i++)
{
if(followpos[i].n)
{
printf("\n%d\t{",i);
for(j=0;j<followpos[i].n;j++)
printf("%d,",followpos[i].data[j]);
printf("\b}");
}
}
printf("\n\n\n\nRE to DFA Table\n");
printf("\nState\t");
for(i=0;i<sym_n;i++)
printf("%2c\t",sym[i]);
printf("\b\n");
re2dfa(Tree);
printf("\n\n\nPress any key to exit...");
getch();
return 0;
}
void postfix(node *a)
{
int *fpos,i,n;
static int leaf=1;
if(a==NULL)
return;
postfix(a->left);
postfix(a->right);
printf("\n%-2d\t%-4c\t{",(isleaf(a) ? leaf++ :0),a->data);
for(i=0;i<a->fpos.n;i++)
printf("%d,",a->fpos.data[i]);
printf("\b}\t\t{");
for(i=0;i<a->lpos.n;i++)
printf("%d,",a->lpos.data[i]);
printf("\b}");
}
int Union(int **uni,int *arr1,int *arr2,int n1,int n2)
{
int i=0,k=0,j=0;
int *res;

res = (int *)malloc(sizeof(int)*(n1+n2));


while(i<n1 && j<n2)
{
if(arr1[i]<arr2[j])
res[k++]=arr1[i++];
else if(arr1[i]>arr2[j])
res[k++]=arr2[j++];
else
{
res[k++]=arr1[i++];
j++;
}
}
while(i<n1)
res[k++]=arr1[i++];
while(j<n2)
res[k++]=arr2[j++];
*uni = res;
return k;
}
node * createTree(char *str)
{
int i=0,j;
int num=1;
node *nd,*left,*right;
char temp[2];
char a;
stack s;
init(&s);
temp[1]='\0';
leaves=0;
while(str[i]!='\0')
{
a=str[i];
nd=(node *)malloc(sizeof(node));
nd->left = nd->right = NULL;
nd->data=a;
nd->num=0;
if(isalpha(a))
{
nd->num=num++;
nd->fpos.n = firstpos(nd,&(nd->fpos.data));
nd->lpos.n = lastpos(nd,&(nd->lpos.data));
push(&s,nd);
i++;
leaves++;
continue;
}

if(isopt(a))
{
if(isunary(a))
{
nd->left = pop(&s);
}
else
{
nd->right = pop(&s);
nd->left = pop(&s);
}
nd->fpos.n = firstpos(nd,&(nd->fpos.data));
nd->lpos.n = lastpos(nd,&(nd->lpos.data));
push(&s,nd);
i++;
}
}
sym = (char *)calloc(sizeof(char),leaves+2);
terminals = (char *)calloc(sizeof(char),leaves+2);
sym_n=0;
i=0;
j=1;
terminals[0] = ' ';
while(str[i]!='\0')
{
temp[0]=str[i];
if(isalpha(str[i]))
{
terminals[j++]=str[i];
if(!present(sym,temp))
sym[sym_n++]=temp[0];
}
i++;
}
sym_n--;
followpos = (pos *)calloc(sizeof(pos),(leaves+1));
return pop(&s);
}
int isNullable(node *a)
{
if(a==NULL)
return 0;
if(a->data == '*')
return 1;
if(a->left==NULL && a->right==NULL)

return 0;
if(a->data == '|')
return(isNullable(a->left) || isNullable(a->right));
if(a->data == '.')
return(isNullable(a->left) && isNullable(a->right));
return 0;
}
int firstpos(node *n,int **set)
{
int k,n1,n2;
int *arr1,*res;
int *arr2;
k=0;
if(n->num)
{
arr1 = (int *)malloc(sizeof(int)*2);
arr1[0]=n->num;
*set = arr1;
return 1;
}
if(n->data == '|')
{
n1 = firstpos(n->left,&arr1);
n2 = firstpos(n->right,&arr2);
k = Union(&res,arr1,arr2,n1,n2);
free(arr1);
free(arr2);
*set = res;
return k;
}
if(n->data == '.')
{
n1 = firstpos(n->left,&arr1);
if(isNullable(n->left))
{
n2 = firstpos(n->right,&arr2);
k = Union(&res,arr1,arr2,n1,n2);
*set = res;
free(arr1);
free(arr2);
return k;
}
else
{
*set = arr1;
return n1;

}
}
if(n->data=='*')
{
n1 = firstpos(n->left,&arr1);
*set = arr1;
return n1;
}
*set = NULL;
return 0;
}
int lastpos(node *n,int **set)
{
int k,n1,n2;
int *arr1,*res;
int *arr2;
k=0;
if(n->num)
{
arr1 = (int *)malloc(sizeof(int)*2);
arr1[0]=n->num;
*set = arr1;
return 1;
}
if(n->data == '|')
{
n1 = lastpos(n->left,&arr1);
n2 = lastpos(n->right,&arr2);
k = Union(&res,arr1,arr2,n1,n2);
*set = res;
free(arr1);
free(arr2);
return k;
}
if(n->data == '.')
{
n2 = lastpos(n->right,&arr2);
if(isNullable(n->right))
{
n1 = lastpos(n->left,&arr1);
k = Union(&res,arr1,arr2,n1,n2);
*set = res;
free(arr1);
free(arr2);
return k;
}
else
{
*set = arr2;
return n2;

}
}
if(n->data=='*')
{
n1 = lastpos(n->left,&arr1);
*set = arr1;
return n1;
}
*set = NULL;
return 0;
}
void follpos(node *root)
{
int i,j,n,*res,*arr1,num;
node *a,*b;
if(root==NULL)
return;
a=root->left;
b=root->right;
if(root->data=='.')
{
for(i=0;i< a->lpos.n; i++)
{
num = a->lpos.data[i];
n = Union(&res,followpos[num].data,b>fpos.data,followpos[num].n,b->fpos.n);
free(followpos[num].data);
followpos[num].n = n;
followpos[num].data = res;
}
}
if(root->data=='*')
{
for(i=0;i< root->lpos.n; i++)
{
num = root->lpos.data[i];
res = (int *)malloc(sizeof(int)*(followpos[num].n +
root->fpos.n));
n = Union(&res,followpos[num].data,root>fpos.data,followpos[num].n,root->fpos.n);
free(followpos[num].data);
followpos[num].n = n;
followpos[num].data = res;
}
}
follpos(a);
follpos(b);
}
int inStates(pos a)

{
int i,j,same;
for(i=0;i<states;i++)
{
if(a.n != dfa[i][0].n)
continue;
same=1;
for(j=0;j<a.n;j++)
{
if(a.data[j]!=dfa[i][0].data[j])
{
same=0;
break;
}
}
if(same)
return 1;
}
return 0;
}
void printState(pos a)
{
int i;
for(i=0;i<a.n;i++)
printf("%d",a.data[i]);
}
void re2dfa(node *root)
{
int i=0,j,loc,k,n,*res;
char temp[2];
states=1;
dfa[i] = (pos *)calloc(sizeof(pos),sym_n+1);
dfa[i][0].n = root->fpos.n;
dfa[i][0].data = root->fpos.data;
temp[1]='\0';
//printf("\n\n%s\n\n",terminals);
while(i<states)
{
j=0;
k=1;
while(j < dfa[i][0].n)
{
k = dfa[i][0].data[j];
temp[0] = terminals[k];

if(terminals[k] == '#')
{
j++;
continue;
}
loc = present(sym,temp) - sym;
loc++;
n =
Union(&res,followpos[k].data,dfa[i][loc].data,followpos[k].n,dfa[i][loc].n
);
dfa[i][loc].data = res;
dfa[i][loc].n = n;
j++;
}
printf("\n");
getch();
printState(dfa[i][0]);
printf("\t");
for(j=1;j<=sym_n;j++)
{
if(!inStates(dfa[i][j]))
{
dfa[states] = (pos
*)calloc(sizeof(pos),sym_n+1);
dfa[states][0].n = dfa[i][j].n;
dfa[states][0].data = dfa[i][j].data;
states++;
}
printState(dfa[i][j]);
printf("\t");
}
printf("\b");
i++;
}
}

#include<stdio.h>
#include<conio.h>
char Po[25], In[25];
int node[10][10], flps[10][10], position[10][10], DF[10][10];
int NN=1, DD=1, S=0, var=0, terp=1, v;
struct STACK
{
char data;

struct STACK *D;


struct STACK *next,*L,*R;
int FP[10],LP[10];
} *O, *A;
void PUSH(char a)
{
struct STACK *t;
t=(struct STACK *)malloc(sizeof(struct STACK));
t->data=a;
if (O!=NULL) t->next=O;
else
t->next=NULL;
O=t;
}
char POP()
{
char l;
l=O->data;
if(O->next!=NULL) O=O->next;
else
O=NULL;
return l;
}
void PUSHNODE(struct STACK *a)
{
struct STACK *t;
t=(struct STACK *)malloc(sizeof(struct STACK));
t->D=a;
if(O!=NULL) t->next=O;
else
t->next=NULL;
O=t;
}
struct STACK *POPNODE()
{
struct STACK *t;
t=(struct STACK *)malloc(sizeof(struct STACK));
t=O->D;
if(O->next!=NULL) O=O->next;
else
O=NULL;
return t;

}
void INFIX2POSTFIX(char I[25])
{
int i,j,k;
char p;
for(i=0;I[i]!='\0';i++){
if(I[i]=='(')
PUSH('(');
else if(I[i]==')')
while(1){
p=POP();
if(p!='(') Po[v++]=p;
else
break;
}
else if(I[i]=='+'){
while((O->data=='*'||O->data=='+'||O->data=='.')&&O!=NULL)
Po[v++]=POP();
PUSH(I[i]);
}
else if(I[i]=='.'){
while((O->data=='*'||O->data=='.')&&O!=NULL)
Po[v++]=POP();
PUSH('.');
}
else if(I[i]=='*'&&O!=NULL)
PUSH(I[i]);
else Po[v++]=I[i];
}
while(O!=NULL){
p=POP();
if(p!='(') Po[v++]=p;
}
Po[v++]='$';
Po[v++]='.';
Po[v]='\0';
}
void FORMTREE(char Po[])
{
int i,j,k;
struct STACK *N;

O=NULL;
for(i=0;Po[i]!='\0';i++){
N=(struct STACK *)malloc(sizeof(struct STACK));
N->data=Po[i]; N->L=NULL; N->R=NULL; N->next=NULL;
N->FP[0]=0; N->LP[0]=0;
if(Po[i]=='.'||Po[i]=='+'){
N->R=POPNODE();
N->L=POPNODE();
}
else if(Po[i]=='*'){
N->L=POPNODE();
N->R=NULL;
}
PUSHNODE(N);
}
}
void FLPDISP (struct STACK *a)
{
int i;
if(a!=NULL){
FLPDISP(a->L);
FLPDISP(a->R);
S++;
printf("\n %c",a->data);
printf("\nFIRSTPOS -> ");
for(i=1;i<=a->FP[0];i++)
printf(" %d",a->FP[i]);
printf("\nLASTPOS -> ");
for(i=1;i<=a->LP[0];i++)
printf(" %d",a->LP[i]);
printf("\n---------------");
getch();
}
}

void FP_LP_CAL(struct STACK *a)


{
if(a!=NULL){
int i, flg;
FP_LP_CAL(a->L);
FP_LP_CAL(a->R);

if(a->data!='+'&&a->data!='.'&&a->data!='*'){
a->FP[0]++; a->LP[0]++;
a->FP[a->FP[0]]=terp;
a->LP[a->LP[0]]=terp++;
flg=0;
for(i=0;i<var;i++)
if(position[i][0]==a->data){
position[i][1]++;
position[i][position[i][1]+1]=terp-1;
flg=1;
}
if(flg==0){
position[var++][1]=1;
position[var-1][2]=terp-1;
position[var-1][0]=a->data;
}
}
else if(a->data=='+'){
for(i=1;i<=a->L->FP[0];i++){
a->FP[0]++;
a->FP[a->FP[0]]=a->L->FP[i];
}
for(i=1;i<=a->R->FP[0];i++){
if(PRESENT(a->FP, a->R->FP[i],1)==0){
a->FP[0]++;
a->FP[a->FP[0]]=a->R->FP[i];
}
}
for(i=1;i<=a->L->LP[0];i++){
a->LP[0]++;
a->LP[a->LP[0]]=a->L->LP[i];
}
for(i=1;i<=a->R->LP[0];i++)
if(PRESENT(a->LP, a->R->LP[i], 1)==0){
a->LP[0]++;
a->LP[a->LP[0]]=a->R->LP[i];
}
}
else if(a->data=='*'){
for(i=1;i<=a->L->FP[0];i++){
a->FP[0]++;
a->FP[a->FP[0]]=a->L->FP[i];

}
for(i=1;i<=a->L->LP[0];i++){
a->LP[0]++;
a->LP[a->LP[0]]=a->L->LP[i];
}
}
else if(a->data=='.'){
//firstpos
for(i=1;i<=a->L->FP[0];i++){
a->FP[0]++;
a->FP[a->FP[0]]=a->L->FP[i];
}
if(a->L->data=='*')
for(i=1;i<=a->R->FP[0];i++)
if(PRESENT(a->FP, a->R->FP[i],1)==0){
a->FP[0]++;
a->FP[a->FP[0]]=a->R->FP[i];
}
//lastpos
for(i=1;i<=a->R->LP[0];i++){
a->LP[0]++;
a->LP[a->LP[0]]=a->R->LP[i];
}
if(a->R->data=='*')
for(i=1;i<=a->L->LP[0];i++)
if(PRESENT(a->LP,a->L->LP[i],1)==0){
a->LP[0]++;
a->LP[a->LP[0]]=a->L->LP[i];
}
}
}
}
int PRESENT(int p[], int a, int b)
{
int i,j;
for(i=b,j=0;j<p[b-1];i++,j++)
if(p[i]==a)
return 1;
return 0;
}
void FLPCAL(struct STACK *a, int n)

{
int j;
if(a!=NULL){
FLPCAL(a->L,n);
if(a->data=='.'){
if(PRESENT(a->L->LP,n,1)==1)
for(j=1;j<=a->R->FP[0];j++)
if(PRESENT(flps[n],a->R->FP[j],1)==0){
flps[n][1]++;
flps[n][flps[n][1]+1]=a->R->FP[j];
}
}
else if(a->data=='*'){
if(PRESENT(a->LP,n,1)==1)
for(j=1;j<=a->FP[0];j++)
if(PRESENT(flps[n],a->FP[j],1)==0){
flps[n][1]++;
flps[n][flps[n][1]+1]=a->FP[j];
}
}
FLPCAL(a->R,n);
}
}
/***************/
void FOLLOWPOS()
{
int i,j,k;
A=O;
flps[0][0]=terp;
for(i=1;i<=terp;i++){
flps[i][0]=i;
flps[i][1]=0;
FLPCAL(O->D,i);
}
O=A;
}
void DFA()
{
int i,j,k,l,m,fg,find;
int tmp[10], newn[10];
node[0][0]=1;

node[1][0]='A';
node[1][1]=0;
for(i=1;i<=O->D->FP[0];i++){
node[1][1]++;
node[1][node[1][1]+1]=O->D->FP[i];
}
DF[0][0]=var-1;
for(i=1;i<var;i++)
DF[0][i]=position[i-1][0];
for(i=1;i<=node[0][0];i++){
DF[i][0]=node[i][0];
for(j=1;j<=DF[0][0];j++){
tmp[0]=0;
newn[0]=0;
for(k=2;k<=node[i][1]+1;k++){
if(PRESENT(position[j-1],node[i][k],2)==1)
tmp[++tmp[0]]=node[i][k];
for(l=1;l<=tmp[0];l++){
for(m=2;m<=flps[tmp[l]][1]+1;m++)
if(PRESENT(newn,flps[tmp[l]][m],1)==0){
newn[0]++;
newn[newn[0]]=flps[tmp[l]][m];
}
}
}
printf(" ");
fg=0;
for(l=1;l<=node[0][0];l++){
find=0;
if(newn[0]==node[l][1])
for(k=1;k<=newn[0];k++)
if(PRESENT(node[l],newn[k],2)==0)
fg=1;
else find++;
if(find==newn[0]&&find!=0)
goto l1;
}
if(newn[0]==0){
DF[i][j]='-';
goto l2;
}
node[0][0]++;

node[node[0][0]][0]=node[node[0][0]-1][0]+1;
for(m=0;m<=newn[0];m++)
node[node[0][0]][m+1]=newn[m];
DF[i][j]=node[node[0][0]][0];
goto l2;
l1:
DF[i][j]=node[l][0];
l2: if(fg==1||fg==0) printf(" ");
}
}
printf("\n\nNODES:\n\n");
for(i=1;i<=node[0][0];i++,printf("\n\n"))
for(j=0;j<=node[i][1]+1;j++)
if(j==0)
printf(" %c ",node[i][j]);
else if(j==1)
printf(" => ");
else
printf(" %d ",node[i][j]);
printf("\n\nDFA TABLE:\n\n");
for(i=0;i<=node[0][0];i++,printf("\n\n"))
for(j=0;j<=DF[0][0];j++)
if(i==0&&j==0)
printf(" ");
else
printf("%4c",DF[i][j]);
}

void main()
{
int i,j,k, gd, gm;
int ch;
O=NULL; A=NULL; v=0;
do{
clrscr();
printf("\n----------------------------");
printf("\n
RE -> DFA ");
printf("\n----------------------------");
printf("\n 1. ENTER R.E.");
printf("\n 2. POSTFIX EXPRESSION ");

printf("\n 3. VIEW FOLLOWPOS");


printf("\n 4. VIEW DFA ");
printf("\n 5. QUIT");
printf("\n---------------------------");
printf("\n\n ENTER YOUR CHOICE : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n\n ENTER REGULAR EXPRESSION : ");
scanf("%s",&In);
break;
case 2:
INFIX2POSTFIX(In);
printf("\n\n POSTFIX EXPRESSION : \n\n\t%s",Po);
O=NULL;
FORMTREE(Po);
break;
case 3:
FP_LP_CAL(O->D);
FLPDISP(O->D);
FOLLOWPOS();
printf("\n\n FOLLOWPOS :\n\n");
for(i=1;i<terp;i++)
{
for(j=0;j<=flps[i][1]+1;j++)
if(j!=1)
printf(" %d ",flps[i][j]);
printf("\n");
}
break;
case 4:
printf("\n\n POSITION MATRIX :\n\n");
for(i=0;i<var;i++)
{
for(j=0;j<=position[i][1]+1;j++)
if(j==0)
printf(" %c ",position[i][j]);
else if (j!=1)
printf(" %d ",position[i][j]);
printf("\n\n");
}

DFA();
break;
case 5:
printf("\n Thanks.....\a");
break;
default :
printf("\n\n SORRY!! WRONG CHOICE!\a");
break;
}
}while(ch!=5);
getch();
}

Conversion of a Regular Expression to NFA Algorithm Source code C


programming
CS342 Compiler Lab Source code Algorithm C Programming
#include<stdio.h>
#include<conio.h>
void main()
{
char reg[20];
int q[20][3],i,j,len,a,b;
clrscr();
for(a=0;a<20;a++)
{
for(b=0;b<3;b++)
{
q[a][b]=0;
}
}
printf("Regular expression: \n");
scanf("%s",reg);
len=strlen(reg);
i=0;
j=1;
while(i<len)
{
if(reg[i]=='a'&[i+1]!='/'&[i+1]!='*')
{
q[j][0]=j+1;
j++;
}
if(reg[i]=='b'&[i+1]!='/'&[i+1]!='*')
{
q[j][1]=j+1;
j++;
}
if(reg[i]=='e'&[i+1]!='/'&[i+1]!='*')
{
q[j][2]=j+1;
j++;
}
if(reg[i]=='a'&[i+1]=='/'&[i+2]=='b')
{
q[j][2]=((j+1)*10)+(j+3);
j++;
q[j][0]=j+1;
j++;

q[j][2]=j+3;
j++;
q[j][1]=j+1;
j++;
q[j][2]=j+1;
j++;
i=i+2;
}
if(reg[i]=='b'&[i+1]=='/'&[i+2]=='a')
{
q[j][2]=((j+1)*10)+(j+3);
j++;
q[j][1]=j+1;
j++;
q[j][2]=j+3;
j++;
q[j][0]=j+1;
j++;
q[j][2]=j+1;
j++;
i=i+2;
}
if(reg[i]=='a'&[i+1]=='*')
{
q[j][2]=((j+1)*10)+(j+3);
j++;
q[j][0]=j+1;
j++;
q[j][2]=((j+1)*10)+(j-1);
j++;
}
if(reg[i]=='b'&[i+1]=='*')
{
q[j][2]=((j+1)*10)+(j+3);
j++;
q[j][1]=j+1;
j++;
q[j][2]=((j+1)*10)+(j-1);
j++;
}
if(reg[i]==')'&[i+1]=='*')
{
q[0][2]=((j+1)*10)+1;
q[j][2]=((j+1)*10)+1;
j++;
}

i++;
}
printf("Transition function \n");
for(i=0;i<=j;i++)
{
if(q[i][0]!=0)
printf("\n q[%d,a]-->%d",i,q[i][0]);
if(q[i][1]!=0)
printf("\n q[%d,b]-->%d",i,q[i][1]);
if(q[i][2]!=0)
{
if(q[i][2]<10)
printf("\n q[%d,e]-->%d",i,q[i][2]);
else
printf("\n q[%d,e]-->%d & %d",i,q[i][2]/10,q[i][2]%10);
}
}
getch();
}

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
struct current{int first,last;}stat[15];
int l,j,change,n=0,i=0,state=1,x,y,start,final;
char store,*input1,input[15];
clrscr();
printf("\n\n****ENTER THE REGULAR EXPRESSION****\n\n");
scanf("%s",input1);/*ex inputs:1.(a*) 2.(a|b) 3.(a.b) 4.((a|b).(a*))*/
for(i=0;i<10;i++)
input[i]=NULL;
l=strlen(input1);
a:
for(i=0;input1[i]!=')';i++);
for(j=i;input1[j]!='(';j--);
for(x=j+1;x<i;x++)
if(isalpha(input1[x]))
input[n++]=input1[x];
else if(input1[x]!='0')
store=input1[x];
input[n++]=store;
for(x=j;x<=i;x++)

input1[x]='0';
if(input1[0]!='0')
goto a;
printf("\n\n\tFROM\tTO\tINPUT\n\n");
i=0;
while(input[i]!='\0')
{
if(isalpha(input[i]))
{
stat[i].first=state++;
stat[i].last=state++;
printf("\n\t%d\t%d\t%c",stat[i].first,stat[i].last,input[i]);
}
else
{
change=0;
switch(input[i])
{
case'|':
stat[i].first=state++;
stat[i].last=state++;
x=i-2;
y=i-1;
if(!isalpha(input[y]))
{
b:
switch(input[y])
{
case'*':if(!isalpha(input[y-1]))
{
y=y-1;
goto b;
}
else
x=y-2;
break;
case'|':x=y-3;
break;
case '.':x=y-3;break;
}
change=1;
}
if(!isalpha(input[y]&&change==0))
c:switch(input[x])
{
case '*':

if(!isalpha(input[x-1]))
{x=x-1;goto c;
}
else x=x-2;
break;
case'|':x=x-2;
break;
case '.':x=x-3;
break;
}
printf("\n\t%d\t%d\tE",stat[i].first,stat[x].first);
printf("\n\t%d\t%d\tE",stat[x].last,stat[i].last);
printf("\n\t%d\t%d\tE",stat[i].first,stat[i-1].first);
printf("\n\t%d\t%d\tE",stat[i-1].last,stat[i].last);
start=stat[i].first;
final=stat[i].last;
break;
case'.':
x=i-2;
y=i-1;
if(!isalpha(input[y]))
{
d:
switch(input[y])
{
case'*':if(!isalpha(input[y-1]))
{
y=y-1;
goto d;
}
else
x=y-2;
break;
case'|':x=y-3;
break;
case '.':x=y-3;
break;
}
change=1;
}
if(!isalpha(input[y]&&change==0))
e:switch(input[x])
{
case'*':
if(!isalpha(input[x-1]))
{

x=x-1;
goto e;
}
else x=x-2;
break;
case'|':x=x-3;
break;
case'.':x=x-3;
break;
}
stat[i].last=stat[y].last;
stat[i].first=stat[x].first;
printf("\n\t%d\t%d\tE",stat[x].last,stat[i-1].first);
start=stat[x].first;
final=stat[i-1].last;
break;
case'*':
stat[i].first=state++;
stat[i].last=state++;
printf("\n\t%d\t%d\tE",stat[i].first,stat[i-1].first);
printf("\n\t%d\t%d\tE",stat[i].first,stat[i].last);
printf("\n\t%d\t%d\tE",stat[i-1].last,stat[i-1].first);
printf("\n\t%d\t%d\tE",stat[i-1].last,stat[i].last);
start=stat[i].first;
final=stat[i].last;
break;
}}
i++;
}
printf("\n the starting state is %d",start);
printf("\n the final state is %d",final);
getch();
}

OUTPUT:

****ENTER THE REGULAR EXPRESSION****


((a|b)*)
****NFA FOR THE GIVEN REGULAR EXPRESSION****
FROM
TO
INPUT
1
2
a
3
4
b
5
1
E

2
5
4
7
7
6
6

6
3
6
5
8
5
8

E
E
E
E
E
E
E

The starting state is 7


The final state is 8

Conversion of a Regular Expression to NFA Algorithm Source


code C programming
CS342 Compiler Lab Source code Algorithm C Programming
#include<stdio.h>
#include<conio.h>
void main()
{
char reg[20];
int q[20][3],i,j,len,a,b;
clrscr();
for(a=0;a<20;a++)
{
for(b=0;b<3;b++)
{
q[a][b]=0;
}
}
printf("Regular expression: \n");
scanf("%s",reg);
len=strlen(reg);
i=0;
j=1;
while(i<len)
{
if(reg[i]=='a'&[i+1]!='/'&[i+1]!='*')
{
q[j][0]=j+1;
j++;
}
if(reg[i]=='b'&[i+1]!='/'&[i+1]!='*')
{
q[j][1]=j+1;
j++;
}
if(reg[i]=='e'&[i+1]!='/'&[i+1]!='*')
{

q[j][2]=j+1;
j++;
}
if(reg[i]=='a'&[i+1]=='/'&[i+2]=='b')
{
q[j][2]=((j+1)*10)+(j+3);
j++;
q[j][0]=j+1;
j++;
q[j][2]=j+3;
j++;
q[j][1]=j+1;
j++;
q[j][2]=j+1;
j++;
i=i+2;
}
if(reg[i]=='b'&[i+1]=='/'&[i+2]=='a')
{
q[j][2]=((j+1)*10)+(j+3);
j++;
q[j][1]=j+1;
j++;
q[j][2]=j+3;
j++;
q[j][0]=j+1;
j++;
q[j][2]=j+1;
j++;
i=i+2;
}
if(reg[i]=='a'&[i+1]=='*')
{
q[j][2]=((j+1)*10)+(j+3);
j++;
q[j][0]=j+1;
j++;
q[j][2]=((j+1)*10)+(j-1);
j++;
}
if(reg[i]=='b'&[i+1]=='*')
{
q[j][2]=((j+1)*10)+(j+3);
j++;
q[j][1]=j+1;
j++;
q[j][2]=((j+1)*10)+(j-1);
j++;
}
if(reg[i]==')'&[i+1]=='*')
{
q[0][2]=((j+1)*10)+1;
q[j][2]=((j+1)*10)+1;
j++;
}
i++;

}
printf("Transition function \n");
for(i=0;i<=j;i++)
{
if(q[i][0]!=0)
printf("\n q[%d,a]-->%d",i,q[i][0]);
if(q[i][1]!=0)
printf("\n q[%d,b]-->%d",i,q[i][1]);
if(q[i][2]!=0)
{
if(q[i][2]<10)
printf("\n q[%d,e]-->%d",i,q[i][2]);
else
printf("\n q[%d,e]-->%d & %d",i,q[i][2]/10,q[i][2]%10);
}
}
getch();
}

Implementation Of Lexical Analyzer Algorithm Source C


Programming CS342 Compiler Lab
Algorithm Source code For Lexical Analyzer CS342 Compiler Lab
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define size 128
#define NONE -1
#define EOS '\0'
#define NUM 256
#define keyword 257
#define punt 258
#define id 259
#define assign 260
#define arith 200
#define rel_op 261
#define DONE 262
#define MAX 999
char buffer[size],lexemes[MAX];
int lastchar=-1,lastentr y=0,tokenval=NONE,lineno=1;
struct entry
{
char *lexptr;
int token;
}symtable[100];
struct entry keywords[]={"if",keyword,"else",keyword,"for",keyword,"int",keyword,"float",
keyword,"double",keyword,"struct",keyword,"return",keyword,0,0};
int look_up(char s[])
{
int k;
for(k=lastentr y;k>0;k=k -1)
if(strcmp(symtable[k].lexptr,s)==0)

return k;
return 0;
}
int insert(char s[],int tok)
{
int len;
len=strlen(s);
lastentry=lastentr y+1;
symtable[lastentr y].token=tok;
symtable[lastentr y].lexptr=&lexemes[lastchar+1];
lastchar=lastchar+len+1;
strcpy(s ymtable[lastentry].lexptr,s);
return lastentr y;
}
void initialize()
{
struct entry *ptr;
for(ptr=keywords;ptr->token;ptr++)
insert(ptr->lexptr,ptr->token);
}
int lexer()
{
int t,val,i=0;
while(1)
{
t=getchar();
if(t==' '||t==' \t')
;
else if(t=='\n')
lineno=lineno+1;
else if(t=='('||t==')' ||t==','||t==';'||t=='{'||t=='}')
return punt;
else if(t=='<'||t=='>')
return rel_op;
else if(t=='+'||t=='-'||t=='*'||t=='/')
return arith;
else if(t=='=')
return assign;
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;
}
buffer[i]=EOS;
if(t!=EOF)
ungetc(t,stdin);
val=look_up(buffer);

if(val==0)
val=insert(buffer,id);
tokenval=val;
return sym table[val].token;
}
else if(t==EOF)
return DONE;
else
{
tokenval=NONE;
return t;
}
}
}
void main()
{
int lookahead;
char ans;
clrscr();
initialize();
printf("\n Enter the expression \n Press ctrl z to terminate... \n");
lookahead=lexer();
while(lookahead!=DONE)
{
if(lookahead==NUM)
printf("\n Number:%d",tokenval);
if(lookahead==arith)
printf("\n Arithmetic Operator");
if(lookahead==punt)
printf("\n Punctuation Symbol");
if(lookahead==id)
printf("\n Identifier:%s",symtable[tokenval].lexptr);
if(lookahead==keyword)
printf("\n Keyword");
if(lookahead==assign)
printf("\n Assignment operator");
if(lookahead==rel_op)
printf("\n Relational operator");
lookahead=lexer();
}
}
Implementation of lexical analyzer using LEX Algorithm CS342 Compiler Lab
%{
#include<stdio.h>
%}
%%
[0-9]+ {printf("\n %s is a number \n",yytext);}
int |
float |
long |
double |
case |
switch |
break |

if |
else |
exit |
continue |
struct |
const |
atof |
atoi |
typedef |
return |
printf |
scanf |
void {printf("\n %s is the keyword \n",yytext);}
[a-zA-Z][a-zA-Z0-9]* {printf("\n %s is an identifier \n",yytext);}
= {printf("\n %s is an assignment operator \n",yytext);}
\>= |
\> |
\< |
== {printf("\n %s is a relational operator \n",yytext);}
\+ |
\- |
\* |
\/ {printf("\n %s is an arithmetic operator \n",yytext);}
; |
"(" |
")" |
"{" |
"}" {printf("\n %s is a s ymbol\n",yytext);}
"/*" {printf("\n %s is a comment line\n",yytext);}
%%
int main(int argc,char **argv)
{
if(argc>1)
{
FILE *file;
file=fopen(argv[1],"r");
if(!file)
{
printf("Could not open %s",argv[1]);
exit(0);
}
yyin=file;
}
yylex();
printf("\n\n");
return 0;
}

Token Separation Source code c programming CS342 Compiler


Lab
Algorithm Source Token Separation Source code C Programming
#include<stdio.h>

#include<conio.h>
#include<graphics.h>
#include<string.h>
struct str
{
char a[30];
}s;
void main()
{
FILE *fp;
int j,f=0,flag,gd,gm;
char p[15][15]={"stdio.h","include","main","math.h","string.h",
"graphics.h","void","int","float","char"};
char q[15][15]={"(",")","[","]","{","}",":",".","#",";",",","?"};
char r[15][15]={"+","-","*","/","<",">","%","="};
gd=DETECT;
initgraph(&gd,&gm,"");
gotox y(28,2);
printf("Token Seperation");
rectangle(3,35,530,475);
rectangle(3,35,100,475);
rectangle(200,35,300,475);
rectangle(300,35,400,475);
rectangle(3,35,530,75);
gotox y(3,4);
printf("KEYW ORDS");
gotox y(16,4);
printf("PUNCT.SYMS");
gotox y(28,4);
printf("OPERATORS");
gotox y(40,4);
printf("CONSTANTS");
gotox y(53,4);
printf("IDENTIFIERS");
fp=fopen("add.c","r");
f=7;
do
{
fscanf(fp,"%s",s.a);
for(j=0;j<=15;j++)
{
if(strcmp(s.a,p[j])==0)
flag=0;
}
for(j=0;j<=15;j++)
{
if(strcmp(s.a,q[j])==0)
flag=1;
}
for(j=0;j<=15;j++)
{
if(strcmp(s.a,r[j])==0)
flag=2;
}
if(atoi(s.a)>0||atoi(s.a)<0)

flag=3;
printf("\n");
switch(flag)
{
case 0:
gotox y(5,f);
printf("%s",s.a);
break;
case 1:
gotox y(17,f);
printf("%s",s.a);
break;
case 2:
gotox y(29,f);
printf("%s",s.a);
break;
case 3:
gotox y(41,f);
printf("%s",s.a);
break;
default:
gotox y(53,f);
printf("%s",s.a);
}
f++;
flag=-1;
}while(!feof(fp));
getch();
closegraph();
}
Token Separation Algorithm Source CS342 Compiler Lab
Input File: add.c
void main ( )
{
int x = 6 ;
int y = 4 ;
x = x + y ;
}

Converting NFA To DFA Compile Design in C Source Code


Programming
Include the Necessar y Package, Declare the variable in array. I use the checke(char a),
push(char a), pop(),pushd(char *a) function to perform the NFA to DFA conversion.
Algorithm Source Code NFA to DFA Example
#include<iostream.h>
#include<string.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
char nfa[50][50],s[20],st[10][20],eclos[20],input[20];
int x,e,top=0,topd=0,n=0,ns,nos,in;
int checke(char a)

{
int i;
for(i=0;i<e;i++)
{
if(eclos[i]==a)
return i;
}
return -1;
}
int check(char a)
{
int i;
for(i=0;i<in;i++)
{
if(input[i]==a)
return i;
}
return -1;
}
void push(char a)
{
s[top]=a;
top++;
}
char pop()
{
top--;
return s[top];
}
void pushd(char *a)
{
strcpy(st[topd],a);
topd++;
}
char *popd()
{
topd--;
return st[topd];
}
int ctoi(char a)
{
int i=a-48;
return i;
}
char itoc(int a)
{
char i=a+48;
return i;
}
char *eclosure(char *a)
{
int i,j;
char c;
for(i=0;i<strlen(a);i++)
push(a[i]);

e=strlen(a);
strcpy(eclos,a);
while(top!=0)
{
c=pop();
for(j=0;j<ns;j++)
{
if(nfa[ctoi(c)][j]=='e')
{
if(check(itoc(j))==-1)
{
eclos[e]=itoc(j);
push(eclos[e]);
e++;
}
}
}
}
eclos[e]='\0';
return eclos;
}
void main()
{
int i,j,k,count;
char ec[20],a[20],b[20],c[20],dstates[10][10];
clrscr();
cout<<"Enter the number of states"<<endl;
cin>>ns;
for(i=0;i<ns;i++)
{
for(j=0;j<ns;j++)
{
cout<<"Move["<<i<<"]["<<j<<"]";
cin>>nfa[i][j];
if(nfa[i][j]!='-'&&nfa[i][j]!='e')
{
if((check(nfa[i][j]))==-1)
input[in++]=nfa[i][j];
}
}
}
topd=0;
nos=0;
c[0]=itoc(0);
c[1]='\0';
pushd(eclosure(c));
strcpy(dstates[nos],eclosure(c));
for(x=0;x<in;x++)
cout<<"\t"<<input[x];
cout<<"\n";
while(topd>0)
{
strcpy(a,popd());
cout<<a<<"\t";
for(i=0;i<in;i++)

{
int len=0;
for(j=0;j<strlen(a);j++)
{
int x=ctoi(a[j]);
for(k=0;k<ns;k++)
{
if(nfa[x][k]==input[i])
ec[len++]=itoc(k);
}
}
ec[len]='\0';
strcpy(b,eclosure(ec));
count=0;
for(j=0;j<=nos;j++)
{
if(strcmp(dstates[j],b)==0)
count++;
}
if(count==0)
{
if(b[0]!='\0')
{
nos++;
pushd(b);
strcpy(dstates[nos],b);
}
}
cout<<b<<"\t";
}
cout<<endl;
}
getch();
}
OUTPUT NFA to DFA Example
Enter the number of states
5
Move[0][0]Move[0][1]e
Move[0][2]Move[0][3]e
Move[0][4]Move[1][0]Move[1][1]Move[1][2]a
Move[1][3]Move[1][4]Move[2][0]Move[2][1]e
Move[2][2]Move[2][3]e
Move[2][4]Move[3][0]Move[3][1]-

Move[3][2]Move[3][3]Move[3][4]b
Move[4][0]Move[4][1]Move[4][2]Move[4][3]Move[4][4]a b
013 213 4
4
213 213 4
OUTPUT NFA to DFA

Enter the number of states


6
Move[0][0]Move[0][1]a
Move[0][2]Move[0][3]Move[0][4]Move[0][5]Move[1][0]Move[1][1]Move[1][2]b
Move[1][3]Move[1][4]Move[1][5]Move[2][0]Move[2][1]Move[2][2]Move[2][3]a
Move[2][4]e
Move[2][5]Move[3][0]Move[3][1]Move[3][2]c
Move[3][3]Move[3][4]e
a b
0 1
1 24
24 3244 5
5
3244 3244 55
55

Top Down Parsing in Complier Design Source Code


Programming
First include the package and Necessary variable. I use the following function int
parse::scannt(char a), void parse::input(),int parse::scant(char b), void
parse::process(),void parse::input(), To perform the operation Processing.
Compiler Design Source code Programming
#include<iostream.h>
#include<conio.h>
#include<string.h>
class parse
{
int nt,t,m[20][20],i,s,n,p1,q,k,j;
char p[30][30],n1[20],t1[20],ch,b,c,f[30][30],fl[30][30];
public:
int scant(char);
int scannt(char);
void process();
void input();
};
int parse::scannt(char a)
{
int c=-1,i;
for(i=0;i<nt;i++)
{
if(n1[i]==a)
{
return i;
}
}
return c;
}
int parse::scant(char b)
{
int c1=-1,j;
for(j=0;j<t;j++)
{
if(t1[j]==b)
{
return j;
}
}
return c1;
}

void parse::input()
{
cout<<"Enter the number of productions:";
cin>>n;
cout<<"Enter the productions one by one"<<endl;
for(i=0;i<n;i++)
cin>>p[i];

nt=0;
t=0;
}
void parse::process()
{
for(i=0;i<n;i++)
{
if(scannt(p[i][0])==-1)
n1[nt++]=p[i][0];
}
for(i=0;i<n;i++)
{
for(j=3;j<strlen(p[i]);j++)
{
if(p[i][j]!='e')
{
if(scannt(p[i][j])==-1)
{
if((scant(p[i][j]))==-1)
t1[t++]=p[i][j];
}
}
}
}
t1[t++]='$';
for(i=0;i<nt;i++)
{
for(j=0;j<t;j++)
m[i][j]=-1;
}
for(i=0;i<nt;i++)
{
cout<<"Enter first["<<n1[i]<<"]:";
cin>>f[i];
}
for(i=0;i<nt;i++)
{
cout<<"Enter follow["<<n1[i]<<"]:";
cin>>fl[i];
}
for(i=0;i<n;i++)
{
p1=scannt(p[i][0]);
if((q=scant(p[i][3]))!=-1)
m[p1][q]=i;
if((q=scannt(p[i][3]))!=-1)
{
for(j=0;j<strlen(f[q]);j++)
m[p1][scant(f[q][j])]=i;
}
if(p[i][3]=='e')
{
for(j=0;j<strlen(fl[p1]);j++)
m[p1][scant(fl[p1][j])]=i;

}
}
for(i=0;i<t;i++)
cout<<"\t"<<t1[i];
cout<<endl;
for(j=0;j<nt;j++)
{
cout<<n1[j];
for(i=0;i<t;i++)
{
cout<<"\t"<<" ";
if(m[j][i]!=-1)
cout<<p[m[j][i]];
}
cout<<endl;
}
}
void main()
{
clrscr();
parse p;
p.input();
p.process();
getch();
}
Top Down Parsing in Complier Design output
Enter the number of productions:8
Enter the productions one by one
E->TA
A->+TA
A->e
T->FB
B->e
B->*FB
F->(E)
F->i
Enter first[E]: (i
Enter first[A]: +e
Enter first[T]: (i
Enter first[B]: *e
Enter first[F]: (i
Enter follow[E]: $)
Enter follow[A]: $)
Enter follow[T]: +)$
Enter follow[B]: +)$
Enter follow[F]: +*)$
+ ( ) i * $
E E->TA E->TA
A A->+TA A->e A->e
T T->FB T->FB
B B->e B->e B->*FB B->e
F F->(E) F->i
Top down Parsing in Compiler Design Output
Enter the no of productions:

8
Enter the production one by one:
E->TX
X->+TX
X->e
T->FY
Y->*FY
Y->e
F->(E)
F->i
Enter FIRST[E](i
Enter FIRST[X]+e
Enter FIRST[T](i
Enter FIRST[Y]*e
Enter FIRST[F](i
Enter FOLLOW [E])$
Enter FOLLOW [X])$
Enter FOLLOW [T]+)$
Enter FOLLOW [Y]+)$
Enter FOLLOW [F]*+)$
+ * ( ) i $
E E->TX E->TX
X X->+TX X->e X->e
T T->FY T->FY
Y Y->e Y->*FY Y->e Y->e
F F->(E) F->i

How To Write Token Separation Source Code Programming


Include the necessar y package. Declare array for the exp,key,ptr.
Source code In C programming | Token Separation Project
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
void main()
{
char exp[50]="\0",con[50]="\0",kwd[50]="\0",id[50]="\0",sym [50]="\0",opr[50]="\0";
char key[6][10]={"if","for","do","while","int","float"};
char ch;
char ptr[10][10]={"\0"};
int i=0,j=0,k=-1,n=-1,p=-1,s=-1;
clrscr();
puts("Enter the expression for lexical analysis");
gets(exp);
puts("\nThe tokens are");
do
{
ch=exp[i];
if(isalpha(ch))
{
k=-1;
ptr[++n][++k]=ch;

i++;
ch=exp[i];
if(isalpha(ch)||isdigit(ch))
{
while(isalpha(ch)||isdigit(ch))
{
ptr[n][++k]=ch;
i++;
ch=exp[i];
}
while(j<6)
{
if(strcmp(key[j],ptr[n])==0)
{
ptr[n][++k]=' ';
strcat(k wd,ptr[n]);
break;
}
if(j==5)
{
ptr[n][++k]=' ';
strcat(id,ptr[n]);
}
j++;
}
}
else
{
ptr[n][++k]=' ';
strcat(id,ptr[n]);
}
i--;
ch=exp[i];
j=0;
}
else if(isdigit(ch))
{
k=-1;
ptr[++n][++k]=ch;
i++;
ch=exp[i];
if(isdigit(ch))
{
while(isdigit(ch))
{
ptr[n][++k]=ch;
i++;
ch=exp[i];
}
}
i--;
ch=exp[i];
ptr[n][++k]=' ';
strcat(con,ptr[n]);
}
else if(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='%'||ch=='>'||ch=='<'||ch=='='||ch=='!')

{
opr[++p]=ch;
i++;
ch=exp[i];
if(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='%'||ch=='>'||ch=='<'||ch=='='||ch=='!')
{
opr[++p]=ch;
}
else
{
i--;
ch=exp[i];
opr[++p]=' ';
}
}
else
{
sym[++s]=ch;
sym[++s]=' ';
}
i++;
}while(exp[i]!='\0');
puts("\nKeyword:");
puts(kwd);
puts("\nIdentifier:");
puts(id);
puts("\nConstant:");
puts(con);
puts("\nOperator:");
puts(opr);
puts("\nSymbol:");
puts(sym);
getch();
}
Example Compiler Design Output Result Token Separation
Enter the expression for lexical analysis
if(a/b==10)
The tokens are
Keyword:
if
Identifier:
a b
Constant:
10
Operator:
/ ==
Symbol:
( )
Example Output Result Token separation
Enter the expression for lexical analysis
int a=1;float b;
The tokens are
Keyword:
int float
Identifier:
a b

Constant:
1
Operator:
=
Symbol:
;

How To Write Shift Reduce Parsing Source Code in C


Include the Necessar y Package ctype.h, string.h, and conio.h. Then I declare the
Variable in the structure. Then get the input in the for loop.
Example Source Code Programming | Shift Reduce Parsing Projec t
#include<stdio.h>
#include<iostream.h>
#include<ctype.h>
#include<string.h>
#include<conio.h>
struct stru1
{
char non_ter[1],pro[25];
}cfg[25];
int n,st=-1,j,i,t=-1,m;
int v,c,p=1;
char str[20],stack[20],ch,tmp[10];
void match(int k);
void matchl(int k);
void main()
{
clrscr();
cprintf("Enter the number of productions: \n\r");
cscanf("%d",&n);
cprintf("\n\r");
cprintf("Enter the productions on LEFT and RIGHT sides: \n\r");
for(i=0;i<n;i++)
{
cscanf("%s",cfg[i].non_ter);
cprintf("\n\r");
cprintf("->\n\r");
cscanf("%s",cfg[i].pro);
cprintf("\n\r");
}
cprintf("Enter the input string: \n\r");
cscanf("%s",str);
cprintf("\n\r");
i=0;
do
{
ch=str[i];
stack[++st]=ch;
tmp[0]=ch;
match(1);
i++;

}while(str[i]!='\0');
c=st;
v=st;
cputs(stack);
cprintf("\n\r");
while(st!=0)
{
v=--st;
t=-1;
p=0;
while(v<=c)
{
tmp[++t]=stack[v++];
p++;
}
matchl(p);
}
cfg[0].non_ter[1]='\0';
if(strcmp(stack,cfg[0].non_ter)==0)
cprintf("String is present in Grammar G \n\r");
else
cprintf("String is not present in Grammar G \n\r");
}
void match(int k)
{
for(j=0;j<n;j++)
{
if(strlen(cfg[j].pro)==k)
{
if(strcmp(tmp,cfg[j].pro)==0)
{
stack[st]=cfg[j].non_ter[0];
break;
}
}
}
}
void matchl(int k)
{
int x=1,y;
y=k-1;
for(j=0;j<n;j++)
{
if(strlen(cfg[j].pro)==k)
{
if(strcmp(tmp,cfg[j].pro)==0)
{
k=c-k+1;
stack[k]=cfg[j].non_ter[0];
do
{
stack[k+x]='\0';
tmp[t--]='\0';
c--;
x++;

}while(x<=y);
tmp[t]='\0';
cputs(stack);
cprintf("\n\r");
break;
}
}
}
}
Example Shift Reduce Parsing Output Result
Enter the number of productions:
3
Enter the productions o n LEFT and RIGHT sides:
E
->
a
E
->
E+E
E
->
E*E
Enter the input string:
a*a+a
E*E+E
E*E
E
String is present in Grammar G
Output Result Shift Reduce Parsing Project
Enter the number of productions:
2
Enter the productions on LEFT and RIGHT sides:
E
->
id
E
->
E+E
Enter the input string:
id+id
E+E
E

MONDAY, MARCH 2, 2009

OPERATOR PRECEDENCE PARSER

/* PROGRAM FOR OPERATOR PRECEDENCE PARSER */


#include
#include
#include
void main()
{
int i,j,x,x1,k,k1,z1,z;
char a[20],b[20][20],c[20],p[10]="*+$";
char fp[10][10]={"id1","id2","id3"},d[10];
clrscr();
printf("ENTER INPUT STRING: ");
gets(a);
i=0;j=0;x=0;x1=0;
printf("$ ");
while(a[i]!='\0')
{
j=0;
while(1)
{
if(a[i]!='+' && a[i]!='*' && a[i]!='$')
{
b[x][j]=a[i];
i++;
j++;
}
else
{
c[x]=a[i];
break;
}
}
b[x][j]='\0';
for(k=0;k<3;k++)
if(!strcmp(b[x],fp[k]))
for(k1=0;k1<3;k1++)
if(c[x]==p[k1])
printf("< %s > %c ",b[x],c[x]);
x++;
i++;
}
c[x]='\0';
i=0;
printf("\n$ <");
while(c[i]!='\0')
{
for(k=0;k<3;k++)
if(c[i]==p[k])
z=k;
i++;

printf(" %c",c[i-1]);
for(k=0;k<3;k++)
if(c[i]==p[k])
z1=k;
i++;
if(z>z1)
printf(" <");
else
printf(" >");
printf(" %c",c[i-1]);
i++;
}
printf(" > $");
for(i=0;i<3;i++)
{
if(c[i]==p[0])
c[i]=' ';
}
i=0;
printf("\n$ <");
for(k=0;k<3;k++)
if(c[i]==p[k])
z=k;
printf(" %c",c[i]);
i++;
i++;
for(k=0;k<3;k++)
if(c[i]==p[k])
z1=k;
if(z>z1)
printf(" <");
else
printf(" >");
printf(" %c",c[i]);
getch();
}

MONDAY, MARCH 2, 2009

Lexical Analysis
/*Program for Lexical Analyzer*/
#include
#include
#include
#include

//char symbol[15]={'=','+','-','*','/','#','!','(',')','[',']','}','{'};
char key[15][10]={"include","exit","extern",
"break","int","char",
"float","void","for",
"if","while","do",
"else","goto"};
char delim[14]={' ','\t','\n',',',';','(',')','{','}','[',']'};
char oper[10]={'+','-','*','/','%','=','!','<','>'};
char header[8][15]={"stdio.h","conio.h","malloc.h","process.h","string.h","ctype.h"};
char digit[10]={'0','1','2','3','4','5','6','7','8','9'};
FILE *fp;
void token(char *);
char *str="";

void main()
{
clrscr();
fp=fopen("e:\tc\ADD7.cpp","r"); //expt.cpp is an input file
printf("\nOutput of program is:\n\n");
char *ch,c='\0';
ch = &c;
clrscr();
fp=fopen("E:\ADD7.cpp","r");
if(fp==NULL)
{
printf("\n\n Cannot Open The Source File.......");
exit(0);
}
*ch=getc(fp);
while(*ch!=EOF)
{
if(*ch==' ')
{
token(str);
strcpy(str,"");
}
else
{
str=strcat(str,&c);
}
*ch=fgetc(fp);
}
}
void token(char *p)
{
int i,j;
char pass = *p;

for(i=0;i<=15;i++)
{
j=strcmp(p,key[i]);
if(j==0)
{
printf("%s - Keyword \t",key[i]);
str="";
break;
}
}
for(i=0;i<=6;i++)
{
j=strcmp(p,header[i]);
if(j==0)
{
printf("%s - Header File \t",header[i]);
str="";
break;
}
}
/* for(i=1;i<=20;i++)
{
if(pass==symbol[i])
{
printf("%c - Symbol \t",symbol[i]);
str="";
break;
}
} */
for(i=0;i<=14;i++)
{
if(pass==delim[i])
{
printf("%c - Delimator \t",delim[i]);
str="";
break;
}
}
for(i=0;i<=10;i++)
{
if(pass==digit[i])
{
printf("%c - Digit \t",digit[i]);
str="";
break;
}
}
for(i=0;i<=10;i++)

{
if(pass==oper[i])
{
printf("%c - Operator \t",oper[i]);
str="";
break;
}
}
pass = '\0';
getch();
}

Vous aimerez peut-être aussi