You are on page 1of 25

Lexical.

c
#include"stdio.h"
#include"conio.h"
void main()
{
FILE *fi,*fo,*fop,*fk;
int flag=0,i=1;
char c,t,a[15],ch[15],file[20];
clrscr();
printf("Enter the file name : ");
scanf("%s",file);
fi=fopen(file,"r");
fo=fopen("inter.c","w");
fop=fopen("oper.c","r");
fk=fopen("key.c","r");
c=getc(fi);
while(!feof(fi))
{
if(isalpha(c)||isdigit(c)||(c=='['||c==']'||c=='.'==1))
fputc(c,fo);
else
{
if(c=='\n')
fprintf(fo,"\t$\t");
else
fprintf(fo,"\t%c\t",c);
}
c=getc(fi);
}
fclose(fi);
fclose(fo);
fi=fopen("inter.c","r");
printf("\t\tLEXICAL ANALYSIS \n");
fscanf(fi,"%s",a);
printf("\nline : %d\n",i++);
while(!feof(fi))
{
if((strcmp(a,"$")==0))
{
printf("\nline : %d\n",i++);
fscanf(fi,"%s",a);
}
fscanf(fop,"%s",ch);
while(!feof(fop))
{
if(strcmp(ch,a)==0)
{
fscanf(fop,"%s",ch);
printf("\t\t%s\t:\t%s\n",a,ch);
flag=1;
}
fscanf(fop,"%s",ch);

}
rewind(fop);
fscanf(fk,"%s",ch);
while(!feof(fk))
{
if(strcmp(ch,a)==0)
{
fscanf(fk,"%s",ch);
printf("\t\t%s\t:\tkeyword\n",a);
flag=1;
}
fscanf(fk,"%s",ch);
}
rewind(fk);
if(flag==0)
{
if(isdigit(a[0]))
printf("\t\t%s\t:\tconstant\n",a);
else
printf("\t\t%s\t:\tidentifier\n",a);
}
flag=0;
fscanf(fi,"%s",a);
}
getch();
}
key.c
int
void
main
char
if
for
while
else
printf
scanf
FILE
include
stdio.h
conio.h
iostream.h
oper.c
( openpara
) closepara
{ openbrace
} closebrace
< lesser
> greater
" doublequote

' singlequote
: colon
; semicolon
# preprocessor
= equal
== assign
% percentage
^ bitwise
& reference
* star
+ add
- sub
\ backslash
/ slash
input.c
#include"stdio.h"
#include"conio.h"
void main()
{
int a=10,b,c;
a=b*c;
getch();
}

C program to find First and Follow in a given Grammar.

