0% found this document useful (0 votes)
2 views

CD Lab Manual compressed(1)

The document outlines the implementation of a Lexical Analyzer using the Lex tool and a C program for computing First and Follow sets for a given grammar. It includes code snippets for recognizing C program constructs, handling comments, and parsing grammar rules. Additionally, it describes the implementation of LR parsing and code generation algorithms for expressions/statements.

Uploaded by

gan
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)
2 views

CD Lab Manual compressed(1)

The document outlines the implementation of a Lexical Analyzer using the Lex tool and a C program for computing First and Follow sets for a given grammar. It includes code snippets for recognizing C program constructs, handling comments, and parsing grammar rules. Additionally, it describes the implementation of LR parsing and code generation algorithms for expressions/statements.

Uploaded by

gan
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/ 2

if (argc > 1) { FILE *file;

file = fopen(argv[1],"r");
if(!file) {
EXERCISE 2 printf("could not open %s \n",argv[1]); EXERCISE 6
exit(0); } yyin = file; } yylex();
Write a Lex Program to implement a Lexical Analyzer using Lex tool. printf("\n\n Total No.Of comments are %d",cnt); Write C program to compute the First and Follow Sets for the given Grammar.
return 0; } int yywrap()
AIM: To write a Lex program to implement a Lexical Analyzer using Lex tool. { return AIM: To write C program to compute the First and Follow Sets for the given Grammar.
1;
PROGRAM: DESCRIPTION: This C program computes the First and Follow sets for the given grammar
/* program name is lexp.l */ } using a bottom-up approach. It reads the production rules from the grammar, constructs a parse
%{ table, and generates the First and Follow sets. The program outputs the computed sets for each
INPUT:
/* program to recognize a c program */ non-terminal symbol in the grammar.
#include<stdio.h>
int COMMENT=0; main() { int a,b; }
int cnt=0; OUTPUT: PROGRAM:
%} #include<stdio.h> is a PREPROCESSOR DIRECTIVE #include<stdio.h>
identifier [a-zA-Z][a-zA-Z0-9]* FUNCTION main #include<ctype.h>
%% ( #include<string.h> void
#.* { printf("\n%s is a PREPROCESSOR DIRECTIVE",yytext);} ) followfirst(char, int, int);
int | float | char | double | while | for | do | BLOCK BEGINS void follow(char c);void findfirst(char, int, int);
if | int is a KEYWORD int count, n = 0; char calc_first[10][100]; char
break | a IDENTIFIER b calc_follow[10][100]; int m = 0; char
continue | IDENTIFIER production[10][10]; char f[10], first[10];
void | BLOCK ENDS int k; char
switch | ck;
case | long int e;
| struct | int main(int argc, char **argv)
const | { int jm = 0; int km = 0; int i, choice;
typedef | char c, ch; count = 8;
return | strcpy(production[0], "E=TR");
else | strcpy(production[1], "R=+TR");
goto {printf("\n\t%s is a KEYWORD",yytext);} strcpy(production[2], "R=#");
"/*" {COMMENT = 1;} strcpy(production[3], "T=FY");
"*/" {COMMENT = 0; cnt++;} strcpy(production[4], "Y=*FY");
{identifier}\( {if(!COMMENT)printf("\n\nFUNCTION\n\t%s",yytext);} strcpy(production[5], "Y=#");
\{ {if(!COMMENT) printf("\n BLOCK BEGINS");} strcpy(production[6], "F=(E)");
\} {if(!COMMENT) printf("\n BLOCK ENDS");} strcpy(production[7], "F=i");
{identifier}(\[[0-9]*\])? {if(!COMMENT) printf("\n %s IDENTIFIER",yytext);} int kay; char
\".*\" {if(!COMMENT) printf("\n\t%s is a STRING",yytext);} done[count]; int
[0-9]+ {if(!COMMENT) printf("\n\t%s is a NUMBER",yytext);} ptr = -1;
\)(\;)? {if(!COMMENT) printf("\n\t");ECHO;printf("\n");} \( for(k = 0; k < count; k++) { for(kay = 0;
ECHO; kay < 100; kay++) {
= {if(!COMMENT)printf("\n\t%s is an ASSIGNMENT OPERATOR",yytext);} calc_first[k][kay] = '!';
\<= | }
\>= | }
\< | int point1 = 0, point2, xxx;
== | for(k = 0; k < count; k++)
\> {if(!COMMENT) printf("\n\t%s is a RELATIONAL OPERATOR",yytext);} { c = production[k][0];
%% point2 = 0; xxx = 0;
int main(int argc,char **argv) for(kay = 0; kay <= ptr;
{ kay++) if(c ==
done[kay]) xxx = 1;

}
if (xxx == 1) void findfirst(char c, int q1, int q2)
continue; { int j; if(!(isupper(c))) { EXERCISE 9
findfirst(c, 0, 0); ptr += 1; first[n++] = c;
done[ptr] = c; printf("\n } Write a C program for implementation of LR parsing algorithm to accept a given input
First(%c) = { ", c); for(j = 0; j < count; j++) string.
calc_first[point1][point2++] = { if(production[j][0] == c)
c; for(i = 0 + jm; i < n; i++) { { if(production[j][2] == '#') AIM: To write a C program for implementation of LR parsing algorithm to accept a given input
int lark = 0, chk = 0; { if(production[q1][q2] == '\0') string.
for(lark = 0; lark < point2; lark++) { first[n++] = '#';
if (first[i] == calc_first[point1][lark]) else if(production[q1][q2] != '\0' DESCRIPTION: This C program implements the LR parsing algorithm to accept a given input
{ chk = 1; && (q1 != 0 || q2 != 0)) { string for a given grammar. It reads the production rules, constructs a parse table, and applies the
break; findfirst(production[q1][q2], q1, (q2+1)); } LR parsing algorithm to match the input string with the grammar. The program outputs whether
} else the input string is valid or not according to the defined grammar.
} if(chk == first[n++] = '#';
0) } PROGRAM:
{ printf("%c, ", first[i]); void followfirst(char c, int c1, int c2) #include
calc_first[point1][point2++] = first[i]; { int k; #include char
} if(!(isupper(c))) stack[30]; int
} printf("}\n"); f[m++] = c; top=-1; void
jm = n; push(char c)
else {
point1++; { top++;
int i = 0, j = 1; for(i = 0;
} printf("\n"); stack[top]=c;
i < count; i++)
printf(" \n\n"); char donee[count]; ptr = -1; } char
{ if(calc_first[i][0] == c)
for(k = 0; k < count; k++) { for(kay = 0; pop()
break; }
kay < 100; kay++) { { char c;
while(calc_first[i][j] != '!')
calc_follow[k][kay] = '!'; if(top!=-1)
{ if(calc_first[i][j] != '#') {
} { c=stack[top];
f[m++] = calc_first[i][j]; } else
} point1 = 0; int top--; return c;
{ if(production[c1][c2] == '\0') {
land = 0; } return'x'; }
follow(production[c1][0]); void printstat()
for(e = 0; e < count; e++) {
} else { { int
ck =
followfirst(production[c1][c2], c1, i;
production[e][0];
c2+1);
point2 = 0; xxx = 0; printf("\n\t\t\t $"); for(i=0;i<=top;i++)
for(kay = 0; kay <= ptr; kay++) } printf("%c",stack[i]);
if(ck == donee[kay]) } j++}}} }
xxx = 1; OUTPUT: void main()
First(E)= { (, i, } {
if (xxx == 1) First(R)= { +, #, } First(T)= int i,j,k,l;
continue; char s1[20],s2[20],ch1,ch2,ch3; clrscr();
land += 1; follow(ck); ptr += 1; { (, i, }
printf("\n\n\t\t LR PARSING"); printf("\n\t\t
donee[ptr] = ck; printf(" First(Y)= { *, #, } ENTER THE EXPRESSION");
Follow(%c) = { ", ck); scanf("%s",s1);
{ chk = 1; First(F)= { (, i, }
l=strlen(s1); j=0;
break; printf("\n\t\t $");
} Follow(E) = { $, ), }
for(i=0;i<l;i++)< span="" style="box-sizing: border-box;">
} if(chk =0) Follow(R) = { $, ), } {
{ printf("%c, ", f[i]); if(s1[i]=='i' && s1[i+1]=='d')
Follow(T) = { +, $, ), }
calc_follow[point1][point2++] = f[i]; } {
} printf(" }\n\n"); km = m; Follow(Y) = { +, $, ), }
point1++; } Follow(F) = { *, +, $, ), }
s1[i]=' '; s1[i+1]='E'; case 3:
printstat(); printf("id"); printf("Enter the expression with relational operator");
push('E'); printstat(); } scanf("%s%s%s",&id1,&op,&id2);
EXERCISE 13
else if(s1[i]=='+'||s1[i]=='-'||s1[i]=='*' ||s1[i]=='/' ||s1[i]=='d') if(((strcmp(op,"<")==0)||(strcmp(op,">")==0)||(strcmp(op,"<=")==0)||(strcmp(op,">=")==0)||(strc
{ push(s1[i]); mp(op,"==")==0)||(strcmp(op,"!=")==0))==0)
printstat(); Write a C program for generating the three-address code of a given expression/statement.
printf("Expression is error");
} else {
AIM: To write a C program for generating the three-address code of a given
} printstat(); printf("\n%d\tif %s%s%s goto %d",addr,id1,op,id2,addr+3); addr++;
expression/statement.
l=strlen(s2); printf("\n%d\t T:=0",addr);
while(l) { PROGRAM:
#include<stdio.h> addr++;
ch1=pop(); printf("\n%d\t goto %d",addr,addr+2); addr++;
if(ch1=='x') { #include<string.h>
void pm(); void printf("\n%d\t T:=1",addr);
printf("\n\t\t\t $");
plus(); void div(); } break;
break; }
int i,ch,j,l,addr=100; case 4:
if(ch1=='+'||ch1=='/'||ch1=='*'||ch1=='-')
char ex[10], exp[10] ,exp1[10],exp2[10],id1[5],op[5],id2[5]; void exit(0)}
{ ch3=pop();
main() { } void pm() {
if(ch3!='E') {
clrscr(); strrev(exp); j=l-i-1;
printf("errror");
while(1) { strncat(exp1,exp,j);
exit(); } else {
printf("\n1.assignment\n2.arithmetic\n3.relational\n4.Exit\nEnter the choice:"); strrev(exp1);
push('E');
scanf("%d",&ch); switch(ch) printf("Three address code:\ntemp=%s\ntemp1=%c%ctemp\n",exp1,exp[j+1],exp[j]);
printstat();
{ case 1: printf("\nEnter the expression with assignment } void
}
operator:"); scanf("%s",exp); l=strlen(exp); exp2[0]='\0'; div() {
}
i=0; strncat(exp1,exp,i+2);
ch2=ch1;
} getch(); while(exp[i]!='=') printf("Three address code:\ntemp=%s\ntemp1=temp%c%c\n",exp1,exp[i+2],exp[i+3]);
} { i++; } void
OUTPU } plus() {
T: strrev strncat(exp1,exp,i+2);
LR PARSING (exp1 printf("Three address code:\ntemp=%s\ntemp1=temp%c%c\n",exp1,exp[i+2],exp[i+3]); }
);
ENTER THE EXPRESSION printf("Three address code:\ntemp=%s\n%s=temp\n",exp1,exp2); break; OUTPUT:
id+id*id-id case 2: 1) 1. assignment
$ printf("\nEnter the expression with arithmetic operator:"); 2. arithmetic
$id scanf("%s",ex); strcpy(exp,ex); l=strlen(exp); 3. relational
$E exp1[0]='\0'; 4. Exit
$E+ for(i=0;i<l;i++) { Enter the choice:1
$E+id if(exp[i]=='+'||exp[i]=='-') { Enter the expression with assignment operator:
$E+E if(exp[i+2]=='/'||exp[i+2]=='*') a=b
$E+E* { Three address code:
$E+E*id pm(); temp=b
$E+E*E break; a=temp
$E+E*E- } 2)1.assignment
$E+E*E-id else{ 2.arithmetic
$E+E*E-E plus() 3.relational
$E+E*E-E ;
4.Exit
$E+E*E break;
Enter the choice:2
$E }}
Enter the expression with arithmetic operator:
$ else if(exp[i]=='/'||exp[i]=='*')
a+b-c
{ div();
Three address code:
break;
temp=a+b temp1=temp-
}}
break;

if(str[x]!='$'&& flag==0)
{
left[w++]=str[x];
EXERCISE 14 left[w]='\0';
str[x]='$';
Write a C program for implementation of a Code Generation Algorithm of a given flag=1; } x--; } }
expression/statement. void fright(int x)
{
AIM: To write a C program for implementation of a Code Generation Algorithm of a given int w=0,flag=0; x++;
expression/statement. while(x!= -1 && str[x]!= '+'&&str[x]!='*'&&str[x]!='\0'&&str[x]!='='&&str[x]!=':'&&str[x]!='-
'&&str[x]!='/')
PROGRAM: {
#include<stdio.h> if(str[x]!='$'&& flag==0)
#include<conio.h> {
#include<string.h> int right[w++]=str[x];
i=1,j=0,no=0,tmpch=90; char right[w]='\0';
str[100],left[15],right[15]; void str[x]='$'; flag=1;
findopr(); void explore(); void } x++;
fleft(int); void fright(int); struct }
exp { int pos; char op; }k[15]; }
void main()
{ OUTPUT:
printf("\t\tINTERMEDIATE CODE GENERATION\n\n");
printf("Enter the Expression :"); scanf("%s",str);
INTERMEDIATE CODE GENERATION
printf("The intermediate code:\n");
Enter the Expression :w: =a*b+c/d-e/f+g*h The
findopr(); explore(); }
intermediate code:
void findopr()
Z := c/d
{
Y := e/f
for(i=0;str[i]!='\0';i++)
X := a*b
if(str[i]==':')
W := g*h
{ k[j].pos=i;
V := X+Z
k[j++].op=':';
U := Y+W
}
T := V-U
for(i=0;str[i]!='\0';i++) if(str[i]=='/')
W:=T
{

k[j].pos=i;
k[j++].op='/'; }
for(i=0;str[i]!='\0';i++)
if(str[i]=='*') {
k[j].pos=i;
k[j++].op='*'; }
for(i=0;str[i]!='\0';i++)

void fleft(int x)
{ int
w=0,flag=0; x-
-;

while(x!= -1 &&str[x]!= '+' &&str[x]!='*'&&str[x]!='='&&str[x]!='\0'&&str[x]!='-


'&&str[x]!='/'&&str[x]!=':')
{

You might also like