You are on page 1of 33

EX.

NO:1


STUDY OF LEX AND YACC

DATE:

EX.NO:2

IMPLEMENTATION OF LEXICAL ANALYSIS USING LEX
TOOL
DATE:

AIM:
To write a C program to implement lexical analysis using LEX tool.

ALGORITHM:
STEP 1: Start the program.
STEP 2: LEX program consist of three parts,
a) Declaration%%
b) Translation rules%%
c) Auxiliary procedure
STEP 3: The declaration section include declaration of variables, main test, constant and
regular definitions.
STEP 4: Translation rule of LEX program are statement of the form
a) P1{action}
b) P2{action}
c) .
d) .
e) Pn{action}
STEP 5: Write a program in the Vi editor and save it with I extensions.
STEP 6: Compile the LEX program with LEX compiler to produce output file as lex.yy.c
Ex: $lex
Filename.I.
$cc lex.yy.c
STEP 7: Compile that file with C compile and verify the output.

PROGRAM:

OUTPUT:

RESULT:
Thus the implementation of lexical analysis using LEX tool was executed successfully
and the output is verified.

EX.NO:3


IMPLEMENTATION OF SYNTAX ANALYSIS USING YACC

DATE:

AIM:
To implement a calculator that takes an expression with digits, + and * and computer and
print the values.


ALGORITHM:
STEP 1: Start the program.
STEP 2: Define the type as double.
STEP 3: Declare the token name as number and operator such as +,-,* and /.
STEP 4: Declare the list option which consist of expression.
STEP 5: Declare the operation done using operator.
STEP 6: Compile the program using YACC tool.
STEP 7: Run the program in a C compile using the extension cc.y.tab.c
STEP 8: Output is displayed using ./a.out
STEP 9: Stop the program.

PROGRAM:

OUTPUT:

RESULT:
Thus the implementation of syntax analysis using YACC was executed successfully
and the output is verified.

EX.NO:4
CONSTRUCTION OF NFA FRON GIVEN REGULAR
EXPRESSION
DATE:


AIM:
To write a C program to convert a given regular expression to NFA.

ALGORITHM:
STEP 1: Get the input expression.
STEP 2: Check the expression for the terminal and operator open an close parenthesis.
STEP 3: If there is a terminal replace it into a state.
STEP 4: Find the transition at each state.
STEP 5: Include the starting and ending state.
STEP 6: Display all the states along with the transition.
STEP 7: Stop the program.

PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<process.h>
void main()
{
char r[10],m[10],b[10];
int i=0,j=0,p;
clrscr();
printf("\n regular expression to NFA \n");
printf("\n enter the regular expression:");
scanf("%s",r);
p=strlen(r);
while(i<p)
{
if(r[i]=='a'||r[i]=='b')
{
m[j]=r[i];
j++;
}
else if(r[i]=='*')
{
m[j-1]='E';
m[j]=r[i-1];
b[j+1]='E';
j+=2;
}
else if(r[i]=='+')
{
m[j]='E';
m[j+1]=r[i-1];
b[j+2]='E';
j+=3;
}
else
{
printf("\n invalid character in the input \n");
getch();
exit(0);
}
i+=1;
}
printf("\n TRANSITION TABLE FOR NFA\n");
printf("\n STATES \t a\t b\t E\n");
for(i=0;i<j;i++)
{
if(m[i]=='a')
printf("\n%d\t\t%d",i,i+1);
else if(m[i]=='b')
printf("\n%d\t\t\t%d",i,i+1);
else if(m[i]=='E')
printf("\n%d\t\t\t\t%d"",""%d",i,i+1,i+3);
else if(b[i]=='E')
printf("\n%d\t\t\t\t%d"",""%d",i,i-1,i+1);
else
printf("\n%d\t\t-\t-\t-",i);
}
getch();
}

OUTPUT:

regular expression to NFA
enter the regular expression:ab+a*

TRANSITION TABLE FOR NFA

STATES a b E
0 1
1 2
2 3,5
3 4
4 3,5
5 6,8
6 7
7 6,8


RESULT:
Thus the program for construction of NFA from regular expression is executed
successfully and the output was verified.


EX.NO:5
CONSTRUCTION OF MINIMIZED DFA FROM A GIVEN
REGULAR EXPRESSION
DATE:

AIM:

ALGORITHM:

PROGRAM:

OUTPUT:

RESULT:


EX.NO:6
IMPLEMENTATION OF SYMBOL TABLE
DATE:


AIM:
To write a C program to implement a symbol table.

ALGORITHM:
STEP 1: Start a program.
STEP 2: Get the input from the user with the terminating symbol $.
STEP 3: Allocate memory for the variables by dynamic memory allocation function.
STEP 4: If the next character of the symbol is an operator then only the memory is allocated.
STEP 5: While reading the input symbol are inserted into symbol table along with its memory
Address.
STEP 6: The steps are repeated till & is reached.
STEP 7: To search a variable, enter the variable to be searched and symbol table has been
Checked.
STEP 8: Stop the program.

PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<string.h>
#include<math.h>
#include<ctype.h>
void main()
{
int i=0,j=0,x=0,n,flag=0;
void *p,*add[15];
char ch,srch,b[15],d[15],c;
clrscr();
printf("expression terminated by $:");
while((c=getchar())!='$')
{
b[i]=c;
i++;
}
n=i-1;
printf("given expression:");
i=0;
while(i<=n)
{
printf("%c",b[i]);
i++;
}
printf("\nsymbol table\n");
printf("symbol\taddr\ttype\n");
while(j<=n)
{
c=b[j];
if(isalpha(toascii(c)))
{
if(j==n)
{
p=malloc(c);
add[x]=p;
d[x]=c;
printf("%c\t%d\tidentifier\n",c,p);
}
else
{
ch=b[j+1];
if(ch=='+'||ch=='-'||ch=='*'||ch=='=')
{
p=malloc(c);
add[x]=p;
d[x]=c;
printf("%c\t%d\tidentifier\n",c,p);
x++;
}
}
}
j++;
}
printf("the symbol is to be searched\n");
srch=getch();
for(i=0;i<=x;i++)
{
if(srch==d[i])
{
printf("symbol found\n");
printf("%c%s%d\n",srch,"@address",add[i]);
flag=1;
}
}
if(flag==0)
printf("symbol not found\n");
getch();
}

OUTPUT:

expression terminated by $:a+b*c$
given expression: a+b*c
symbol table
symbol addr type
a 1894 identifier
b 1996 identifier
c 2098 identifier
the symbol is to be searched

RESULT:
Thus the above program is compiled and executed successfully and output is
verified.


EX.NO:7
IMPLEMENTATION OF SHIFT REDUCE PARSING
ALGORITHM
DATE:

AIM:
To write a C program to implement the shift reduce parser using stack.

ALGORITHM:
STEP 1: Start the program.
STEP 2: Read the start symbol to be parsed.
STEP 3: Read input string to be parsed.
STEP 4: Push $ onto stack and append the input string with $.
STEP 5: Push first terminal or input to stack and increment to the pointer.
STEP 6: Check the tree,perform reduce action,else perform shift action.
STEP 7: If 6 is tree,perform reduce action,else perform shift action.
STEP 8: Perform 6 and 7 untillis on top of the stack at input.
STEP 9: Stop the program.

PROGRAM:

#include<stdio.h>
#include<iostream.h>
#include<ctype.h>
#include<string.h>
#include<conio.h>
struct stru1
{
char non_ter[1],pro[25];
}
cfg[25];
int n,st=-1,j,i,t=-1,m;
int v,c,p=1;
char str[20],stack[20],ch,tmp[10];
void match(int k);
void match1(int k);
void main()
{
clrscr();
cprintf("enter the no.of productions:\n\t");
cscanf("%d",&n);
cprintf("\n\r");
cprintf("enter the production of LEFT & RIGHT sides:\n\r");
for(i=0;i<n;i++)
{
cscanf("%s",cfg[i].non_ter);
cprintf("\n\r");
cprintf("->\n\r");
cscanf("%s",cfg[i].pro);
cprintf("\n\r");
}
cprintf("enter the input string:\n\r");
cscanf("%s",str);
cprintf("\n\r");
i=0;
do
{
ch=str[i];
stack[++st]=ch;
tmp[0]=ch;
match(1);
i++;
}
while(str[i]!='\0');
c=st;
cputs(stack);
cprintf("\n\r");
while(st!=0)
{
v=-st;
t=-1;
p=0;
while(v<=c)
{
tmp[++t]=stack[v++];
p++;
}
match1(p);
}
cfg[0].non_ter[1]='\0';
if(strcmp(stack,cfg[0].non_ter)==0)
cprintf("string is present in grammer G\n\r");
else
cprintf("string is not present in grammer G\n\r");
}
void match(int k)
{
for(j=0;j<n;j++)
{
if(strlen(cfg[j].pro)==k)
{
if(strcmp(tmp,cfg[j].pro)==0)
{
stack[st]=cfg[j].non_ter[0];
break;
}
}
}
}
void match1(int k)
{
int x=1,y;
y=k-1;
for(j=0;j<n;j++)
{
if(strlen(cfg[j].pro)==k)
{
if(strcmp(tmp,cfg[j].pro)==0)
{
k=c-k+1;
stack[k]=cfg[j].non_ter[0];
do
{
stack[k+x]='\0';
tmp[t--]='\0';
c--;
x++;
}
while(x<=y);
tmp[t]='\0';
cputs(stack);
cprintf("\n\r");
break;
}
}
}
}