#include<stdio.h>
#include<string.h>
int i,j,l,m,n=0,o,p,nv,z=0,x=0;
char str[10],temp,temp2[10],temp3[20],*ptr;
struct prod
{
char lhs[10],rhs[10][10],ft[10],fol[10];
int n;
}pro[10];
void findter()
{
int k,t;
for(k=0;k<n;k++)
{
if(temp==pro[k].lhs[0])
{
for(t=0;t<pro[k].n;t++)
{
if( pro[k].rhs[t][0]<65 || pro[k].rhs[t][0]>90 )
pro[i].ft[strlen(pro[i].ft)]=pro[k].rhs[t][0];
else if( pro[k].rhs[t][0]>=65 && pro[k].rhs[t][0]<=90 )
{
temp=pro[k].rhs[t][0];
if(temp=='S')
pro[i].ft[strlen(pro[i].ft)]='#';
findter();
}
}
break;
}
}
}
void findfol()
{
int k,t,p1,o1,chk;
char *ptr1;
for(k=0;k<n;k++)
{
chk=0;
for(t=0;t<pro[k].n;t++)
{
ptr1=strchr(pro[k].rhs[t],temp);
if( ptr1 )
{
p1=ptr1-pro[k].rhs[t];
if(pro[k].rhs[t][p1+1]>=65 && pro[k].rhs[t][p1+1]<=90)
{
for(o1=0;o1<n;o1++)
if(pro[o1].lhs[0]==pro[k].rhs[t][p1+1])
{
strcat(pro[i].fol,pro[o1].ft);

chk++;
}
}
else if(pro[k].rhs[t][p1+1]=='\0')
{
temp=pro[k].lhs[0];
if(pro[l].rhs[j][p]==temp)
continue;
if(temp=='S')
strcat(pro[i].fol,"$");
findfol();
chk++;
}
else
{
pro[i].fol[strlen(pro[i].fol)]=pro[k].rhs[t][p1+1];
chk++;
}
}
}
if(chk>0)
break;
}
}
int main()
{
FILE *f;
//clrscr();
for(i=0;i<10;i++)
pro[i].n=0;
f=fopen("tab5.txt","r");
while(!feof(f))
{
fscanf(f,"%s",pro[n].lhs);
if(n>0)
{
if( strcmp(pro[n].lhs,pro[n-1].lhs) == 0 )
{
pro[n].lhs[0]='\0';
fscanf(f,"%s",pro[n-1].rhs[pro[n-1].n]);
pro[n-1].n++;
continue;
}
}
fscanf(f,"%s",pro[n].rhs[pro[n].n]);
pro[n].n++;
n++;
}
printf("\n\nTHE GRAMMAR IS AS FOLLOWS\n\n");
for(i=0;i<n;i++)
for(j=0;j<pro[i].n;j++)
printf("%s -> %s\n",pro[i].lhs,pro[i].rhs[j]);

pro[0].ft[0]='#';
for(i=0;i<n;i++)
{
for(j=0;j<pro[i].n;j++)
{
if( pro[i].rhs[j][0]<65 || pro[i].rhs[j][0]>90 )
{
pro[i].ft[strlen(pro[i].ft)]=pro[i].rhs[j][0];
}
else if( pro[i].rhs[j][0]>=65 && pro[i].rhs[j][0]<=90 )
{
temp=pro[i].rhs[j][0];
if(temp=='S')
pro[i].ft[strlen(pro[i].ft)]='#';
findter();
}
}
}
printf("\n\nFIRST\n");
for(i=0;i<n;i++)
{
printf("\n%s -> ",pro[i].lhs);
for(j=0;j<strlen(pro[i].ft);j++)
{
for(l=j-1;l>=0;l--)
if(pro[i].ft[l]==pro[i].ft[j])
break;
if(l==-1)
printf("%c",pro[i].ft[j]);
}
}
for(i=0;i<n;i++)
temp2[i]=pro[i].lhs[0];
pro[0].fol[0]='$';
for(i=0;i<n;i++)
{
for(l=0;l<n;l++)
{
for(j=0;j<pro[i].n;j++)
{
ptr=strchr(pro[l].rhs[j],temp2[i]);
if( ptr )
{
p=ptr-pro[l].rhs[j];
if(pro[l].rhs[j][p+1]>=65 && pro[l].rhs[j][p+1]<=90)
{
for(o=0;o<n;o++)
if(pro[o].lhs[0]==pro[l].rhs[j][p+1])
strcat(pro[i].fol,pro[o].ft);
}
else if(pro[l].rhs[j][p+1]=='\0')
{
temp=pro[l].lhs[0];
if(pro[l].rhs[j][p]==temp)
continue;

if(temp=='S')
strcat(pro[i].fol,"$");
findfol();
}
else
pro[i].fol[strlen(pro[i].fol)]=pro[l].rhs[j][p+1];
}
}
}
}
printf("\n\nFOLLOW\n");
for(i=0;i<n;i++)
{
printf("\n%s -> ",pro[i].lhs);
for(j=0;j<strlen(pro[i].fol);j++)
{
for(l=j-1;l>=0;l--)
if(pro[i].fol[l]==pro[i].fol[j])
break;
if(l==-1)
printf("%c",pro[i].fol[j]);
}
}
printf("\n");
//getch();
}
Input File(tab5.txt) For First and Follow Program:S ABE
Sa
Ap
At
B Aq
Sf
Aw

3.Develop an operator precedence parser for a given Language


