Compiler Lab
Compiler Lab
Compiler Lab
#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,"");
gotoxy(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);
gotoxy(3,4);
printf("KEYWORDS");
gotoxy(16,4);
printf("PUNCT.SYMS");
gotoxy(28,4);
printf("OPERATORS");
gotoxy(40,4);
printf("CONSTANTS");
gotoxy(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:
gotoxy(5,f);
printf("%s",s.a);
break;
case 1:
gotoxy(17,f);
printf("%s",s.a);
break;
case 2:
gotoxy(29,f);
printf("%s",s.a);
break;
case 3:
gotoxy(41,f);
printf("%s",s.a);
break;
default:
gotoxy(53,f);
printf("%s",s.a);
}
f++;
flag=-1;
}while(!feof(fp));
getch();
closegraph();
}
Input File: add.c
void main ( )
{
int x = 6 ;
int y = 4 ;
x=x+y;
}
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 symtable[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();
}
}
if(!file)
{
printf("Could not open %s",argv[1]);
exit(0);
}
yyin=file;
}
yylex();
printf("\n\n");
return 0;
}
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();
}
{
printf("\n\t\tF->i");
advance();
}
else
{
if(ip_sym[ip_ptr]=='(')
{
printf("\n\t\tF->(E)");
advance();
e();
if(ip_sym[ip_ptr]==')')
{
advance();
}
}
else
{
printf("\n\t\tSYNTAX ERROR");
getch();
exit(1);
}
}
}
void advance()
{
ip_ptr++;
}
void main()
{
int i;
clrscr();
printf("\n\t GRAMMAR WITHOUT LEFT RECURSION");
printf("\n\t\tE->TE'\n\t\tE'->+TE'|e\n\t\tT->FT'");
printf("\n\t\tT'->*FT'|e\n\t\tF->(E)|i");
printf("\n Enter the input expression:\n");
gets(ip_sym);
printf("\n Sequence of production rules:");
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("\n SYNTAX ERROR");
break;
}
}
getch();
}
printf("%s\n",stack);
j=-1;
}
}
if(strcmp(stack,"E")==0&&i==strlen(input))
{
printf("\n Accepted");
break;
}
if(i==strlen(input))
{
printf("\n Not Accepted");
break;
}
}
getch();
}
Input File: sr_input.c
E->E+E
E->E*E
E->i
}
while(postfix[i]!='#');
getch();
}
void post()
{
do
{
ch=infix[i];
if((ch>='A'&&ch<='a')||(ch>='Z'&&ch<='z'))
{
postfix[pt++]=ch;
}
if(ch=='('||ch==')'||ch=='+'||ch=='-'||ch=='*'||ch=='/')
{
ip=infixpri(ch);
sp=stackpri();
if(sp>=ip)
{
postfix[pt++]=stack[st];
stack[st]=ch;
}
else
{
stack[++st]=ch;
}
}
i++;
}while(infix[i]!='#');
do
{
if(stack[st]!='('&&stack[st]!=')')
{
postfix[pt++]=stack[st];
}
}while(stack[st--]!='#');
postfix[pt]='#';
}
int infixpri(char ch)
{
int t;
if(ch=='(')
t=3;
else if(ch==')')
t=0;
else if(ch=='*'||ch=='/')
t=2;
else if(ch=='+'||ch=='-')
t=1;
else if(ch=='#')
t=0;
return(t);
}
int stackpri()
{
int t;
char ch;
ch=stack[st];
if(ch=='(')
t=0;
else if(ch==')')
t=0;
else if(ch=='*'||ch=='/')
t=2;
else if(ch=='+'||ch=='-')
t=1;
else if(ch=='#')
t=0;
return(t);
}
op1=strtok(exp,"/");
op2=strtok(NULL,"/");
strcpy(op,"/");
}
if(strcmp(RO,op1)!=0)
{
if(strcmp(RO,op2)!=0)
{
printf("\t MOV ");
printf("%s,RO \n",op1);
printop(op);
printf("%s,RO\n",op2);
}
else
{
printop(op);
printf("%s,RO\n",op1);
}
}
else
{
printop(op);
printf("%s,RO\n",op2);
}
printf("\t MOV RO,%s\n",res);
strcpy(RO,res);
}
getch();
}
Input.c
T0=a+b
T1=T0*c
T2=T1/d
int main()
{
yyparse();
return(0);
}
LEX
%{
#include <stdio.h>
#include "y.tab.h"
int c;
extern int yylval;
%}
%%
""
;
[a-z] {
c = yytext[0];
yylval = c - 'a';
return(LETTER);
}
[0-9] {
c = yytext[0];
yylval = c - '0';
return(DIGIT);
}
[^a-z0-9\b] {
c = yytext[0];
return(c);
}
%%