WEEK-1: LEX ANALYZER
CODE:
#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",
"size of","static"," struct","switch","typedef",
"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("[Link]","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;
}
[Link] file:
int main(){
int a,b;
int c=a+b;
return 0;
}
OUTPUT:
int is keyword
main is indentifier
int is keyword
ab is indentifier
int is keyword
= is operator
+ is operator
cab is indentifier
return is keyword
0 is indentifier
-------------------------------------------------
WEEK-2: FIRST
CODE:
#include<stdio.h>
#include<ctype.h>
void FIRST(char );
int count,n=0;
char prodn[10][10], first[10];
main()
{
int i,choice;
char c,ch;
printf("How many productions ? :");
scanf("%d",&count);
printf("Enter %d productions epsilon= $ :\n\n",count);
for(i=0;i<count;i++)
scanf("%s%c",prodn[i],&ch);
do
{
n=0;
printf("Element :");
scanf("%c",&c);
FIRST(c);
printf("\n FIRST(%c)= { ",c);
for(i=0;i<n;i++)
printf("%c ",first[i]);
printf("}\n");
printf("press 1 to continue : ");
scanf("%d%c",&choice,&ch);
}
while(choice==1);
}
void FIRST(char c)
{
int j;
if(!(isupper(c)))first[n++]=c;
for(j=0;j<count;j++)
{
if(prodn[j][0]==c)
{
if(prodn[j][2]=='$') first[n++]='$';
else if(islower(prodn[j][2]))first[n++]=prodn[j][2];
else FIRST(prodn[j][2]);
}
}
}
OUTPUT:
How many productions ? :2
Enter 2 productions epsilon= $ :
S=aA/$
A=b/$
Element :S
FIRST(S)= { a }
press 1 to continue : 1
Element :A
FIRST(A)= { b }
press 1 to continue :
0
---------------------------------------------------------
WEEK-3 : FOLLOW
CODE:
#include<stdio.h>
#include<string.h>
void Follow(char q);
void FIRST(char m);
void addToResult(char o);
int count=0;
int i,j,choice;
char prodn[20][20];
char ch;
char addtofollow[20];
char e;
int m=0;
void main()
{
printf("enter the no of production\n");
scanf("%d",&count);
for(int i=0;i<count;i++)
{
scanf("%s%c",prodn[i],&ch);
}
do
{
printf("enter the element\n");
scanf("%c",&e);
Follow(e);
printf("{");
for(int i=0;i<m;i++)
{
printf("%c",addtofollow[i]);
}
printf("}");
printf("enter the choice");
scanf("%d%c",&choice,&ch);
}while(choice==1);
}
void FIRST(char m)
{
if(!isupper(m))
{
addToResult(m);
}
for(int i=0;i<count;i++)
{
if(prodn[i][0]==m)
{
if(prodn[i][2]=='$')
{
Follow(prodn[i][0]);
}
else if(islower(prodn[i][2]))
{
addToResult(prodn[i][2]);
}
else{
FIRST(prodn[i][2]);
}
}
}
}
void Follow(char q)
{
if(prodn[0][0])
{
addToResult('$');
}
for(int i=0;i<count;i++)
{
int d=strlen(prodn[i]);
for(j=1;j<d;j++)
{
if(prodn[i][j]==q)
{
if(prodn[i][j+1]!='$')
{
FIRST(prodn[i][j+1]);
}
if((j==(strlen(prodn[i]))-1)&& q!=prodn[i][0])
{
Follow(prodn[i][0]);
}
}
}
}
}
void addToResult(char o)
{
for(int i=0;i<m;i++)
{
if(addtofollow[i]==o)
{
return;
}
}
addtofollow[m++]=o;
}
OUTPUT:
enter the no of production
2
S=aA
A=$
enter the element
S
{$}enter the choice1
enter the element
A
{$ }enter the choice0
---------------------------------------------------
WEEK-4: TOP DOWN
CODE:
#include<stdio.h>
#include<string.h>
char l;
void match (char);
void E ();
void E1 ();
void F ();
void T ();
void T1 ();
void main ()
{
printf ("enter string:\n");
l = getchar ();
E ();
if (l == '$')
printf ("Given string is accepted \n");
else
printf ("Given string is not accepted \n");
}
void match (char t)
{
if (l == t)
l = getchar ();
else
printf ("error");
}
void F ()
{
if (l == '(')
{
match ('(');
E ();
if (l == ')')
match (')');
}
else
{
if (l == 'i')
match ('i');
if (l == 'd')
match ('d');
}
}
void E1 ()
{
if (l == '+')
{
match ('+');
T ();
E1 ();
}
else
return;
}
void T1 ()
{
if (l == '*')
{
match ('*');
F ();
T1 ();
}
else
return;
}
void T ()
{
F ();
T1 ();
}
void E ()
{
T ();
E1 ();
}
OUTPUT:
enter string:
id*id+id$
Given string is accepted
----------------------------------------------
WEEK-5 : INFIX TO POSTFIX
CODE:
#include<stdio.h>
#include<ctype.h>
char stack[100];
int top = -1;
void push(char x)
{
stack[++top] = x;
}
char pop()
{
if(top == -1)
return -1; else
return stack[top--];
}
int priority(char x)
{
if(x == '(') return 0;
if(x == '+' || x == '-') return 1;
if(x == '*' || x == '/') return 2;
return 0;
}
int main()
{
char exp[100]; char *e, x;
printf("Enter the expression : "); scanf("%s",exp);
printf("\n"); e = exp;
while(*e != '\0')
{
if(isalnum(*e)) printf("%c ",*e);
else if(*e == '(') push(*e);
else if(*e == ')')
{
while((x = pop()) != '(') printf("%c ", x);
}
else
{
while(priority(stack[top]) >= priority(*e)) printf("%c ",pop());
push(*e);
} e++;
}
while(top != -1)
{
printf("%c ",pop());
}return 0;
}
OUTPUT:
Enter the expression : a+b*c+b-c*a
a b c * + b + c a * -
-------------------------------------------------------------------------
WEEK-6 : SHIFT REDUCED PARSE
CODE:
#include<stdio.h>
#include<string.h>
int k=0,z=0,i=0,j=0,c=0;
char a[16],ac[20],stk[15],act[10]; void check();
void main()
{
puts("GRAMMAR is E->E+E \n E->E*E \n E->(E) \n E->id"); puts("enter input
string ");
gets(a); c=strlen(a);
strcpy(act,"SHIFT->"); puts("stack \t input \t action"); for(k=0,i=0;
j<c; k++,i++,j++)
{
if(a[j]=='i' && a[j+1]=='d')
{
stk[i]=a[j];
stk[i+1]=a[j+1];
stk[i+2]='\0';
a[j]=' ';
a[j+1]=' ';
printf("\n$%s\t%s$\t%sid",stk,a,act); check();
}
else
{
stk[i]=a[j];
stk[i+1]='\0';
a[j]=' ';
printf("\n$%s\t%s$\t%ssymbols",stk,a,act); check();
}
}
}
void check()
{
strcpy(ac,"REDUCE TO E");
for(z=0; z<c; z++)
if(stk[z]=='i' && stk[z+1]=='d')
{
stk[z]='E';
stk[z+1]='\0'; printf("\n$%s\t%s$\t%s",stk,a,ac); j++;
}
for(z=0; z<c; z++)
if(stk[z]=='E' && stk[z+1]=='+' && stk[z+2]=='E')
{
stk[z]='E';
stk[z+1]='\0';
stk[z+2]='\0'; printf("\n$%s\t%s$\t%s",stk,a,ac); i=i-2;
}
for(z=0; z<c; z++)
if(stk[z]=='E' && stk[z+1]=='*' && stk[z+2]=='E')
{
stk[z]='E';
stk[z+1]='\0';
stk[z+1]='\0'; printf("\n$%s\t%s$\t%s",stk,a,ac); i=i-2;
}
for(z=0; z<c; z++)
if(stk[z]=='(' && stk[z+1]=='E' && stk[z+2]==')')
{
stk[z]='E';
stk[z+1]='\0';
stk[z+1]='\0'; printf("\n$%s\t%s$\t%s",stk,a,ac); i=i-2;
}
}
OUTPUT:
GRAMMAR is E->E+E
E->E*E
E->(E)
E->id
enter input string
id*id+id
stack input action
$id *id+id$ SHIFT->id
$E *id+id$ REDUCE TO E
$E* id+id$ SHIFT->symbols
$E*id +id$ SHIFT->id
$E*E +id$ REDUCE TO E
$E +id$ REDUCE TO E
$E+ id$ SHIFT->symbols
$E+id $ SHIFT->id
$E+E $ REDUCE TO E
$E $ REDUCE TO E
------------------------------------------------------------------------
WEEK-7 : PREDECATIVE PARSER
CODE:
#include <stdio.h>
#include <string.h>
char prol[7][10] = { "S", "A", "A", "B", "B", "C", "C" };
char pror[7][10] = { "A", "Bb", "Cd", "aB", "@", "Cc", "@" };
char prod[7][10] = { "S->A", "A->Bb", "A->Cd", "B->aB", "B->@", "C->Cc",
"C->@" };
char first[7][10] = { "abcd", "ab", "cd", "a@", "@", "c@", "@" };
char follow[7][10] = { "$", "$", "$", "a$", "b$", "c$", "d$" }; char
table[5][6][10];
int numr(char c)
{
switch (c)
{
case 'S': return 0;
case 'A':
return 1; case 'B':
return 2; case 'C':
return 3; case 'a':
return 0; case 'b':
return 1; case 'c':
return 2; case 'd':
return 3; case '$':
return 4;
}
return (2);
}
int main()
{
int i, j, k;
for (i = 0; i < 5; i++) for (j = 0; j < 6; j++)
strcpy(table[i][j], " ");
printf("The following grammar is used for Parsing Table:\n"); for (i = 0;
i < 7; i++)
printf("%s\n", prod[i]); printf("\nPredictive parsing table:\n");
fflush(stdin);
for (i = 0; i < 7; i++)
{
k = strlen(first[i]);
for (j = 0; j < 10; j++) if (first[i][j] != '@')
strcpy(table[numr(prol[i][0]) + 1][numr(first[i][j]) + 1], prod[i]); }
for (i = 0; i < 7; i++)
{
if (strlen(pror[i]) == 1)
{
if (pror[i][0] == '@')
{
k = strlen(follow[i]); for (j = 0; j < k; j++)
strcpy(table[numr(prol[i][0]) + 1][numr(follow[i][j]) + 1], prod[i]);
}
}
}
strcpy(table[0][0], " ");
strcpy(table[0][1], "a");
strcpy(table[0][2], "b");
strcpy(table[0][3], "c");
strcpy(table[0][4], "d");
strcpy(table[0][5], "$");
strcpy(table[1][0], "S");
strcpy(table[2][0], "A");
strcpy(table[3][0], "B");
strcpy(table[4][0], "C");
printf("\n..........................................................\n");
for (i = 0; i < 5; i++)
for (j = 0; j < 6; j++)
{
printf("%-10s", table[i][j]); if (j == 5)
printf("\n...........................................................\n")
;
}
}
OUTPUT:
The following grammar is used for Parsing Table:
S->A
A->Bb
A->Cd
B->aB
B->@
C->Cc
C->@
Predictive parsing table:
..........................................................
a b c d $
...........................................................
S S->A S->A S->A S->A
...........................................................
A A->Bb A->Bb A->Cd A->Cd
...........................................................
B B->aB B->@ B->@ B->@
...........................................................
C C->@ C->@ C->@
...........................................................
------------------------------------------------------------------------
WEEK-8 : LL1 PARSER
CODE:
#include<string.h>
char s[20],stack[20];
void main()
{
char m[5][6][3]={"tb"," "," ","tb"," "," "," ","+tb"," ","
","n","n","fc"," "," ","fc"," "," "," ","n","*fc"," ","n","n","i"," ","
","(e)"," "," "};
int
size[5][6]={2,0,0,2,0,0,0,3,0,0,1,1,2,0,0,2,0,0,0,1,3,0,1,1,1,0,0,3,0,0};
int i,j,k,n,str1,str2;
printf("\n Enter the input string: "); scanf("%s",s);
strcat(s,"$"); n=strlen(s); stack[0]='$';
stack[1]='e'; i=1;
j=0;
printf("\nStack Input\n"); printf(" \n");
while((stack[i]!='$')&&(s[j]!='$')){
if(stack[i]==s[j]){
i--; j++;}
switch(stack[i]){
case 'e': str1=0; break;
case 'b': str1=1; break;
case 't': str1=2; break;
case 'c': str1=3; break;
case 'f': str1=4; break;
}
switch(s[j]){
case 'i': str2=0; break;
case '+': str2=1; break;
case '*': str2=2; break;
case '(': str2=3; break;
case ')': str2=4; break;
case '$': str2=5; break;
}
if(m[str1][str2][0]=='\0'){
printf("\nERROR");
exit(0);}
else if(m[str1][str2][0]=='n')
i--;
else if(m[str1][str2][0]=='i')
stack[i]='i';
else{
for(k=size[str1][str2]-1;k>=0;k--){
stack[i]=m[str1][str2][k];
i++;
}
i--;
}
for(k=0;k<=i;k++)
printf(" %c",stack[k]);
printf(" ");
for(k=j;k<=n;k++)
printf("%c",s[k]);
printf(" \n ");}
printf("\n SUCCESS");
}
OUTPUT:
Enter the input string: i*i+i
Stack Input
$ b t i*i+i$
$ b c f i*i+i$
$ b c i i*i+i$
$ b c f * *i+i$
$ b c i i+i$
$ b +i$
$ b t + +i$
$ b c f i$
$ b c i i$
$ b $
SUCCESS
----------------------------------------------------------------------
WEEK-9 : THREE ADDRESS CODE
CODE:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
char s[100];
char j='A';
printf("Enter the expression\n");
scanf("%s",s);
printf("3address code\n");
int i=2,n,h;
n=strlen(s);
for(i=2;i<=n;i++)
{
if(s[i]=='*'||s[i]=='/')
{
printf("%c=%c%c%c\n",j,s[i-1],s[i],s[i+1]);
s[i-1]=j++;
for(h=i;h<n-1;h++)
s[h]=s[h+2];
n-=2;
i=i-1;
}
}
for(i=2;i<=n;i++)
{
if(s[i]=='+'||s[i]=='-')
{
printf("%c=%c%c%c\n",j,s[i-1],s[i],s[i+1]);
s[i-1]=j++;
for(h=i;h<n-1;h++)
s[h]=s[h+2];
n-=2;
i=i-1;
}
}
printf("%c=%c",s[0],s[2]);
return 0;
}
OUTPUT:
Enter the expression
z=a+b-c/d*e+f-g/i
3address code
A=c/d
B=A*e
C=g/i
D=a+b
E=D-B
F=E+f
G=F-C
z=G
-------------------------------------------------------------------------