#include stdio.h
#include string.h
#include conio.h
char stack[20],stack1[20],next,s[10];
int top=-1;
char prod[9][10]={
">><<<<<>>",
">><<<<<>>",
">>>><<<>>",
">>>><<<>>",
">>>><<<>>",
">>>>>ee>>",
"<<<<<<<=e",

">>>>>ee>>",
"<<<<<<

};
char G[7][6]={
"E->E+E",
" /E-E",
" /E*E",
" /E/E",
" /(E)",
" /i "
};
int main()
{
char symbol;
int i=0,flag=0;
int j,k;
clrscr();
printf("Grammar\n");
for(j=0;j<7;j++)
{
for(k=0;k<6;k++)
printf("%c",G[j][k]);
printf("\n");
}
printf("\n\n OPERATOR PRECEDENCE RELATIONS \n");
printf("\n -------------------------------------------------------- \n");
printf("%c\t%c\t%c\t%c\t%c\t%c\t%c\t%c\t%c\t",'+','-','*','/','^','i','(',')','$');
printf("\n------------------------------------------------------------------\n");
for(j=0;j<9;j++)
{
for(k=0;k<10;k++)
printf("%c\t",prod[j][k]);
printf("\n");
}
printf("Enter the string : ");
gets(s);
++top;
stack[top]='$';
next=s[i];
while(1)
{
if(stack[top]=='$'&& next=='$'||next=='\0')
break;
else
{
symbol=prod[f(stack[top])][f(next)];
if(symbol=='<'||symbol=='=')
{
stack[++top]=symbol;
stack[++top]=next;
}
else if(symbol=='>')
{
do
{

top--;
}while(stack[top]!='<');
stack[++top]=next;
if(next!='$')
{
for(j=0;j<=top;j++)
stack1[j]=stack[j];
stack1[j]=symbol;
}
}
else
flag=1;
next=s[++i];
}
}
printf("\n STACK : ");
for(j=0;j<=top;j++)
printf("%c",stack1[j]);
printf("%c",'$');
if(flag==0)
printf("\n\n Accepted");
else
printf("Rejected");
return 0;
}
int f(char ch)
{
switch(ch)
{
case '+':return 0;
case '-':return 1;
case '*':return 2;
case '/':return 3;
case '^':return 4;
case 'i':return 5;
case '(':return 6;
case ')':return 7;
case '$':return 8;
default :
{
printf("\n ERROR ");
exit(0);
}
}
}

recursive descent parser in c


#include"stdio.h"

#include"conio.h"

#include"string.h"
#include"stdlib.h"
#include"ctype.h"

char ip_sym[15],ip_ptr=0,op[50],tmp[50];
void e_prime();
void e();
void t_prime();
void t();
void f();
void advance();
int n=0;
void e()
{
strcpy(op,"TE'");
printf("E=%-25s",op);
printf("E->TE'\n");
t();
e_prime();
}
void e_prime()
{
int i,n=0,l;
for(i=0;i<=strlen(op);i++)
if(op[i]!='e')
tmp[n++]=op[i];
strcpy(op,tmp);
l=strlen(op);
for(n=0;n < l && op[n]!='E';n++);<l&&op[n]!='e';n++);></l&&op[n]!='e';n++);>
if(ip_sym[ip_ptr]=='+')
{
i=n+2;
do
{
op[i+2]=op[i];
i++;
}while(i<=l);
op[n++]='+';
op[n++]='T';
op[n++]='E';
op[n++]=39;
printf("E=%-25s",op);
printf("E'->+TE'\n");
advance();
t();
e_prime();
}
else

