0% found this document useful (0 votes)
22 views33 pages

R20 CD Lab2

The document outlines a series of programming experiments focused on lexical analysis and parsing using C and Lex. It includes tasks such as identifying tokens, implementing lexical analyzers, and simulating parsers, along with example code and expected outputs for each task. The document serves as a practical guide for understanding and applying various parsing techniques in programming.

Uploaded by

StephenKarunakar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views33 pages

R20 CD Lab2

The document outlines a series of programming experiments focused on lexical analysis and parsing using C and Lex. It includes tasks such as identifying tokens, implementing lexical analyzers, and simulating parsers, along with example code and expected outputs for each task. The document serves as a practical guide for understanding and applying various parsing techniques in programming.

Uploaded by

StephenKarunakar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

INDEX

S.No Name of the Experiment Page No.


Write a C program to identify different types of Tokens in a given
1
Program.
Write a Lex Program to implement a Lexical Analyzer using Lex
2
tool.
Write a C program to Simulate Lexical Analyzer to validating a
3
given input String.
Write a C program to implement the Brute force technique of Top
4
down Parsing.
5 Write a C program to implement a Recursive Descent Parser.
Write C program to compute the Follow Sets for the given
6
Grammar.
Write a C program for eliminating the left recursion and left
7
factoring of a given grammar.
Write a C program to check the validity of input string using
8
Predictive Parser.
Write a C program for implementation of LR parsing algorithm to
9
accept a given input string.
Write a C program for implementation of a Shift Reduce Parser
10 using Stack Data Structure to accept a given input string of a
given grammar.

1
1. Write a C program to identify different types of Tokens in a given Program.