OUTPUT:

enter the no.of productions:
3
enter the production of LEFT & RIGHT sides:
E
->
a
E
->
E+E
E
->
E*E
enter the input string:
a*a+a
E*E+E

RESULT:
Thus the implementation of shift reduce parsing was executed successfully and the
output is verified.

EX.NO:8
CONSTRUCTION OF LR PARSING TABLE
DATE:

AIM:
To create a C program for implementation of LR parsing table.
ALGORITHM:
STEP 1: Start the program.
STEP 2: Declare the productions.
STEP 3: Enter the production for one by one.
STEP 4: Perform the first and follow position.
STEP 5: Construct the symbol table.
STEP 6: Stop the program.

PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<string.h>
int nt,t,m[20][20],i,s,n,p1,q,k,j;
char p[30][30],n1[20],t1[20],ch,b,c,f[30][30],f1[30][30];
void main()
{
int scant(char);
int scannt(char);
clrscr();
printf("enter the no.of productions:");
scanf("%d",&n);
printf("\n enter the productions one by one:");
for(i=0;i<n;i++)
scanf("%s",&p[i]);
nt=0;
t=0;
for(i=0;i<n;i++)
{
if(scannt(p[i][0])==-1)
n1[nt++]=p[i][0];
}
for(i=0;i<n;i++)
{
for(j=3;j<strlen(p[i]);j++)
{
if(p[i][j]!='e')
{
if(scannt(p[i][j])==-1)
{
if((scant(p[i][j]))==-1)
t1[t++]=p[i][j];
}
}
}
}
t1[t++]='$';
for(i=0;i<nt;i++)
{
for(j=0;j<t;j++)
m[i][j]=-1;
}
for(i=0;i<nt;i++)
{
printf("enter first %c:",n1[i]);
scanf("%s",&f[i]);
}
for(i=0;i<nt;i++)
{
printf("enter follow%c",n1[i]);
scanf("%s",&f1[i]);
}
for(i=0;i<n;i++)
{
p1=scannt(p[i][0]);
if((q=scant(p[i][3]))!=-1)
m[p1][q]=i;
if((q=scannt(p[i][3]))!=-1)
{
for(j=0;j<strlen(f[q]);j++)
m[p1][scant(f[q][j])]=1;
}
if(p[i][3]=='e')
{
for(j=0;j<strlen(f1[p1]);j++)
m[p1][scant(f1[p1][j])]=i;
}
}
for(i=0;i<t;i++)
printf("\t%s",t1[i]);
printf("\n\n");
for(j=0;j<nt;j++)
{
printf("%s",n1[j]);
for(i=0;i<t;i++)
{
printf("\t");
if(m[j][i]!=-1)
printf("\n%c",p[m[j][i]]);
}
}
getch();
}
int scannt(char a)
{
int c=-1,i;
for(i=0;i<nt;i++)
{
if(n1[i]==a)
{
return i;
}
}
return c;
}
int scant(char b)
{
int c1=-1,j;
for(j=0;j<t;j++)
{
if(t1[j]==b)
{
return j;
}
}
return c1;
}



OUTPUT:

RESULT:
Thus the implementation of LR parsing was executed successfully and the output is
verified.