{
op[n]='e';
for(i=n+1;i<=strlen(op);i++)
op[i]=op[i+1];
printf("E=%-25s",op);
printf("E'->e");
}
}
void t()
{
int i,n=0,l;
for(i=0;i<=strlen(op);i++)
if(op[i]!='e')
tmp[n++]=op[i];
strcpy(op,tmp);
l=strlen(op);
for(n=0;n < l && op[n]!='T';n++);
<l&&op[n]!='t';n++);></l&&op[n]!='t';n++);>
i=n+1;
do
{
op[i+2]=op[i];
i++;
}while(i < l);
op[n++]='F';
op[n++]='T';
op[n++]=39;
printf("E=%-25s",op);
printf("T->FT'\n");
f();
t_prime();
}
void t_prime()
{
int i,n=0,l;
for(i=0;i<=strlen(op);i++)
if(op[i]!='e')
tmp[n++]=op[i];
strcpy(op,tmp);
l=strlen(op);
for(n=0;n < l && op[n]!='T';n++);
if(ip_sym[ip_ptr]=='*')
{
i=n+2;
do
{
op[i+2]=op[i];
i++;
}while(i < l);

op[n++]='*';
op[n++]='F';
op[n++]='T';
op[n++]=39;
printf("E=%-25s",op);
printf("T'->*FT'\n");
advance();
f();
t_prime();
}
else
{
op[n]='e';
for(i=n+1;i<=strlen(op);i++)
op[i]=op[i+1];
printf("E=%-25s",op);
printf("T'->e\n");
}
}
void f()
{
int i,n=0,l;
for(i=0;i<=strlen(op);i++)
if(op[i]!='e')
tmp[n++]=op[i];
strcpy(op,tmp);
l=strlen(op);
for(n=0;n < l && op[n]!='F';n++);
if((ip_sym[ip_ptr]=='i')||(ip_sym[ip_ptr]=='I'))
{
op[n]='i';
printf("E=%-25s",op);
printf("F->i\n");
advance();
}
else
{
if(ip_sym[ip_ptr]=='(')
{
advance();
e();
if(ip_sym[ip_ptr]==')')
{
advance();
i=n+2;
do
{
op[i+2]=op[i];
i++;

}while(i<=l);
op[n++]='(';
op[n++]='E';
op[n++]=')';
printf("E=%-25s",op);
printf("F->(E)\n");
}
}
else
{
printf("\n\t syntax error");
getch();
exit(1);
}
}
}
void advance()
{
ip_ptr++;
}
void main()
{
int i;
clrscr();
printf("\nGrammar without left recursion");
printf("\n\t\t E->TE' \n\t\t E'->+TE'|e \n\t\t T->FT' ");
printf("\n\t\t T'->*FT'|e \n\t\t F->(E)|i");
printf("\n Enter the input expression:");
gets(ip_sym);
printf("Expressions");
printf("\t Sequence of production rules\n");
e();
for(i=0;i < strlen(ip_sym);i++)
{
if(ip_sym[i]!='+'&&ip_sym[i]!='*'&&ip_sym[i]!='('&&
ip_sym[i]!=')'&&ip_sym[i]!='i'&&ip_sym[i]!='I')
{
printf("\nSyntax error");
break;
}
for(i=0;i<=strlen(op);i++)
if(op[i]!='e')
tmp[n++]=op[i];
strcpy(op,tmp);
printf("\nE=%-25s",op);
}
getch();
}

C program- LL1 grammar