#include<ctype.h>
#include<stdio.h>
#include<string.h>
main()
{
int i=0,f,k=0,j,l,n,a,a1;
char temp[10];
char s[100],g[100];
/*
Note: Enter the input below code:
main()
{
int a,b,c;
c=245;
a=b+c;
}
*/
printf( "Enter Program $ for termination:\n");
do
{
gets(g);
if(strcmp(g,"$")==0) goto s1;
for(a1=0;g[a1]!='\0';a1++,i++)
s[i]=g[a1];
}
while(1);
s1:
s[i]='\0';

i=0;
printf("\nvariables:");
while(s[i]!='\0')
{
if(isalpha(s[i]))
{
j=i;
while(isalnum(s[i+1])||s[i+1]=='['||s[i+1]==']')
{
i++;
}

2
if(s[i+1]=='
'||s[i+1]=='('||s[i+1]=='{'||s[i+1]=='\n')
{
i++;
}
else
{
for(;j<=i;j++)
printf("%c",s[j]);
}
printf("");
}
i++;
} /*end of while*/

i=0;
printf("\nOperators:");
while(s[i]!='\0')
{

if(s[i]=='='||s[i]=='+'||s[i]=='-
'||s[i]=='*'||s[i]=='/'||s[i]=='>'||s[i]=='<')
{
printf("%c",s[i]);
printf("");
}
i++;
} /* end of while */

i=0;
printf("\nconstants:");

while(s[i]!='\0')
{

if(isalpha(s[i]))
{
while(isalnum(s[i+1])||s[i+1]=='['||s[i+1]==']')
i++;
i++;
}

if(isdigit(s[i]))
{
k=i;
while(isdigit(s[i+1]))
{

3
i++;
}

for(;k<=i;k++)
printf("%c",s[k]);

printf("");
} /*end of if (after while)*/
i++;
} /*end of while*/

i=0;
printf("\nspecial symbols:");

while(s[i]!='\0')
{

if(s[i]==';'||s[i]==','||s[i]=='('||s[i]==')'||s[i]=='{'||s[i]=='
}'||s[i]=='['||s[i]==']')
printf("%c",s[i]);
i++;
}

i=0;
printf("\nkeywords:");
while(s[i]!='\0')
{
if(isalpha(s[i]))
{
j=i;
while(isalpha(s[i+1]))
{
i++;
}
if(s[i+1]==' ')
{
for(;j<=i;j++)
printf("%c",s[j]);
}
else
{
printf("");
}
}
i++;
} /*end of while*/
}

4
OUTPUT:
Enter Program $ for termination:
main()
{
int a,b,c;
a=2+5;
b=4*6;
c=8/8;
}
$

5
2. Write a Lex Program to implement a Lexical Analyzer using Lex tool.

%{
%}
identifier [a-zA-Z]*
digit [0-9]
letter [a-zA-Z]
int com = 0,com1 = 0;
%%

#.* { printf("\n%s is PRE PROCESSOR DIRECTIVE",yytext); }


int|float|if { if(!com) printf("\n keyword %s ",yytext); }
"/*" { com=1; printf("\n Comments"); }
"*/" { com=0; }
[a-z]+ { if(!com) printf("\n varibles %s ",yytext); }
{identifier}\( { if(!com) printf("\n function %s",yytext); }
\{ { if(!com) printf("\n block begines "); }
\} { if(!com) printf("\n block ends "); }
\".*\" { if(!com) printf("\n string is %s ",yytext); }
[0-9]+ { if(!com) printf("\n numberis %s ",yytext); }
\\ { if(!com) printf("\n\t"); }
= { if(!com) printf("\n assigment %s ",yytext); }
\<=|\>=|\>|\<|\== { if(!com) printf("\n relational operators %s
",yytext); }
%%
main(argc,argv)
int argc;
char **argv;
{
if(argc>1)
{
FILE *file;
file=fopen(argv[1],"r");
if(!file)
{
printf("couldnot open %s",argv[1]);
exit(0);
}
yyin=file;
}
yylex();
}
int yywrap()
{
return(0);
}

6
OUTPUT:
$ lex cse.l
$ cc cse.yy.c
$/a.out temp.c

temp.c
#include<stdio.h>
main()
{
int a,b,c;
a=1;
b=2;
c=a+b;
printf("Sum:%d",c);
}

#include<stdio.h>is PRE PROCESSOR DIRECTIVE


function main
block begines
keyword int
variables a b c
assignment = = =
number is 1 2
function printf
block ends

7
3. Write a C program to Simulate Lexical Analyzer to validating a given input String.

#include<ctype.h>
#include<stdio.h>
#include<string.h>
void main()
{
char s[20],c;
int state=0,i=0;
printf("\n Enter a string:");
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;
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;

8
case 6: printf("\n %s is not recognised.",s);
exit(0);
}
}
if(state==1)
printf("\n %s is accepted under rule 'a'",s);
else if((state==2)||(state==4))
printf("\n %s is accepted under rule 'a*b+'",s);
else if(state==5)
printf("\n %s is accepted under rule 'abb'",s);
getch();
}

OUTPUT:

Enter a String: aaaabbbbb


aaaabbbbb is accepted under rule 'a*b+'

Enter a string: cdgs


cdgs is not recognized

9
4. Write a C program to implement the Brute force technique of Top down Parsing.

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

char s[10],*p;
void s1();
main()
{
printf("Enter the string:");
gets(s);
p=s;
s1();
}
void s1()
{
int a();
if(*p=='c')
{
p++;
if(a()==0)
{
if(*p=='d')
{
p++;
if(*p=='\0')
printf("string parsed successfully.\n");
}
else
printf("sting not parsed.\n");
}
else
printf("string not parsed.\n");
}
else
printf("string not paresd.\n ");
}
int a()
{
char *i;
i=p;
if(*p=='a')
{
p++;
if(*p=='b')

10
{
p++;
return 0;
}
}
p=i;
if(*p=='a')
{
p++;
return 0;
}
}

OUTPUT:
Enter the string:cad
string parsed successfully.

Enter the string:cabd


string parsed successfully.

Enter the string:cbd


string not parsed.

11
5. Write a C program to implement a Recursive Descent Parser.

#include<ctype.h>
#include<stdio.h>
#include<string.h>
char input[100];
int i,l;
void main()
{
printf("\nRecursive Descent Parsing for the following
Grammar:\n");
printf("E->TE'\nE'->+TE'|@\nT->FT'\nT'->*FT'|@\nF->(E)|id\n");
printf("Enter the string to be checked:");
gets(input);
if(E())
{
if(input[i+1]=='\0')
printf("\nString is accepted");
else
printf("\nString is not accepted");
}
else
printf("\nString not accepted");
//getch();
}
E()
{
if(T())
{
if(EP())
return(1);
else
return(0);
}

12
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
return(0);
}

13
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);
}

14
else if(input[i]=='a')
{
i++;
return(1);
}
else
return(0);
}

OUTPUT:
Recursive Descent Parsing for the following Grammar:
E->TE'
E'->+TE'|@
T->FT'
T'->*FT'|@
F->(E)|id

Enter the string to be checked:a+a


String is accepted

Enter the string to be checked:a*a+a


String is accepted

Enter the string to be checked:a*+a


String not accepted

15
6. Write C program to compute the First and Follow Sets for the given Grammar.

#include<stdio.h>
#include<string.h>
char LG[5]={'E','X','T','Y','F'};
char RG[5][5]={
{'T','X',' ',' ',' '},
{'+','T','X',' ','@'},
{'F','Y',' ',' ',' '},
{'*','F','Y',' ','@'},
{'(','E',')',' ','i'}
};
char first[5][2]={
{'(','i'},
{'+','@'},
{'(','i'},
{'*','@'},
{'(','i'} };
char FOLLOW[5][5]={
{'1','1','1','1','1'},
{'1','1','1','1','1'},
{'1','1','1','1','1'},
{'1','1','1','1','1'},
{'1','1','1','1','1'} };

int i,j,k;
main()
{
printf("The given grammar is:\n");
for(i=0;i<5;i++)
{
printf("%c->",LG[i]);
for(j=0;j<5;j++)
{
printf("%c",RG[i][j]);
}
printf("\n");
}
printf("-----------------\n FOLLOW FUNCTION \n--------------
-\n");
FOLLOW[0][0]='$';
for(i=0;i<5;i++)
{
follow(i);
}
for(i=0;i<5;i++)
{
printf(" Follow %c is:",LG[i]);
for(j=0;j<5;j++)
{
if(FOLLOW[i][j]!='1')
printf(" %c",FOLLOW[i][j]);
}
printf("\n");

16
}

follow(j)
{

char a;
int c,d;
a=LG[j];
for(c=0;c<5;c++)
{

for(d=0;d<5;d++)
{
if(a==RG[c][d])
checkf(c,d);
}

}
}

checkf(int c,int d)
{
char b,a;
int m,n,p;

b=RG[c][d+1];
if(isalpha(b))
{
for(k=0;k<5;k++)
{
if(b==LG[k])
{
for(m=0;m<2;m++)
{
if(first[k][m]!='@')
FOLLOW[i][m]=first[k][m];
else
{

for(n=0,p=0;n<5;n++)
{
if(FOLLOW[i][n]=='1')
FOLLOW[i][n]=FOLLOW[c][n];
else
FOLLOW[i][++p]=FOLLOW[c][n];
}
}
}
}
}
}

17
else if(b==' ')
{
for(k=0;k<5;k++)
{
if(FOLLOW[i][k]=='1')
{
FOLLOW[i][k]=FOLLOW[c][k];
}
}
}
else
{
for(k=0;k<5;k++)
{
if(FOLLOW[i][k]=='1')
{
FOLLOW[i][k]=RG[c][d+1];
break;
}
}
}
}

OUTPUT:

The given grammar is:


E->TX
X->+TX @
T->FY
Y->*FY @
F->(E) i
-----------------
FOLLOW FUNCTION
---------------
Follow E is: $ )
Follow X is: $ )
Follow T is: + $ )
Follow Y is: + $ )
Follow F is: * + $ )