EX.NO:9
GENERATION OF CODE FOR A GIVEN INTERMEDIATE
CODE
DATE:


AIM:
To write a C program to implement the implementation of intermediate code.

ALGORITHM:
STEP 1: Enter the expression for which intermediate code is to be generated.
STEP 2: If the length of the string is greater than 3,then we call the precedence to return the
precedence among the operands.
STEP 3: Assign operands to expression array and operator to the array.
STEP 4: Create the three address code using quadrate structure.
STEP 5: Continue these steps for each statements in the input.
STEP 6: Stop the program.

PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s[20],o[20];
int i=0,j=0,k,f1=1,f=1,k1=0;
clrscr();
printf("\n enter the input string\n");
scanf("%s",o);
strlen(o);
while(o[k1]!='\0')
{
if((o[k1]=='=')==1)
{
break;
}
k1++;
}
for(j=k1+1;j<strlen(o);j++)
{
s[i]=o[j];
i++;
}
s[i]='\0';
i=strlen(s);
j=0;
printf("\n three address code is\n");
if(i>3)
{
while(s[j]!='\0')
{
if((s[j]=='*')==1||(s[j]=='/')==1)
{
k=j;
if(f1!=0)
{
printf("t1=%c%c%c\n",s[k-1],s[k],s[k+1]);
}
else
{
if(k>3)
{
printf("t2=t1%c%c\n",s[k],s[k+1]);
}
else
{
printf("t2=t1%c%c\n",s[k],s[k-1]);
}
}
f=0;
break;
}
j++;
}
j=0;
while(s[j]!='\0')
{
if((s[j]=='+')==1||(s[j]=='-')==1)
{
k=j;
if(f==0)
{
if(k<3)
{
printf("t2=t1%c%c\n",s[k],s[k-1]);
}
else
{
printf("t2=t1%c%c\n",s[k],s[k+1]);
}
}
else
{
printf("t1=%c%c%c\n",s[k-1],s[k],s[k+1]);
}
f1=0;
}
j++;
}
printf("%c=t2",o[0]);
}
else
{
printf("t1=%s\n",s);
printf("%c=t1",o[0]);
}
getch();
}

OUTPUT:

enter the input string
x=a+b*c

three address code is
t1=b*c
t2=t1+a
x=t2

RESULT:
Thus the generation of a code for a given intermediate code was executed
successfully and the output is verified.

EX.NO:10
IMPLEMENTATION OF CODE OPTIMIZATION TECHNIQUES
DATE:

AIM:
To execute a C program to implement the code optimization technique.

ALGORITHM:
STEP 1: Start the program.
STEP 2: Open the input file in.txt and read the data from it.
STEP 3: Identify the variable and operator separately and assign them to specific variable.
STEP 4: Convert the variable to specific identifier and their respective operators.
STEP 5: Using the required loop, print the assembly instruction into the output file out.txt.
STEP 6: Stop the program.
PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char *a,*b,*c,*temp;
int i=0,flag=0,ll=0;
char *f1,*f2;
clrscr();
printf("\n enter the expression\n");
scanf("%s%s",a,b);
flag=0;
for(i=0;i<=strlen(a);i++)
{
if(a[i]=='=')
{
flag=1;
i++;
}
if(flag)
{
*(temp+ll)=*(a+i);
ll++;
}
}
*(temp+ll)='\0';
f1=strstr(b,temp);
f2=strstr(c,temp);
if(f1==NULL&f2==NULL)
{
printf("\n expression could not be optimized");
exit(0);
}
printf("\n optimized code for the expression");
printf("\n\n");
printf("t1=%s\n",a);
if(strlen(f1)>0)
{
for(i=0;i<2;i++)
printf("\nt1");
for(i=0;i<2;i++)
printf("%c",*(b+strlen(b)+i-2));
}
else{
printf("%s",b);
}
printf("\n");
if(strlen(f2)>0)
{
for(i=0;i<2;i++)
printf("%c",*(c+strlen(c)+i+2));
}
else
{
printf("%s",c);
}
getch();
}

OUTPUT:
enter the expression:
a=b+c
d=b+c-f
optimized code for the expression:
t1=b+c
t1
t1-f



RESULT:
Thus the implementation of code optimization technique was executed successfully
and the output is verified.

You might also like