/*LL1 PARSING TABLE*/
/*To check whether the grammar is LL1 or not*/
#include<stdio.h>
#include<conio.h>
void main()
{
char pro[10][10],first[10][10],follow[10][10],nt[10],ter[10],res[10][10][10],temp[10];
int npro,noter=0,nont=0,i,j,k,flag=0,count[10][10],row,col,l,m,n,index;
clrscr();
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
count[i][j]=NULL;
for(k=0;k<10;k++){
res[i][j][k]=NULL; }
}
}
printf("Enter the no of productions:");
scanf("%d",&npro);
printf("Enter the productions:");
for(i=0;i<npro;i++)
{
scanf("%s",pro[i]);

}
for(i=0;i<npro;i++)
{
flag=0;
for(j=0;j<nont;j++)
{
if(nt[j]==pro[i][0])
{
flag=1;
}
}
if(flag==0)
{
nt[nont]=pro[i][0];
nont++;
}
}
printf("\nEnter the first values:\n");
for(i=0;i<nont;i++)
{
printf("First value(%c):",nt[i]);
scanf("%s",first[i]);
}
printf("\nEnter the follow values:\n");
for(i=0;i<nont;i++)
{
printf("Follow value(%c):",nt[i]);
scanf("%s",follow[i]);
}
for(i=0;i<nont;i++)
{
flag=0;
for(j=0;j<strlen(first[i]);j++)
{
for(k=0;k<noter;k++)
{
if(ter[k]==first[i][j])
{
flag=1;
}
}
if(flag==0)
{
if(first[i][j]!='#')
{
ter[noter]=first[i][j];
noter++;
}
}
}
}
for(i=0;i<nont;i++)
{
flag=0;
for(j=0;j<strlen(follow[i]);j++)
{
for(k=0;k<noter;k++)

{
if(ter[k]==follow[i][j])
{
flag=1;
}
}
if(flag==0)
{
ter[noter]=follow[i][j];
noter++;
}
}
}
for(i=0;i<nont;i++)
{
for(j=0;j<strlen(first[i]);j++)
{
flag=0;
if(first[i][j]=='#')
{
col=i;
for(m=0;m<strlen(follow[col]);m++)
{
for(l=0;l<noter;l++)
{
if(ter[l]==follow[col][m])
{
row=l;
}
}
temp[0]=nt[col];
temp[1]='-' ;
temp[2]='>';
temp[3]='#';
temp[4]='\0';
printf("temp %s",temp);
strcpy(res[col][row],temp);
count[col][row]+=1;
for(k=0;k<10;k++){
temp[k]=NULL;
}
}
}
else{
for(l=0;l<noter;l++)
{
if(ter[l]==first[i][j])
{
row=l;
}
}
for(k=0;k<npro;k++){
if(nt[i]==pro[k][0])
{
col=i;
if((pro[k][3]==first[i][j])&&(pro[k][0]==nt[col]))
{

strcpy(res[col][row],pro[k]);
count[col][row]+=1;
}
else
{
if((isupper(pro[k][3]))&&(pro[k][0]==nt[col]))
{
flag=0;
for(m=0;m<nont;m++)
{
if(nt[m]==pro[k][3]){index=m;flag=1;}
}
if(flag==1){
for(m=0;m<strlen(first[index]);m++)
{if(first[i][j]==first[index][m])
{strcpy(res[col][row],pro[k]);
count[col][row]+=1;}
}
}
}}}}}
}}
printf("LL1 Table\n\n");
flag=0;
for(i=0;i<noter;i++)
{
printf("\t%c",ter[i]);
}
for(j=0;j<nont;j++)
{
printf("\n\n%c",nt[j]);
for(k=0;k<noter;k++)
{
printf("\t%s",res[j][k]);
if(count[j][k]>1){flag=1;}
}
}
if(flag==1){printf("\nThe given grammar is not LL1");}
else{printf("\nThe given grammar is LL1");}
getch();
}

6. To Implement Predictive Parsing


#include<string.h>
#include<conio.h>
char a[10];
int top=-1,i;
void error(){
printf("Syntax Error");
}
void push(char k[]) //Pushes The Set Of Characters on to the Stack
{
for(i=0;k[i]!='\0';i++)
{
if(top<9)
a[++top]=k[i];
}
}
char TOS()
//Returns TOP of the Stack
{
return a[top];
}
void pop()
//Pops 1 element from the Stack
{
if(top>=0)
a[top--]='\0';
}
void display() //Displays Elements Of Stack
{
for(i=0;i<=top;i++)
printf("%c",a[i]);
}
void display1(char p[],int m) //Displays The Present Input String
{
int l;
printf("\t");
for(l=m;p[l]!='\0';l++)
printf("%c",p[l]);
}
char* stack(){
return a;
}
int main()
{
char ip[20],r[20],st,an;
int ir,ic,j=0,k;
char t[5][6][10]={"$","$","TH","$","TH","$",
"+TH","$","e","e","$","e",
"$","$","FU","$","FU","$",
"e","*FU","e","e","$","e",
"$","$","(E)","$","i","$"};
clrscr();
printf("\nEnter any String(Append with $)");

gets(ip);
printf("Stack\tInput\tOutput\n\n");
push("$E");
display();
printf("\t%s\n",ip);
for(j=0;ip[j]!='\0';)
{
if(TOS()==an)
{
pop();
display();
display1(ip,j+1);
printf("\tPOP\n");
j++;
}
an=ip[j];
st=TOS();
if(st=='E')ir=0;
else if(st=='H')ir=1;
else if(st=='T')ir=2;
else if(st=='U')ir=3;
else if(st=='F')ir=4;
else {
error();
break;
}
if(an=='+')ic=0;
else if(an=='*')ic=1;
else if(an=='(')ic=2;
else if(an==')')ic=3;
else if((an>='a'&&an<='z')||(an>='A'&&an<='Z')){ic=4;an='i';}
else if(an=='$')ic=5;
strcpy(r,strrev(t[ir][ic]));
strrev(t[ir][ic]);
pop();
push(r);
if(TOS()=='e')
{
pop();
display();
display1(ip,j);
printf("\t%c->%c\n",st,238);
}
else{
display();
display1(ip,j);
printf("\t%c->%s\n",st,t[ir][ic]);
}
if(TOS()=='$'&&an=='$')
break;
if(TOS()=='$'){
error();
break;
}
}
k=strcmp(stack(),"$");
if(k==0 && i==strlen(ip))
printf("\n Given String is accepted");
else
printf("\n Given String is not accepted");
return 0;
}