18
7. Write a C program for eliminating the left recursion and left factoring of a given
grammar

#include<stdio.h>
#include<string.h>
int main()
{
char input[100],*l,*r,*temp,tempprod[20],productions[25][50];
int i=0,j=0,flag=0;
printf("Enter the productions: ");
scanf("%s",input);
l = strtok(input,"->");
r = strtok(NULL,"->");
temp = strtok(r,"|");
while(temp) {
if(temp[0] == l[0]) {
flag = 1;
sprintf(productions[i++],"%s'->%s%s'",l,temp+1,l);
}
else
sprintf(productions[i++],"%s->%s%s'",l,temp,l);
temp = strtok(NULL,"|");
}
sprintf(productions[i++],"%s->^",l);
if(flag == 0)
printf("The given productions don't have Left Recursion");
else
for(j=0;j<i;j++) {
printf("\n%s",productions[j]);
}
}

OUTPUT:
Enter the productions: E->E+T|a

E'->+TE'
E->aE'
E->^

19
8. Write a C program to check the validity of input string using Predictive Parser.

#include<stdio.h>
#include<string.h>
#include <stdlib.h>
int ip=0,point=0,top,mpoint=-1;
char inp[20],stack[100],match[100];
void push(char k)
{
if(top==99)
{
printf("string not parsed");
}
else
stack[++top]=k;
}
void pop()
{
if(top==-1)
{
printf("string not accepeted");
}
else
top--;
}
void matched(char k)
{
match[++mpoint]=k;
}
void E(char c)
{
if(c=='i' || c=='(')
{
printf("E-->TE'\n");
pop();
push('e');
push('T');
}

else
{
printf("string not parsed");
exit(0);
}
}
void Ep(char c)
{
if(c=='+')
{
printf("E'-->+TE'\n");
pop();
push('e');
push('T');

20
push('+');
}
else if(c==')' || c=='$')
{
printf("E'-->^\n");

pop();
}
else
{
printf("string not parsed");
exit(0);
}
}
void T(char c)
{
if(c=='i' || c=='(')
{
printf("T-->FT'\n");
pop();
push('t');
push('F');
}
else
{
printf("string not parsed");
}
}
void Tp(char c)
{
if(c=='*')
{
printf("T'-->*FT'\n");
pop();
push('t');
push('F');
push('*');
}

else
if(c==')' || c=='$' ||c=='+')
{
printf("T'-->^\n");
pop();
}
else
{
printf("string not parsed");
exit(0);
}
}
void F(char c)
{
if(c=='i')
{

21
printf("F-->i\n");
pop();
push('i');
}
else if(c=='(')
{
printf("F-->(E)\n");
pop();
push(')');
push('E');
push('(');
}
else
printf("string not parsed");
}

