Sie sind auf Seite 1von 19

NAME- JITENDRA SIYAG ENROLLMENT NO.-15R/0006083 SUB.

- Compiler Design
YEAR – 02/2019 BRANCH- I.T. , VIITH SEM. (A1) Faculty – Deepika Mam

1. Implementation of LEXICAL ANALYZER for IF STATEMENT.


OR
Implementation of LEXICAL ANALYZER.
OR
2. Implementation of LEXICAL ANALYZER for ARITHMETIC EXPRESSION.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>

int isKeyword(char buffer[]){


char keywords[32][10] = {"auto","break","case","char","const","continue","default",

"do","double","else","enum","extern","float","for","goto",

"if","int","long","register","return","short","signed",

"sizeof","static","struct","switch","typedef","union",
"unsigned","void","volatile","while"};
int i, flag = 0;

for(i = 0; i < 32; ++i){


if(strcmp(keywords[i], buffer) == 0){
flag = 1;
break;
}
}

return flag;
}

int main(){
char ch, buffer[15], operators[] = "+-*/%=";
FILE *fp;
int i,j=0;

fp = fopen("program.txt","r");

if(fp == NULL){
printf("error while opening the file\n");
exit(0);
}
while((ch = fgetc(fp)) != EOF){
for(i = 0; i < 6; ++i){
if(ch == operators[i])
printf("%c is operator\n", ch);
}

if(isalnum(ch)){
buffer[j++] = ch;
}
else if((ch == ' ' || ch == '\n') && (j != 0)){
buffer[j] = '\0';
j = 0;

if(isKeyword(buffer) == 1)
printf("%s is keyword\n", buffer);
else
printf("%s is indentifier\n", buffer);
}

fclose(fp);

return 0;
}

3. Write a Lex program to count number of lines, number of characters, spaces, and tabs in
a C-File.

%{
#include<stdio.h>
int cc=0,bc=0,wc=0,lc=0;
%}
%%
[^ \t\n]+ { wc++;
cc=cc+yyleng;
}
\n lc++;
" " bc++;
\t bc=bc+5;
%%
main(int argc,char *argv[])
{
if (argc!=2) {
printf("\nusage:./a.out filename\n");
return(0);
}
yyin=fopen(argv[1],"r");
yylex();
printf("\n no of lines are %d\n",lc);
printf("\n no of words are %d\n",wc);
printf("\n no of blanks are %d\n",bc);
printf("\n no of character are %d\n",cc);
}
int yywrap()
{
return 1;
}

4. Implementation of SHIFT REDUCE PARSING ALGORITHM.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int z = 0, i = 0, j = 0, c = 0;

char a[16], ac[20], stk[15], act[10];

void check()
{
strcpy(ac,"REDUCE TO E -> ");

for(z = 0; z < c; z++)


{
if(stk[z] == '4')
{
printf("%s4", ac);
stk[z] = 'E';
stk[z + 1] = '\0';

printf("\n$%s\t%s$\t", stk, a);


}
}

for(z = 0; z < c - 2; z++)


{
if(stk[z] == '2' && stk[z + 1] == 'E' && stk[z + 2] == '2')
{
printf("%s2E2", ac);
stk[z] = 'E';
stk[z + 1] = '\0';
stk[z + 2] = '\0';
printf("\n$%s\t%s$\t", stk, a);
i = i - 2;
}

for(z=0; z<c-2; z++)


{
if(stk[z] == '3' && stk[z + 1] == 'E' && stk[z + 2] == '3')
{
printf("%s3E3", ac);
stk[z]='E';
stk[z + 1]='\0';
stk[z + 1]='\0';
printf("\n$%s\t%s$\t", stk, a);
i = i - 2;
}
}
return ;
}

int main()
{
printf("GRAMMAR is -\nE->2E2 \nE->3E3 \nE->4\n");

strcpy(a,"32423");
c=strlen(a);
strcpy(act,"SHIFT");
printf("\nstack \t input \t action");
printf("\n$\t%s$\t", a);

for(i = 0; j < c; i++, j++)


{
printf("%s", act);

stk[i] = a[j];
stk[i + 1] = '\0';
a[j]=' ';

printf("\n$%s\t%s$\t", stk, a);


check();
}
check();
if(stk[0] == 'E' && stk[1] == '\0')
printf("Accept\n");
else
printf("Reject\n");
}

5. Implementation of OPERATOR PRECEDENCE PARSER.

#include<stdio.h>
#include<conio.h>

void main(){
char stack[20],ip[20],opt[10][10][1],ter[10];
int i,j,k,n,top=0,col,row;
clrscr();
for(i=0;i<10;i++)
{
stack[i]=NULL;
ip[i]=NULL;
for(j=0;j<10;j++)
{
opt[i][j][1]=NULL;
}
}
printf("Enter the no.of terminals :\n");
scanf("%d",&n);
printf("\nEnter the terminals :\n");
scanf("%s",&ter);
printf("\nEnter the table values :\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("Enter the value for %c %c:",ter[i],ter[j]);
scanf("%s",opt[i][j]);
}
}
printf("\n**** OPERATOR PRECEDENCE TABLE ****\n");
for(i=0;i<n;i++)
{
printf("\t%c",ter[i]);
}
printf("\n");
for(i=0;i<n;i++){printf("\n%c",ter[i]);
for(j=0;j<n;j++){printf("\t%c",opt[i][j][0]);}}
stack[top]='$';
printf("\nEnter the input string:");
scanf("%s",ip);
i=0;
printf("\nSTACK\t\t\tINPUT STRING\t\t\tACTION\n");
printf("\n%s\t\t\t%s\t\t\t",stack,ip);
while(i<=strlen(ip))
{
for(k=0;k<n;k++)
{
if(stack[top]==ter[k])
col=k;
if(ip[i]==ter[k])
row=k;
}
if((stack[top]=='$')&&(ip[i]=='$')){
printf("String is accepted\n");
break;}
else if((opt[col][row][0]=='<') ||(opt[col][row][0]=='='))
{ stack[++top]=opt[col][row][0];
stack[++top]=ip[i]; printf("Shift
%c",ip[i]);
i++;
}
else{
if(opt[col][row][0]=='>')
{
while(stack[top]!='<'){--top;}
top=top-1; printf("Reduce");

}
else
{
printf("\nString is not accepted");
break;
}
}
printf("\n");

for(k=0;k<=top;k++)
{
printf("%c",stack[k]);
}
printf("\t\t\t");
for(k=i;k<strlen(ip);k++){
printf("%c",ip[k]);
}
printf("\t\t\t");
}
getch();
}

6. Implementation of RECURSIVE DESCENT PARSER.

#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++);

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++);

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_sy
m[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();
}
7. Write a YACC program to implement the desk calculator.
/ Lex file: desk.l

DIGIT [0-9]+\.?|[0-9]*\.[0-9]+

%%

[]
{DIGIT} {yylval=atof(yytext);return NUM;}
\n|. {return yytext[0];}

/ Yacc file: desk.y

%{
#include<ctype.h>
#include<stdio.h>
#define YYSTYPE double
%}

%token NUM

%left '+' '-'


%left '*' '/'
%right UMINUS

%%

S : S E '\n' { printf("Answer: %g \nEnter:\n", $2); }


| S '\n'
|
| error '\n' { yyerror("Error: Enter once more...\n" );yyerrok; }
;
E : E '+' E { $$ = $1 + $3; }
| E'-'E { $$=$1-$3; }
| E'*'E { $$=$1*$3; }
| E'/'E { $$=$1/$3; }
| '('E')' { $$=$2; }
| '-'E %prec UMINUS { $$= -$2; }
| NUM
;

%%

#include "lex.yy.c"

int main()
{
printf("Enter the expression: ");
yyparse();
}

8. Write a program to simulate a Deterministic Finite State Automata (DFA).


#include <stdio.h>
#include <stdlib.h>

struct node{
int id_num;
int st_val;
struct node *link0;
struct node *link1;
};
struct node *start, *q, *ptr;
int vst_arr[100], a[10];
int main(){
int count, i, posi, j;
char n[10];

printf("=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-
=\n"); printf("Enter the number of states in the
m/c:"); scanf("%d",&count);

q=(struct node *)malloc(sizeof(struct node)*count);


for(i=0;i<count;i++){
(q+i)->id_num=i;

printf("State Machine::%d\n",i);
printf("Next State if i/p is 0:");
scanf("%d",&posi);
(q+i)->link0=(q+posi);

printf("Next State if i/p is 1:");


scanf("%d",&posi);
(q+i)->link1=(q+posi);

printf("Is the state final state(0/1)?");


scanf("%d",&(q+i)->st_val);
}

printf("Enter the Initial State of the m/c:");


scanf("%d",&posi);
start=q+posi;

printf("=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=\n");

while(1){
printf("=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=\n");
printf("Perform String Check(0/1):");
scanf("%d",&j);
if(j){
ptr=start;
printf("Enter the string of inputs:");
scanf("%s",n);
posi=0;

while(n[posi]!='\0'){
a[posi]=(n[posi]-'0');
//printf("%c\n",n[posi]);
//printf("%d",a[posi]);
posi++;
}

i=0;
printf("The visited States of the m/c are:");
do{
vst_arr[i]=ptr->id_num;
if(a[i]==0){
ptr=ptr->link0;
}
else if(a[i]==1){
ptr=ptr->link1;
}
else{
printf("iNCORRECT iNPUT\n");
return;
}
printf("[%d]",vst_arr[i]);
i++;
}while(i<posi);

printf("\n");
printf("Present State:%d\n",ptr->id_num);
printf("String Status:: ");
if(ptr->st_val==1)
printf("String Accepted\n");
else
printf("String Not Accepted\n");
}
else
return 0;
}
printf("=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=\n");
return 0;
}
9. Implementation of CODE OPTIMIZATION TECHNIQUES.

Das könnte Ihnen auch gefallen