7. program: shift reduce parser


#include"stdio.h"
#include"stdlib.h"
#include"conio.h"
#include"string.h"
char ip_sym[15],stack[15];
int ip_ptr=0,st_ptr=0,len,i;
char temp[2],temp2[2];
char act[15];
void check();
void main()
{
clrscr();
printf("\n\t\t SHIFT REDUCE PARSER\n");
printf("\n GRAMMER\n");
printf("\n E->E+E\n E->E/E");
printf("\n E->E*E\n E->a/b");
printf("\n enter the input symbol:\t");
gets(ip_sym);
printf("\n\t stack implementation table");
printf("\n stack\t\t input symbol\t\t action");
printf("\n______\t\t ____________\t\t ______\n");
printf("\n $\t\t%s$\t\t\t--",ip_sym);
strcpy(act,"shift ");
temp[0]=ip_sym[ip_ptr];
temp[1]='\0';
strcat(act,temp);
len=strlen(ip_sym);
for(i=0;i<=len-1;i++)
{
stack[st_ptr]=ip_sym[ip_ptr];
stack[st_ptr+1]='\0';
ip_sym[ip_ptr]=' ';
ip_ptr++;
printf("\n $%s\t\t%s$\t\t\t%s",stack,ip_sym,act);
strcpy(act,"shift ");
temp[0]=ip_sym[ip_ptr];
temp[1]='\0';
strcat(act,temp);

check();
st_ptr++;
}
st_ptr++;
check();
}
void check()
{
int flag=0;
temp2[0]=stack[st_ptr];
temp2[1]='\0';
if((!strcmpi(temp2,"a"))||(!strcmpi(temp2,"b")))
{
stack[st_ptr]='E';
if(!strcmpi(temp2,"a"))
printf("\n $%s\t\t%s$\t\t\tE->a",stack, ip_sym);
else
printf("\n $%s\t\t%s$\t\t\tE->b",stack,ip_sym);
flag=1;
}
if((!strcmpi(temp2,"+"))||(strcmpi(temp2,"*"))||(!strcmpi(temp2,"/")))
{
flag=1;
}
if((!strcmpi(stack,"E+E"))||(!strcmpi(stack,"E\E"))||(!strcmpi(stack,"E*E")))
{
strcpy(stack,"E");
st_ptr=0;
if(!strcmpi(stack,"E+E"))
printf("\n $%s\t\t%s$\t\t\tE->E+E",stack,ip_sym);
else
if(!strcmpi(stack,"E\E"))
printf("\n $%s\t\t %s$\t\t\tE->E\E",stack,ip_sym);
else
printf("\n $%s\t\t%s$\t\t\tE->E*E",stack,ip_sym);
flag=1;
}
if(!strcmpi(stack,"E")&&ip_ptr==len)
{
printf("\n $%s\t\t%s$\t\t\tACCEPT",stack,ip_sym);
getch();
exit(0);
}
if(flag==0)
{
printf("\n%s\t\t\t%s\t\t reject",stack,ip_sym);
exit(0);
}
return;
}

8. Program to Design LALR Parser


#include
#include
#include
#include
#include
#include

<iostream>
<sstream>
<cctype>
<deque>
<stack>
<vector>