main()
{
int i,a;
//clrscr();
top=-1;
mpoint=-1;
printf("Grammar\n");
printf("E-->TE' \n");
printf("E'-->+TE' \n");
printf("T-->FT' \n");
printf("T'-->*FT'\n");
printf("F-->(E) | i \n");
printf("Enter the ip $:");
scanf("%s",inp);
printf("\n");
printf("MATCHED\tSTACK\tINPUT\tACTION\n");
printf("____________________________________\n");
push('$');
push('E');

while(1)
{
a=0;
printf("\n");
while(a<=mpoint)
{
if(a==-1)
printf("\t");
printf("%c",match[a]);
a++;
}
printf("\t\t");
i=top;
while(i!=-1)
{
printf("%c",stack[i]);
i--;
}
printf("\t\t");

22
i=point;
while(inp[i]!='\0')
{
printf("%c",inp[i]);
i++;
}
printf("\t\t");

if(stack[top]=='E') E(inp[point]);
else if(stack[top]=='e') Ep(inp[point]);
else if(stack[top]=='T') T(inp[point]);
else if(stack[top]=='t') Tp(inp[point]);
else if(stack[top]=='F') F(inp[point]);
else if(stack[top]=='i' && inp[point]=='i')
{
printf(" POP");
matched(inp[point]);
point++;
pop();
}
else if(stack[top]=='+' && inp[point]=='+')
{
printf(" POP");
pop();
matched(inp[point]);
point++;
}
else if(stack[top]=='*' && inp[point]=='*')
{
printf(" POP");
pop();
matched(inp[point]);
point++;
}

else if(stack[top]=='(' && inp[point]=='(')


{
printf(" POP");
pop();
matched(inp[point]);
point++;
}
else if(stack[top]==')' && inp[point]==')' )
{
printf(" POP");
pop();
matched(inp[point]);
point++;
}
else if(stack[top]=='$' && inp[point]=='$')
{
printf("\tstring Parsed");
exit(0);
}

23
else
{
printf("string not parsed");
exit(0);
}
}
getch();
}

