Beruflich Dokumente
Kultur Dokumente
- Compiler Design
YEAR – 02/2019 BRANCH- I.T. , VIITH SEM. (A1) Faculty – Deepika Mam
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
"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;
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;
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int z = 0, i = 0, j = 0, c = 0;
void check()
{
strcpy(ac,"REDUCE TO E -> ");
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);
stk[i] = a[j];
stk[i + 1] = '\0';
a[j]=' ';
#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();
}
#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);
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);
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);
if(ip_sym[ip_ptr]=='*')
i=n+2;
do
op[i+2]=op[i];
i++;
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);
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
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];}
%{
#include<ctype.h>
#include<stdio.h>
#define YYSTYPE double
%}
%token NUM
%%
%%
#include "lex.yy.c"
int main()
{
printf("Enter the expression: ");
yyparse();
}
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);
printf("State Machine::%d\n",i);
printf("Next State if i/p is 0:");
scanf("%d",&posi);
(q+i)->link0=(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.