Você está na página 1de 23

//Token Separation

#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;
}

//Implementation of lexical analyzer using c


#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,lastentry=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=lastentry;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=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++)
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 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();
}
}

//Implementation of lexical analyzer using LEX


%{
#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 symbol\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;
}

//Conversion of a Regular Expression to NFA


#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'&&reg[i+1]!='/'&&reg[i+1]!='*')
{
q[j][0]=j+1;
j++;
}
if(reg[i]=='b'&&reg[i+1]!='/'&&reg[i+1]!='*')
{
q[j][1]=j+1;
j++;
}
if(reg[i]=='e'&&reg[i+1]!='/'&&reg[i+1]!='*')
{
q[j][2]=j+1;
j++;
}
if(reg[i]=='a'&&reg[i+1]=='/'&&reg[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'&&reg[i+1]=='/'&&reg[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'&&reg[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'&&reg[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]==')'&&reg[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 Recursive Descent Parser


#include<stdio.h>
#include<conio.h>
char ip_sym[15],ip_ptr=0;
void e_prime();
void e();
void t();
void t_prime();
void f();
void advance();
void e()
{
printf("\n\t\tE->TE'");
t();
e_prime();
}
void e_prime()
{
if(ip_sym[ip_ptr]=='+')
{
printf("\n\t\tE'->+TE'");
advance();
t();
e_prime();
}
else
printf("\n\t\tE'->e");
}
void t()
{
printf("\n\t\tT->FT'");
f();
t_prime();
}
void t_prime()
{
if(ip_sym[ip_ptr]=='*')
{
printf("\n\t\tT'->*FT'");
advance();
f();
t_prime();
}
else
printf("\n\t\tT'->e");
}
void f()
{
if((ip_sym[ip_ptr]=='i')||(ip_sym[ip_ptr]=='I'))

{
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();
}

//Implementation of Shift Reduce Parser


#include<stdio.h>
#include<conio.h>
#include<string.h>
struct prodn
{
char p1[10];
char p2[10];
};
void main()
{
char input[20],stack[50],temp[50],ch[2],*t1,*t2,*t;
int i,j,s1,s2,s,count=0;
struct prodn p[10];
FILE *fp=fopen("sr_input.c","r");
stack[0]='\0';
clrscr();
printf("\n Enter the input string\n");
scanf("%s",&input);
while(!feof(fp))
{
fscanf(fp,"%s\n",temp);
t1=strtok(temp,"->");
t2=strtok(NULL,"->");
strcpy(p[count].p1,t1);
strcpy(p[count].p2,t2);
count++;
}
i=0;
while(1)
{
if(i<strlen(input))
{
ch[0]=input[i];
ch[1]='\0';
i++;
strcat(stack,ch);
printf("%s\n",stack);
}
for(j=0;j<count;j++)
{
t=strstr(stack,p[j].p2);
if(t!=NULL)
{
s1=strlen(stack);
s2=strlen(t);
s=s1-s2;
stack[s]='\0';
strcat(stack,p[j].p1);

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

//Implementation of the front end of the compiler using c


#include<stdio.h>
#include<conio.h>
char ch,stack[20],infix[20]="\0",postfix[20],tmp[20][3],a[3]="\0",b[3]="\0",mn='\0';
int i=0,j,ip=0,st=0,pt=0,sp=0;
int infixpri(char a);
int stackpri();
void post();
void main()
{
char code[10][3]={"T0","T1","T2","T3","T4","T5","T6","T7","T8","T9"};
stack[0]='#';
clrscr();
printf("Enter the expression with # at the end \n");
gets(infix);
post();
j=0;i=0;
st=-1;
do
{
mn=postfix[i];
if(isalpha(mn))
{
tmp[++st][0]=mn;
}
else
{
strcpy(b,tmp[st]);
st--;
strcpy(a,tmp[st]);
switch(mn)
{
case '+':
printf("%s=%s+%s\n",code[j],a,b);
break;
case '-':
printf("%s=%s-%s\n",code[j],a,b);
break;
case '*':
printf("%s=%s*%s\n",code[j],a,b);
break;
case '/':
printf("%s=%s/%s\n",code[j],a,b);
break;
}
j++;
strcpy(tmp[st],code[j-1]);
}
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);
}

//Implementation of back end of the compiler using c


#include<stdio.h>
#include<string.h>
void printop(char *op)
{
if(strcmp(op,"+")==0)
printf("\t ADD ");
else if(strcmp(op,"-")==0)
printf("\t SUB ");
else if(strcmp(op,"*")==0)
printf("\t MUL ");
else if(strcmp(op,"/")==0)
printf("\t DIV ");
}
void main()
{
char line[20];
char *exp,*res,*op1,*op2,*op;
char RO[10];
int ch;
FILE *fp=fopen("input.c","r");
clrscr();
RO[0]='\0';
printf("Code is generated\n\n");
while(!feof(fp))
{
fscanf(fp,"%s\n",line);
res=strtok(line,"=");
exp=strtok(NULL,"=");
if(strstr(exp,"+")!=0)
{
op1=strtok(exp,"+");
op2=strtok(NULL,"+");
strcpy(op,"+");
}
else if(strstr(exp,"-")!=0)
{
op1=strtok(exp,"-");
op2=strtok(NULL,"-");
strcpy(op,"-");
}
else if(strstr(exp,"*")!=0)
{
op1=strtok(exp,"*");
op2=strtok(NULL,"*");
strcpy(op,"*");
}
else if(strstr(exp,"/")!=0)
{

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

//Implementation of parser using YACC and LEX


Yacc
%{
#include<stdio.h>
%}
%token ID
%left '+''-'
%left '*''/'
%%
stmt:statement ';'
|stmt statement ';'
;
statement:expn
;
expn:expn '+' expn {printf("The given string is accepted");}
|expn '*' expn {printf("The given string is accepted");}
|expn '-' expn {printf("The given string is accepted");}
|expn '/' expn {printf("The given string is accepted");}
|ID
;
%%
Lex
%{
#include<stdio.h>
#include "y.tab.h"
%}
%%
[a-z]+ return ID;
"+" return '+';
"-" return '-';
"*" return '*';
"/" return '/';
[ \t\n] ;
";" return yytext[0];
%%
extern FILE *yyin;
void yyerror(const char *str)
{
fprintf(stderr,"error: %s\n",str);
}
int yywrap()
{
return 1;
}

int main()
{
yyparse();
return(0);
}

//Implementation of Desktop Calculator using YACC and LEX


YACC
%{
#include <stdio.h>
int regs[26];
int base;
%}
%token DIGIT LETTER
%left '+' '-'
%left '*' '/'
%left UMINUS
%%
list:
|list stat '\n'
|list error '\n' {yyerrok;}
;
stat:expr {printf("%d\n",$1);}
|LETTER '=' expr {regs[$1] = $3;}
;
expr:'(' expr ')' {$$ = $2;}
|expr '*' expr {$$ = $1 * $3;}
|expr '/' expr {$$ = $1 / $3;}
|expr '+' expr {$$ = $1 + $3;}
|expr '-' expr {$$ = $1 - $3;}
|'-' expr %prec UMINUS {$$ = -$2;}
|LETTER {$$ = regs[$1];}
|number
;
number:DIGIT {$$ = $1;base = ($1==0) ? 8 : 10;}
|number DIGIT {$$ = base * $1 + $2;}
;
%%
int main()
{
yyparse();
return(0);
}
yyerror(const char *s)
{
fprintf(stderr, "%s\n",s);
}
int yywrap()
{
return(1);
}

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);
}
%%

Você também pode gostar