OUTPUT:
Grammar
E-->TE'
E'-->+TE'
T-->FT'
T'-->*FT'
F-->(E) | i
Enter the ip $:i*i$

MATCHED STACK INPUT ACTION


_________________________________________

E$ i*i$ E-->TE'

Te$ i*i$ T-->FT'

Fte$ i*i$ F-->i

ite$ i*i$ POP


i te$ *i$ T'-->*FT'

i *Fte$ *i$ POP


i* Fte$ i$ F-->i
i* ite$ i$ POP
i*i te$ $ T'-->^
i*i e$ $ E'-->^
i*i $ $ string Parsed

24
9. Write a C program for implementation of LR parsing algorithm to accept a given
input string.

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int point=0,top;
char inp[20],stack[100];
void push(char);
void pop();
void s0(char);
void s1(char);
void s2(char);
void s3(char);
void s4(char);
void s5(char);
void s8(char);
void push(char k)
{
if(top==99)
{
printf("string not accepted");
}
else
stack[++top]=k;
}
void pop()
{
if(top==-1)
{
printf("string not accepted");
}
else
top--;
}
void s0(char l)
{
if(l=='e')
{
printf("shift3 \n");
push(l);
push('3');
point++;
}
else if(l=='d')
{
printf("shift4 \n");
push(l);
push('4');
point++;
}
else
{
printf("string not accepted");

25
exit(0);
}
}
void s1(char l)
{
if(l=='$')
{
push('$');
printf("string accepted");
exit(0);
}
}
void s2(char l)
{
if(l=='e')
{
printf("shift3 \n");
push(l);
push('3');
point++;
}
else if(l=='d')
{
printf("shift4 \n");
push(l);
push('4');
point++;
}
else
{
printf("string not accepted");
exit(0);
}
}
void s3(char l)
{
if(l=='e')
{
printf("shift3 \n");
push(l);
push('3');
point++;
}
else if(l=='d')
{
printf("shift4 \n");
push(l);
push('4');
point++;
}
else
{
printf("string not accepted");
exit(0);
}

26
}
void s4(char l)
{
if(l=='e'||l=='d'||l=='$')
{
printf("reduce C->d \n");
pop();
pop();
push('C');
}
else
{
printf("string not accepted");
exit(0);
}
}
void s5(char l)
{
if(l=='$')
{
printf("reduce S->CC \n");
pop();
pop();
pop();
pop();
push('S');
}
else
{
printf("string not accepted");
exit(0);
}
}
void s8(char l)
{
if(l=='e'||l=='d'||l=='$')
{
printf("reduce C->eC \n");
pop();
pop();
pop();
pop();
push('C');
}
else
{
printf("string not accepted");
exit(0);
}
}