struct Token{
enum TokenType{
END,
INTEGER,
PLUS,
MINUS,
} type;
long intValue;
Token(TokenType type=END):type(type),intValue(0){}
Token(long val):type(INTEGER),intValue(val){}
Token(char character){
//...
}
};
class NonTerminal{
enum NonTerminalType{
terminal,
expr,
} type;
NonTerminal *trunk;
std::vector<NonTerminal> branches;
bool reduced;
public:
Token leaf;
private:
void reduce_terminal(){
this->reduced=1;
switch (this->leaf.type){
case Token::INTEGER:
this->type=expr;
}
}
void reduce_expr(){
if (!this->branches.size())
return;
if (this->branches.size()<3)
this->leaf=this->branches[0].leaf;
else{
this->leaf.type=Token::INTEGER;
switch (this->branches[1].leaf.type){

case Token::PLUS:
this->leaf.intValue=this->branches[0].leaf.intValue+this>branches[2].leaf.intValue;
break;
case Token::MINUS:
this->leaf.intValue=this->branches[0].leaf.intValue-this>branches[2].leaf.intValue;
break;
default:;
}
}
this->reduced=1;
this->branches.clear();
}
public:
NonTerminal(NonTerminal *trunk=0){
this->type=expr;
this->trunk=trunk;
this->reduced=0;
}
NonTerminal(const Token &token,NonTerminal *trunk=0){
this->leaf=token;
this->type=terminal;
this->trunk=trunk;
this->reduced=0;
}
void set(const Token &token){
this->leaf=token;
this->type=terminal;
this->trunk=trunk;
this->reduced=0;
}
void push(const Token &token){
if (this->type==terminal)
return;
this->branches.push_back(NonTerminal(token));
}
NonTerminal *newBranch(const Token &token){
this->branches.push_back(NonTerminal(this));
return &this->branches.back();
}
bool isComplete(){
if (this->type==terminal)
return 1;
if (!this->branches.size())
return 0;
for (unsigned a=0;a<this->branches.size();a++)
if (this->branches[a].type!=terminal && !this->branches[a].reduced)
return 0;
switch (this->branches.size()){
case 1:
return this->branches[0].leaf.type==Token::INTEGER;
case 3:
if (this->branches[0].leaf.type!=Token::INTEGER ||
this->branches[1].leaf.type!=Token::PLUS &&
this->branches[1].leaf.type!=Token::MINUS ||
this->branches[2].leaf.type!=Token::INTEGER)
return 0;
return 1;
default:
return 0;
}
}
NonTerminal *reduce(){

if (!this->isComplete())
return 0;
switch (this->type){
case terminal:
this->reduce_terminal();
break;
case expr:
this->reduce_expr();
break;
}
return this->trunk?this->trunk:this;

};
class Parser{
std::deque<Token> expression;
std::stack<Token> stack;
long result;
unsigned state;
NonTerminal tree,
*current_node;
Token read(std::stringstream &stream){
//(lexer)
}
unsigned run_state(){
Token::TokenType front_token_type;
switch (this->state){
case 0:
front_token_type=this->expression.front().type;
if (front_token_type==Token::INTEGER)
return 3;
if (front_token_type==Token::END)
return 1;
return 2;
case 3:
this->current_node->push(this->expression.front());
this->expression.pop_front();
if (this->current_node->isComplete())
this->current_node=this->current_node->reduce();
front_token_type=this->expression.front().type;
if (front_token_type==Token::PLUS ||
front_token_type==Token::MINUS)
return 4;
if (front_token_type==Token::END)
return 1;
return 2;
case 4:
this->current_node->push(this->expression.front());
this->expression.pop_front();
front_token_type=this->expression.front().type;
if (front_token_type==Token::INTEGER)
return 3;
return 2;
default:
return this->state;
}
}
//1: continue, 0: accept, -1: abort
int to_state(unsigned n){
this->state=n;
switch (n){
case 1:
return 0;
case 2:

return -1;
default:
return 1;

}
}
public:
Parser(const std::string &str){
std::stringstream stream(str);
do
this->expression.push_back(this->read(stream));
while (this->expression.back().type!=Token::END);
this->result=0;
this->state=0;
this->current_node=&this->tree;
}
bool eval(long &res){
int ret;
while ((ret=this->to_state(this->run_state()))==1);
if (!ret){
this->tree.reduce();
res=this->tree.leaf.intValue;
}
return !ret;
}
};
int main(){
Parser evaluator("12+3-2");
long res;
std::cout <<evaluator.eval(res)<<std::endl;
std::cout <<res<<std::endl;
return 0;
}

You might also like