Compiler
Compiler
PROJECT REPORT ON
------Distributed System------
1 Design a lexical analyzer for given language and the lexical analyzer 1
should ignore
redundant spaces, tabs and new lines.
2 * Write a C program to identify whether a given line is a comment or
not. 4
3
*Write a C program to recognize strings under 'a', 'a*b+', 'abb'. 5
4 *Write a C program to test whether a given identifier is valid or not. 8
5 *Write a C program to simulate lexical analyzer for validating 9
operators
6 Implement the lexical analyzer using JLex, flex or other lexical 11
analyzer generating tools.
7 Write a C program for implementing the functionalities of predictive
parser for the mini
language specified in Note 1.
8 a) *Write a C program for constructing of LL (1) parsing. 17
b) *Write a C program for constructing recursive descent parsing.
9 Write a C program to implement LALR parsing. 24
1.1 OBJECTIVE:
Design a lexical analyzer for given language and the lexical analyzer should ignore redundant spaces, tabs and new
lines. It should also ignore comments. Although the syntax specification states that identifiers can be
arbitrarily long, you may restrict the length to some reasonable value. Simulate the same in C language.
1.2 RESOURCE:
Turbo C ++
1.4 PROCEDURE:
#include<ctype.h>
#include<stdio.h>
if(strcmp("for",str)==0||strcmp("while",str)==0||strcmp("do",str)==0||strcmp("int",str)==0||str
cmp("float",str)==0||strcmp("char",str)==0||strcmp("double",str)==0||strcmp("static",str)==0||strcmp("switch",str
)==0||strcmp("case",str)==0)
printf("\n%s is a keyword",str);
else
printf("\n%s is an identifier",str);
main()
FILE *f1,*f2,*f3;
char c,str[10],st1[10];
int num[100],lineno=0,tokenvalue=0,i=0,j=0,k=0;
f1=fopen("input","w");
while((c=getchar())!=EOF)
putc(c,f1);
fclose(f1);
f1=fopen("input","r");
f2=fopen("identifier","w");
f3=fopen("specialchar","w");
while((c=getc(f1))!=EOF) {
if(isdigit(c))
tokenvalue=c-'0';
1
c=getc(f1);
while(isdigit(c)) {
tokenvalue*=10+c-'0';
c=getc(f1);
num[i++]=tokenvalue;
ungetc(c,f1);
else
if(isalpha(c))
putc(c,f2);
c=getc(f1);
while(isdigit(c)||isalpha(c)||c=='_'||c=='$')
putc(c,f2);
c=getc(f1);
putc(' ',f2);
ungetc(c,f1);
else
if(c==' '||c=='\t')
printf(" ");
else
if(c=='\n')
lineno++;
else
putc(c,f3);
}
fclose(f2);
fclose(f3);
fclose(f1);
for(j=0;j<i;j++)
printf("%d",num[j]);
printf("\n");
f2=fopen("identifier","r");
k=0;
while((c=getc(f2))!=EOF) {
if(c!=' ')
str[k++]=c;
else
str[k]='\0';
keyword(str);
k=0; }
2
}
fclose(f2);
f3=fopen("specialchar","r");
while((c=getc(f3))!=EOF)
printf("%c",c);
printf("\n");
fclose(f3);
1. What is token?
2. What is lexeme?
{
int a[3],t1,t2;
t1=2; a[0]=1; a[1]=2; a[t1]=3;
t2=-(a[2]+t1*6)/(a[2]-t1);
if t2>5 then
print(t2);
else {
int t3;
t3=99;
t2=-25;
print(-t1+t2*t3); /* this is a comment on 2 lines */
} endif
}
$
Output:
Variables : a[3] t1 t2 t3
Operator : - + * / >
Constants : 2 1 3 6 5 99 -25
Keywords : int if then else endif
Special Symbols : , ; ( ) { }
3
EXPERIMENT-2
2.1 OBJECTIVE:
2.2 RESOURCE:
Turbo C++
Check whether the string is starting with ‘/’ and check next character is ‘/’ or’*’.
2.4 PROCEDURE:
2.5 PROGRAM:
#include<stdio.h>
#include<conio.h>
void main() {
char com[30];
int i=2,a=0;
clrscr();
gets(com);
if(com[0]=='/') {
if(com[1]=='/')
printf("\n It is a comment");
else if(com[1]=='*') {
for(i=2;i<=30;i++)
if(com[i]=='*'&&com[i+1]=='/')
{
printf("\n It is a comment");
a=1;
break; }
else
continue; }
if(a==0)
else
else
getch(); }
Output: It is a comment
Input: Enter comment: hello
Output: It is not a comment
4
EXPERIMENT-3
3.1 OBJECTIVE:
3.2 RESOURCE:
Turbo C++
3.4 PROCEDURE:
3.5 PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void main()
char s[20],c;
int state=0,i=0;
clrscr();
gets(s);
while(s[i]!='\0')
switch(state)
{
case 0: c=s[i++];
if(c=='a')
state=1;
else if(c=='b')
state=2;
else
state=6;
break;
case 1: c=s[i++];
if(c=='a')
state=3;
5
else if(c=='b')
state=4;
else
state=6;
break;
case 2: c=s[i++];
if(c=='a')
state=6;
else if(c=='b')
state=2;
else
state=6;
break;
case 3: c=s[i++];
if(c=='a')
state=3;
else if(c=='b')
state=2;
else
state=6;
break;
case 4: c=s[i++];
if(c=='a')
state=6;
else if(c=='b')
state=5;
else
state=6;
break;
case 5: c=s[i++];
if(c=='a')
state=6;
else if(c=='b')
state=2;
else
state=6;
break;
exit(0);
6
I f(state==1)
else if((state==2)||(state==4))
else if(state==5)
getch();
Input :
Output:
4.2 RESOURCE:
Turbo C++
Check the initial character of the string is numerical or any special character except ‘_’ then print it is not a valid
identifier.
Otherwise print it as valid identifier if remaining characters of string doesn’t contains any special characters except
‘_’.
4.4 PROCEDURE:
4.5 PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
char a[10];
clrscr();
gets(a);
if(isalpha(a[0]))
flag=1;
else
while(a[i]!='\0')
{
if(!isdigit(a[i])&&!isalpha(a[i]))
flag=0;
break;
i++;
if(flag==1)
getch();
Output:
Valid identifier
Enter an identifier:1aqw
8
EXPERIMENT-5
5.1 OBJECTIVE:
5.2 RESOURCE:
Turbo C++
5.4 PROCEDURE:
5.5 PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
char s[5];
clrscr();
else
printf("\nLess than");
break;
case'=': if(s[1]=='=')
printf("\nEqual to");
else
printf("\nAssignment");
break;
case'!': if(s[1]=='=')
printf("\nNot Equal");
else
break;
case'|': if(s[1]=='|')
printf("\nLogical OR");
9
else
printf("\nBitwise OR");
break;
case'+': printf("\n Addition");
break;
case'-': printf("\nSubstraction");
break;
case'*': printf("\nMultiplication");
break;
case'/': printf("\nDivision");
break;
case'%': printf("Modulus");
break;
default: printf("\n Not a operator");
}
getch();
Input
Output
Multiplication
10
EXPERIMENT-6
6.1 OBJECTIVE:
Implement the lexical analyzer using JLex, flex or other lexical analyzer generating tools.
6.2 RESOURCE:
Check whether the string is identifier/ keyword /symbol by using the rules of identifier and keywords using LEX
Tool
6.4 PROCEDURE:
6.5 PROGRAM:
/* program name is lexp.l */
%{
/* program to recognize a c program */
int COMMENT=0;
%}
identifier [a-zA-Z][a-zA-Z0-9]*
%%
#.* { printf("\n%s is a PREPROCESSOR DIRECTIVE",yytext);}
int |float |char |double |while |for |do |if |break |continue |void |switch |case |long |struct |const |typedef |return
|else |goto {printf("\n\t%s is a KEYWORD",yytext);}
"/*" {COMMENT = 1;}
/*{printf("\n\n\t%s is a COMMENT\n",yytext);}*/
if (argc > 1)
{
FILE *file;
}
yyin = file;
}
yylex();
11
printf("\n\n");
return 0;
} int yywrap()
{
return 0;
}
1. Write a program that defines auxiliary definitions and translation rules of Pascal tokens?
2. Write a program that defines auxiliary definitions and translation rules of C tokens?
3. Write a program that defines auxiliary definitions and translation rules of JAVA tokens
1. What is Jlex?
2. What is Flex?
main()
{
int a,b;
}
Output
$lex lex.l
$cc lex.yy.c
$./a.out var.c
#include<stdio.h> is a PREPROCESSOR DIRECTIVE
FUNCTION
main (
)
BLOCK BEGINS
int is a KEYWORD
a IDENTIFIER
b IDEN
TIFIER
BLOCK ENDS
12
EXPERIMENT-7
7.1 OBJECTIVE:
Write a C program for implementing the functionalities of predictive parser for the mini language specified in Note
1.
7.2 RESOURCE:
Turbo C++
Verify the FIRST of non terminal and insert the production in the FIRST value
If we have any @ terms in FIRST then insert the productions in FOLLOW values
7.4 PROCEDURE:
7.5 PROGRAM:
#include<stdio.h>
#include<conio.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 table[5][6][10];
numr(char c)
switch(c)
13
return(2);
void main()
int i,j,k;
clrscr();
for(i=0;i<5;i++)
for(j=0;j<6;j++)
strcpy(table[i][j]," ");
grammar:\n"); for(i=0;i<7;i++)
printf("%s\n",prod[i]);
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]);
14
}
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");
getch();
6. What are the derivation methods to generate a string for the given grammar?
15
7.7 LAB ASSIGNMENT:
→ → →
E TE' E' +TE'/î T FT’
→ →
T' *FT'/î F (E)/i
→ →
S iCtSS’ S’ eS/ î
3. Write a program to construct predictive parsing table for the following grammar?
→ →
S iCtSS’ S’ eS/ î
7.8 POST LAB QUESTIONS
5. What is LR Parser?
S->A
A->Bb
A->Cd
B->aB
B->@
C->Cc
C->@
------------------------------------------------------------------
a b c d $
------------------------------------------------------------------
S S->AS->AS->AS->A
------------------------------------------------------------------
------------------------------------------------------------------
------------------------------------------------------------------
C C->@C->@ C->@
------------------------------------------------------------------
16
EXPERIMENT-8(a)
8.1 OBJECTIVE:
8.2 RESOURCE:
Turbo C++
Using predictive parsing table parse the given input using stack .
If stack [i] matches with token input string pop the token else shift it repeat the process until it reaches to $.
8.4 PROCEDURE:
8.5 PROGRAM
#include<stdio.h>
#include<conio.h>
#include<string.h>
char s[20],stack[20];
void main()
char m[5][6][3]={"tb"," "," ","tb"," "," "," ","+tb"," "," ","n","n","fc"," "," ","fc"," "," ","
","n","*fc"," a ","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;
clrscr();
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++;
17
}
switch(stack[i])
break;
break;
break;
break;
break;
switch(s[j])
break;
break;
break;
break;
break;
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')
18
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");
getch(); }
19
EXPERIMENT-8(b)
8.1 OBJECTIVE:
T->FT'
T`->*FT'/@
F->(E)/ID
8.2 RESOURCE:
Turbo C++
Verify the next token equals to non terminals if it satisfies match the non terminal.
8.4 PROCEDURE:
8.5 PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
char input[100];
int i,l;
void main()
clrscr();
printf("\nRecursive descent parsing for the following grammar\n");
printf("\nE->TE'\nE'->+TE'/@\nT->FT'\nT'->*FT'/@\nF->(E)/ID\n")
; printf("\nEnter the string to be checked:"); gets(input);
if(E())
if(input[i+1]=='\0')
printf("\nString is accepted");
else
else
20
getch();
E()
if(T())
if(EP())
return(1);
else
return(0);
else
return(0);
EP()
if(input[i]=='+')
i++;
if(T())
if(EP())
return(1);
else
return(0);
else
return(0);
}
else
return(1);
T()
if(F())
if(TP())
return(1);
else
return(0);
else
21
return(0);
TP()
if(input[i]=='*')
i++;
if(F())
if(TP())
return(1);
else
return(0);
else
return(0);
else
return(1);
F()
if(input[i]=='(')
i++;
if(E())
if(input[i]==')')
{
i++;
return(1);
else
return(0);
else
return(0);
else if(input[i]>='a'&&input[i]<='z'||input[i]>='A'&&input[i]<='Z')
i++;
return(1);
22
}
else
return(0);
E->TE'
E'->+TE'/@
T->FT'
T'->*FT'/@
F->(E)/ID
String is accepted
E->TE'
E'->+TE'/@
T->FT'
T'->*FT'/@
F->(E)/ID
9.1 OBJECTIVE:
Write a program to Design LALR Bottom up Parser.
9.2 RESOURCE:
TURBO C++
Push the input symbol with its state symbols in to the stack by referring lookaheads
9.4 PROCEDURE:
9.5 PROGRAM:
/*LALR PARSER
E->E+T
E->T
T->T*F
T->F
F->(E)
F->i
*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
void isproduct(char,char);
int ister(char);
int isnter(char);
int isstate(char);
void error();
void isreduce(char,char);
struct action
char row[6][5];
};
24
const struct action A[12]={
{"sf","emp","emp","se","emp","emp"},
{"emp","sg","emp","emp","emp","acc"},
{"emp","rc","sh","emp","rc","rc"},
{"emp","re","re","emp","re","re"},
{"sf","emp","emp","se","emp","emp"},
{"emp","rg","rg","emp","rg","rg"},
{"sf","emp","emp","se","emp","emp"},
{"sf","emp","emp","se","emp","emp"},
{"emp","sg","emp","emp","sl","emp"},
{"emp","rb","sh","emp","rb","rb"},
{"emp","rb","rd","emp","rd","rd"},
{"emp","rf","rf","emp","rf","rf"}
};
struct gotol
char r[3][4];
};
{"b","c","d"},
{"emp","emp","emp"},
{"emp","emp","emp"},
{"emp","emp","emp"},
{"i","c","d"},
{"emp","emp","emp"},
{"emp","j","d"},
{"emp","emp","k"},
{"emp","emp","emp"},
{"emp","emp","emp"},
};
char ter[6]={'i','+','*',')','(','$'};
char nter[3]={'E','T','F'};
char states[12]={'a','b','c','d','e','f','g','h','m','j','k','l'};
char stack[100];
int top=-1;
char temp[10];
struct grammar
25
char left;
char right[5];
};
{'E',"e+T"},
{'E',"T"},
{'T',"T*F"},
{'T',"F"},
{'F',"(E)"},
{'F',"i"},
};
void main()
char inp[80],x,p,dl[80],y,bl='a';
int i=0,j,k,l,n,m,c,len;
clrscr();
scanf("%s",inp);
len=strlen(inp);
inp[len]='$';
inp[len+1]='\0';
push(stack,&top,bl);
printt(stack,&top,inp,i);
do
x=inp[i];
p=stacktop(stack);
isproduct(x,p);
if(strcmp(temp,"emp")==0)
error();
if(strcmp(temp,"acc")==0)
break;
else
if(temp[0]=='s')
push(stack,&top,inp[i]);
push(stack,&top,temp[1]);
i++;
26
}
else
if(temp[0]=='r')
j=isstate(temp[1]);
strcpy(temp,rl[j-2].right);
dl[0]=rl[j-2].left;
dl[1]='\0';
n=strlen(temp);
for(k=0;k<2*n;k++)
pop(stack,&top);
for(m=0;dl[m]!='\0';m++)
push(stack,&top,dl[m]);
l=top;
y=stack[l-1];
isreduce(y,dl[0]);
for(m=0;temp[m]!='\0';m++)
push(stack,&top,temp[m]);
printt(stack,&top,inp,i);
}while(inp[i]!='\0');
if(strcmp(temp,"acc")==0)
else
getch();
}
if(*sp==100)
else
*sp=*sp+1;
27
s[*sp]=item;
char i;
i=s[top];
return i;
int k,l;
k=ister(x);
l=isstate(p);
strcpy(temp,A[l-1].row[k-1]);
int ister(char x)
int i;
for(i=0;i<6;i++)
if(x==ter[i])
return i+1;
return 0;
int isnter(char x)
int i;
for(i=0;i<3;i++)
if(x==nter[i])
return i+1;
return 0;
int isstate(char p)
int i;
for(i=0;i<12;i++)
if(p==states[i])
28
return i+1;
return 0;
void error()
exit(0);
int k,l;
k=isstate(x);
l=isnter(p);
strcpy(temp,G[k-1].r[l-1]);
char item;
if(*sp==-1)
else
item=s[*sp];
*sp=*sp-1;
return item;
}
void printt(char *t,int *p,char inp[],int i)
int r;
printf("\n");
for(r=0;r<=*p;r++)
rep(t,r);
printf("\t\t\t");
for(r=i;inp[r]!='\0';r++)
29
printf("%c",inp[r]);
char c;
c=t[r];
switch(c)
break;
break;
break;
break;
break;
break;
break;
break;
break;
break;
break;
default :printf("%c",t[r]);
break;
30
9.6 PRE-LAB QUESTIONS
→
1 Write a program to compute FOLLOW for the following grammar? E
TE'
→ →
E' +TE'/î T FT’
→ →
T' *FT'/î F (E)/i
2 Write a program to construct LALR parsing table for the following grammar.
→
S iCtSS’
→
S’ eS/ î
Output
Stack input
0 i*i+i$
0i5 *i+i$
0F3 *i+i$
0T2 *i+i$
0T2*7 i+i$
0T2*7i5 +i$
0T2*7i5F10 +i$
0T2 +i$
0E1 +i$
0E1+6 i$
0E1+6i5 $
0E1+6F3 $
0E1+6T9 $
0E1 $
accept the input*/
31
EXPERIMENT-10(a)
10.1 OBJECTIVE:
10.2 RESOURCE:
Turbo C++
Find the handle enclosed in < . > and reduce it to production symbol.
10.4 PROCEDURE:
10.5 PROGRAM:
#include<stdio.h>
char str[50],opstr[75];
int f[2][9]={2,3,4,4,4,0,6,6,0,1,1,3,3,5,5,0,5,0};
int col,col1,col2;
char c;
swt()
switch(c)
case'+':col=0;break;
case'-':col=1;break;
case'*':col=2;break;
case'/':col=3;break;
case'^':col=4;break;
case'(':col=5;break;
case')':col=6;break;
case'd':col=7;break;
case'$':col=8;break;
default:printf("\nTERMINAL MISSMATCH\n");
exit(1);
32
break;
// return 0;
main()
int i=0,j=0,col1,cn,k=0;
int t1=0,foundg=0;
char temp[20];
clrscr();
scanf("%s",&str);
while(str[i]!='\0')
i++;
str[i]='$';
str[++i]='\0';
printf("%s\n",str);
come:
i=0;
opstr[0]='$';
j=1;
c='$';
swt();
col1=col;
c=str[i];
swt();
col2=col;
if(f[1][col1]>f[2][col2])
{
opstr[j]='>';
j++;
else if(f[1][col1]<f[2][col2])
opstr[j]='<';
j++;
33
else
opstr[j]='=';j++;
while(str[i]!='$')
c=str[i];
swt();
col1=col;
c=str[++i];
swt();
col2=col;
opstr[j]=str[--i];
j++;
if(f[0][col1]>f[1][col2])
opstr[j]='>';
j++;
else if(f[0][col1]<f[1][col2])
opstr[j]='<';
j++;
else
opstr[j]='=';j++;
}
i++;
opstr[j]='$';
opstr[++j]='\0';
printf("\nPrecedence Input:%s\n",opstr);
i=0;
j=0;
while(opstr[i]!='\0')
34
{
foundg=0;
while(foundg!=1)
if(opstr[i]=='\0')goto redone;
if(opstr[i]=='>')foundg=1;
t1=i;
i++;
if(foundg==1)
for(i=t1;i>0;i--)
if(opstr[i]=='<')break;
if(i==0){printf("\nERROR\n");exit(1);}
cn=i;
j=0;
i=t1+1;
while(opstr[i]!='\0')
temp[j]=opstr[i];
j++;i++;
temp[j]='\0';
opstr[cn]='E';
opstr[++cn]='\0';
strcat(opstr,temp);
printf("\n%s",opstr);
i=1;
redone:k=0;
while(opstr[k]!='\0')
k++;
if(opstr[k]=='<')
Printf("\nError");
exit(1);
35
}
if((opstr[0]=='$')&&(opstr[2]=='$'))goto sue;
i=1
while(opstr[i]!='\0')
c=opstr[i];
if(c=='+'||c=='*'||c=='/'||c=='$')
temp[j]=c;j++;}
i++;
temp[j]='\0';
strcpy(str,temp);
goto come;
sue:
printf("\n success");
return 0;
(d*d)+d$
Output:
(d*d)+d$
Precedence input:$<(<d>*<d>)>+<d>$
$<(E*<d>)>+<d>$
$<(E*E)>+<E>$
$E+<E>$
$E+E$
Precedence input:$<+>$
$E$
success
36
EXPERIMENT-10(b)
10.1 OBJECTIVE:
Program to implement semantic rules to calculate the expression that takes an expression with digits, + and * and
computes the value.
10.2 RESOURCE:
10.3 PROCEDURE:
10.4 PROGRAM:
<parser.l>
%{
#include<stdio.h>
#include "y.tab.h"
%}
%%
[0-9]+ {yylval.dval=atof(yytext);
return DIGIT;
%%
<parser.y>
%{
/*This YACC specification file generates the LALR parser for the program
#include<stdio.h>
%}
%union
double dval;
%%
37
printf("%g\n",$1);
| term
| factor
| DIGIT
%%
int main()
yyparse();
yyerror(char *s)
printf("%s",s);
$lex parser.l
$yacc –d parser.y
$./a.out
2+3
5.0000
38