27
main()
{
int i,j=0,s=0,a,x;
top=-1;

printf("Grammer:\n");
printf("S->CC \n C->eC \n C->d \n");
printf("enter the ip $:");
scanf("%s",&inp);
printf("\n");
printf("STACK \t INPUT \t ACTON \n");
Printf(“_____________________________\n”);
push('$');
push('0');
while(1)
{
j=top;
a=0;
printf("\n");
while(a<=j)
{
printf("%c",stack[a]);
a++;
}
printf("\t");
s=point;
while(inp[s]!='\0')
{
printf("%c",inp[s]);
s++;
}
printf("\t");
if(stack[top]=='0') s0(inp[point]);
else if(stack[top]=='1') s1(inp[point]);
else if(stack[top]=='2') s2(inp[point]);
else if(stack[top]=='3') s3(inp[point]);
else if(stack[top]=='4') s4(inp[point]);
else if(stack[top]=='5') s5(inp[point]);
else if(stack[top]=='8') s8(inp[point]);
else if(stack[top]=='S')
{
x=top-1;
if(stack[x]=='0')
{ printf("shift1");
push('1'); }
else
printf("string not accepted");
}
else if(stack[top]=='C')
{
x=top-1;
if(stack[x]=='0') {
printf("shift2");
push('2'); }

28
else if(stack[x]=='2')
{ printf("shift5");
push('5');
}
else if(stack[x]=='3')
{ printf("shift8");
push('8');
}
else
printf("string not accepted");
}
else
{
printf("string not accepted");
exit(0);
}
}
}

OUTPUT:
Grammer:
S->CC
C->eC
C->d
enter the ip $:edd$
STACK INPUT ACTON
______________________________________

$0 edd$ shift3
$0e3 dd$ shift4
$0e3d4 d$ reduce C->d
$0e3C d$ shift8
$0e3C8 d$ reduce C->eC
$0C d$ shift2
$0C2 d$ shift4
$0C2d4 $ reduce C->d
$0C2C $ shift5
$0C2C5 $ reduce S->CC
$0S $ shift1
$0S1 $ string accepted

29
10. Write a C program for implementation of a Shift Reduce Parser using Stack Data
Structure to accept a given input string of a given grammar.

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char stack[15],input[15];
int ip=0,top=-1;
void push(char k)
{
top=top+1;
stack[top]=k;
}
void pop()
{
top--;
}

void main()
{
int i;
clrscr();
printf("The Grammar:\nE-->E+E\nE-->E*E\nE-->E/E\nE-->id\n");
printf("Enter the input string with followed by $:");
gets(input);
push('$');
printf("STACK\tINPUT\tACTION\n");
printf(“_________________________”);
while(1)
{
i=0;
while(i<=top)
{
printf("%c",stack[i]);
i++;
}
printf("\t");
i=ip;
while(input[i]!='\0')
{
printf("%c",input[i]);
i++;
}
printf("\t");
if(stack[top]=='$')
{
push(input[ip]);
printf(" Shift %c\n",input[ip]);
ip++;
}
else if(stack[top]=='i')
{
pop();
push('E');
printf("Reduce E-->i\n");

30
}
else if(stack[top]=='E' && input[ip]!='$')
{
push(input[ip]);
printf("shift %c\n",input[ip]);
ip++;
}
else if(stack[top]=='*' || stack[top]=='+')
{
push(input[ip]);
printf("shift %c\n",input[ip]);
ip++;
}

else if(stack[top]=='E' && input[ip]=='$')


{
if(stack[top-1]=='+')
{ pop();
pop();
pop();
push('E');
printf("Reduce E-->E+E\n");
}
else if(stack[top-1]=='*')
{
pop();
pop();
pop();
push('E');
printf("Reduce E-->E*E\n");
}
else if(stack[top-1]=='$')
{
printf(" Accepted \n");
exit(0);
}
}
else
{
exit(0);
}
}
}

31
OUTPUT:
The Grammar:
E-->E+E
E-->E*E
E-->E/E
E-->id
Enter the input string with followed by $:i*i$

STACK INPUT ACTION


________________________________
$ i*i$ Shift i
$i *i$ Reduce E-->i
$E *i$ shift *
$E* i$ shift i
$E*i $ Reduce E-->i
$E*E $ Reduce E-->E*E
$E $ Accepted

32
33

You might also like