0% found this document useful (0 votes)
10 views31 pages

CD (Aicte 2020-2021)

Uploaded by

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

CD (Aicte 2020-2021)

Uploaded by

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

INDEX

Sr.
Practical Name Page No
No

1 Lex program to count the number of words, characters, blank spaces


and lines

2 LEX program to identify REAL PRECISION of the given number

3 Elimination of Left Recursion in a grammar.

4 Program to Implement Recursive Descent Parsing

5 Program to Implement Predictive Parsing

6 Program to Implement LL1 Parsing

7 Program to Implement SLR Parsing

8 YACC Program of an advanced desk calculator

9 Program to Implement 3 Address Code

10 Program to Implement Code Optimization using Constant Folding

Subject Teacher HOD Principal


Practical No1
1. Lex program to count the number of words, characters, blank spaces and lines
Procedure:

1. Create a lex specification file to recognize words, characters, blank spaces and lines
2. Compile it by using LEX compiler to get ‘C’ file

3. Now compile the c file using C compiler to get executable file

4. Run the executable file to get the desired output by providing necessary input

Program:

%{

int c=0,w=0,l=0,s=0;

%}

%%

[\n] l++;

[' '\n\t] s++;

[^' '\t\n]+ w++; c+=yyleng;

%%

int main(int argc, char *argv[])

if(argc==2)

yyin=fopen(argv[1],"r");

yylex();

printf("\nNUMBER OF SPACES = %d",s);

printf("\nCHARACTER=%d",c); printf("\

nLINES=%d",l);
printf("\nWORD=%d\n",w);

else
printf("ERROR");
}

Input File:
Hello how are you

Output:
lex filename.l
cc lex.yy.c -ll
./a.out in.txt
Practical No 2
2.LEX program to identify REAL PRECISION of the given
number
Procedure:
1. Create a lex specification file to recognize single line and multiple
comment statements
2. Compile it by using LEX compiler to get ‘C’ file
3. Now compile the c file using C compiler to get executable file
4. Run the executable file to get the desired output by providing necessary
input
Program:
%{
/*Program to identify a integer/float precision*/
%}
integer ([0-9]+)
float ([0-9]+\.[0-9]+)|([+|-]?[0-9]+\.[0-9]*[e|E][+|-][0-9]*)

%%
{integer}
printf("\n %s is an integer.\n",yytext);
{float} printf("\n %s is a floating number.\n",yytext);
%%

main()
{
yylex();
}
int yywrap()
{
return 1;
}

Output:
lex filename.l
cc lex.yy.c -ll
./a.out
*Pass the number

2
2 is an integer

2.3
2.3 is a floating number
Practical No 3
3. Elimination of Left Recursion in a grammar.
#include<stdio.h>
#include<string.h>
void main()
{
char input[100], l[50],r[50],temp[10],tempprod[20],productions[25][50];
int i=0,j=0,flag=0,consumed=0;
printf(“Enter the Productions:”);
scanf(“ %ls->%s”, l, r);
printf(“ %s”, r);
while(sscanf(r+consumed, “ % [^l] s”, temp) == 1 &&consumed<=strlen(r))
{
if(temp[0] == l[0])
{
flag = 1;
sprintf(productions[i++], “%s->%s%s ‘\0”, l,temp+1,1);
}
else
sprintf(productions[i++], “%s->%s%s ‘\0”,l, temp,1);
consumed += strlen(temp)+1;
}
if(flag==1)
{
sprintf(productions[i++], “%s->€ \0”, 1);
printf(“the productions after eliminating left recursion are:\n”);
for(j=0;j<i;j++)
printf(“%s \n “, productions[j]);
}
else
printf(“ The Given Grammar has no Left Recursion”);
}
OUTPUT:
Enter the Productions:
E->E+T
The productions after eliminating Left Recursion are:E->+TE’
E->
Enter the Productions:
T->T*F
The productions after eliminating Left Recursion are: T-> *FT’
T->
Enter the Productions:
F->id
The Given Grammar has no Left Recursion
Practical No 4
4. /* Program to Implement Recursive Descent Parsing */

#include<stdio.h>
//#include<conio.h>
#include<string.h>
char input[100];
int i,l;
int main()
{
printf("recursive decent parsing for the grammar");
printf("\n E->TEP|\nEP->+TEP|@|\nT->FTP|\nTP->*FTP|@|\nF-
>(E)|ID\n");
printf("enter the string to check:");
scanf("%s",input);
if(E()){
if(input[i]=='$')
printf("\n string is accepted\n");
else
printf("\n string is not accepted\n");
}
}
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(1);
}
}
T(){
if(F()){
if(TP())
return(1);
else
return(0);
}
else
return(0);
printf("String is not accpeted\n");
}
TP(){
if(input[i]=='*'){
i++;
if(F()){
if(TP())
return(1);
else
return(0);
}
else
return(0);
printf("The string is not accepted\n");
}
else
return(1);
}
F(){
if(input[i]=='('){
i++;
if(E()){
if(input[i]==')'){
i++;
return(1);
}
else
{
return(0);
printf("String is not accepted\n");
}
}
else
return(0);
}
else if(input[i]>='a'&&input[i]<='z'||
input[i]>='A'&&input[i]<='Z')
{ i+
+;
return(1);
}
else
return(0);
}

OUTPUT:
$ cc rdp.c
$ ./a.out
recursive decent parsing for the grammar
E->TEP|
EP->+TEP|@|
T->FTP|
TP->*FTP|@|
F->(E)|ID
enter the string to check:(i+i)*i

string is accepted
Practical No 5
5. /* Program to Implement Predictive Parsing */
#include<stdio.h>
#include<string.h>
int spt=0,ipt=0;
char s[20],ip[15];
char *m[5][6]={{“TG”,”\0”,”\0”,”TG”,”\0”,”\0”},
{“\0”,”+TG”,”\0”,”\0”,”e”,”e”},
{“FH”,”\0”,”\0”,”FH”,”\0”,”\0”},
{“\0”,”e”,”*FH”,”\0”,”e”,”e”},
{“i”,”\0”,”\0”,”(E)”,”\0”,”\0”}};
char nt[5]={'E','G','T','H','F'};
char t[6]={'i','+','*','(',')','$'};
int nti(char c)
{
int i;
for(i=0;i<5;i++){
if(nt[i]==c)
return(i);
}
return(6);
}
int ti(char c)
{
int i;
for(i=0;i<6;i++)
{
if(t[i]==c)
return(i);
}
return(7);
}
main()
{
char prod[4],temp[4];
int l,k,j;
printf("enter input string:");
scanf("%s",ip);
strcat(ip,"$");
s[0]='$';
s[1]='E';
s[2]='\0';
spt=1;
while(1)
{
if(ip[ipt]=='$'&&s[spt]=='$')
break; if(ti(s[spt])<5||
s[spt]=='$')
{
if(s[spt]==ip[ipt])
{
spt--;
ipt++;
}
else
error();
}
else if(nti(s[spt]<6)){
strcpy(prod,m[nti(s[spt])][ti(ip[ipt])]);
if(prod=='\0')
error();
l=strlen(prod);
for(k=l-1,j=0;k>=0&&j<=l;k--,j++)
temp[j]=prod[k];
for(k=0;k<l;k++)
prod[k]=temp[k];
s[spt--]='\0';
strcat(s,prod);
spt=spt+l;
if(s[spt]=='e')
s[spt--]='\0';
}
else
error();
}
if(s[spt]=='$'&&ip[ipt]=='$')
printf("\n input is parsed\n");
else
error();
return 0;
}
error()
{
printf("input is not parsed\n");
exit(1);
return 0;
}

OUTPUT:
$ cc preparsing.c
$ ./a.out
enter input string:i+i*i$

input is parsed
Practical No 6
6. /* Program to Implement LL1 Parsing */
#include<stdio.h>
#include<string.h>
char s[30],stack[20];
int main()
{
char m[5][6][3]={{"tb"," "," ","tb"," "," "},
{" ","+tb"," "," ","n","n"},
{"fc"," "," ","fc"," "," ",},
{" ","n","*fc"," ","n","n"},
{"d"," "," ","(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("\n stack 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 'd':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]=='$')
{
printf("\n error\n");
return(0);
}
else if(m[str1][str2][0]='n')
i--;
else if(m[str1][str2][0]='i')
stack[i]='d';
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:
$ cc ll1parsing.c
$ ./a.out

enter the input string:i+i*i

stack input

+i*i$
i*i$
*i$
i$
$

SUCCESS
Practical No 7
7. /* Program to Implement SLR Parsing */
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int axn[][6][2]={
{{100,5},{-1,-1},{-1,-1},{100,4},{-1,-1},{-1,-1}},
{{-1,-1},{100,6},{-1,-1},{-1,-1},{-1,-1},{102,102}},
{{-1,-1},{101,2},{100,7},{-1,-1},{101,2},{101,2}},
{{-1,-1},{101,4},{101,4},{-1,-1},{101,4},{101,4}},
{{100,5},{-1,-1},{-1,-1},{100,4},{-1,-1},{-1,-1}},
{{-1,-1},{101,6},{101,6},{-1,-1},{101,6},{101,6}},
{{100,5},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1}},
{{100,5},{-1 -1},{-1,-1},{100,4},{-1,-1},{-1,-1}},
{{-1,-1},{100,6},{-1,-1},{-1,-1},{100,11},{-1,-1}},
{{-1,-1},{101,1},{100,7},{-1,-1},{101,1},{101,1}},
{{-1,-1},{101,3},{101,3},{-1,-1},{101,3},{101,3}},
{{-1,-1},{101,5},{101,5},{-1,-1},{101,5},{101,5}},
};

int gotot[12][3]={1,2,3,-1,-1,-1-1,-1,-1,-1,-1,-1,8,2,3,-1,-1,-1,-1,9,3,-1,-1,10,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};
int a[10];
char b[10];
int top=-1,btop=-1,i;
void push(int k)
{
if(top<9)
a[++top]=k;
}

void pushb(char k)
{
if(btop<9)
b[++btop]=k;
}

char TOS()
{
return a[top];}
void pop()
{
if(top>=0)
top--;
}

void popb()
{
if(btop>=10)
b[btop--]='\0';
}
void display()
{
for(i=0;i<top;i++) printf("%d
%c",a[i],b[i]);
}
void display1(char p[],int m)
{
int l;
printf("\t\t");
for(l=m;p[l]!='\0';l++)
printf("%c",p[l]);
printf("\n");
}
void error()
{
printf("syntax error");
}
void reduce(int p)
{
int k,ad;
char src,*dest;
switch(p)
{
case 1:dest="E+T";
src='E';
break;
case 2:dest="T";
src='E';
break;

case 3:dest="T*F";
src='T';
break;
case 4:dest="F";
src='T';
break;
case 5:dest="(E)";
src='F';
break;
case 6:dest="i";
src='F';
break;
default :dest="\0";
src='\0';
break;
}
for(k=0;k<strlen(dest);k++)
{
pop();
popb();
}
pushb(src);
switch(src)
{
case 'E':ad=0;
break;
case 'T':ad=1;
break;
case 'F':ad=2;
break;
default:ad=-1;
break;
}
push(gotot[TOS()][ad]);
}

int main()
{
int j,st,ic;
char ip[20]="\0",an;
printf("enter any string");
scanf("%s",ip);
push(0);
display();
printf("\t%s\n",ip);
for(j=0;ip[j]!='\0';)
{ st=TOS
();
an=ip[j];
if(an>='a'&&an<='z') ic=0;
else if(an=='+') ic=1;
else if(an=='*') ic=2;
else if(an=='(') ic=3;
else if(an==')') ic=4;
else if(an=='$') ic=5;
else
{
error();
break;
}

if(axn[st][ic][0]==100)
{
pushb(an);
push(axn[st][ic][1]);
display();
j++;
display1(ip,j);
}
if(axn[st][ic][0]==101)
{
reduce(axn[st][ic][1]);
display();
display1(ip,j);
}
if(axn[st][ic][1]==102)
{
printf("given string is accepted");
break;
}
}
return(0);
}

OUTPUT:
$ cc slr.c
$ ./a.out
enter any stringi+i*i
i+i*i
0i +i*i
0i5 +i*i
0i53 +i*i
Practical No 8

8. /* YACC Program of an advanced desk calculator */

%{
#include<ctype.h>
#include<stdio.h>
#define YYSTYPE double
%}
%token NUMBER
%left'+''-'
%left'*''/'
%right UMINUS
%%
lines:lines expr'\n'
{
printf("%g\n",$2);}
|lines'\n'
|/*E*/
;
expr:expr'+'expr{$$=$1+$3;}
|expr'-'expr{$$=$1-$3;}
|expr'*'expr{$$=$1*$3;}
|expr'/'expr{$$=$1/$3;}
|'('expr')'{$$=$2;}
|'-'expr%prec UMINUS{$$=-$2;}
|NUMBER
;
%%
yylex()
{
int c;
while((c=getchar())==' ');
if((c=='.')||(isdigit(c))){
ungetc(c,stdin);
scanf("%lf",&yylval);
return NUMBER;
}
return c;
}
int main()
{
yyparse();
return 1;
}
int yyerror()
{
return 1;
}
int yywrap()
{
return 1;
}
OUTPUT:
$ yacc calc.y
$ gcc -o calc y.tab.c
$ ./calc

1+2/3*6
5
Practical No 9
9. /* Program to Implement 3 Address Code */

#include<stdio.h>
#include<string.h>
void pm();
void plus();
void div();
int i,ch,j,l;
char ex[10],ex1[10],exp1[10],ex2[10];
main()
{
while(1)
{
printf("\n 1.Assignment\n 2.Arithmatic\n 3.exit\n ENTER THE
CHOICE:");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\n enter the expression with assignment operator:");
scanf("%s",ex1);
l=strlen(ex1);
ex2[0]='\0';
i=0;
while(ex1[i]!='=')
{ i+
+;
}
strncat(ex2,ex1,i);
strrev(ex1);
exp1[0]='\0';
strncat(exp1,ex1,l-(i+1));
strrev(exp1);
printf("3 address code:\n temp=%s \n %s=temp\n",exp1,ex2);
break;
case 2:printf("\n enter the expression with arithmatic operator:");
scanf("%s",ex);
strcpy(ex1,ex);
l=strlen(ex1);
exp1[0]='\0';
for(i=0;i<l;i++)
{
if(ex1[i]=='+'||ex1[i]=='-')
{
if(ex1[i+2]=='/'||ex1[i+2]=='*')
{
pm();
break;
}
else
{
plus();
break;
}
}
else if(ex1[i]=='/'||ex1[i]=='*')
{
div();
break;
}
}
break;
}
}
break;
case 3:exit(0);
}
}
}
void pm()
{
strrev(exp1);
j=l-i-1;
strncat(exp1,ex1,j);
strrev(exp1);
printf("3 address code:\n temp=%s\n temp1=%c%c
temp\n",exp1,ex1[j+2],ex1[j]);
}

void div()
{
strncat(exp1,ex1,i+2);
printf("3 address code:\n temp=%s\n temp1=temp%c%c\
n",exp1,ex1[l+2],ex1[i+3]);
}
void plus()
{
strncat(exp1,ex1,i+2);
printf("3 address code:\n temp=%s\n temp1=temp%c%c\
n",exp1,ex1[l+2],ex1[i+3]);
}
OUTPUT:
1. Assignment
2.Arithmetic
3.Exit
Enter the choice:1
Enter the exp with assignment operator:a=b
3 address code:
temp=b
a=temp
1.Assignment
2.Arithmetic
3.Exit
Enter the choice:2
Enter the exp with arithmetic operator:a*b+c
3 address code:
temp=a*b
temp1=temp+c
1.Assignment
2.Arithmetic
3.Exit
Enter the choice:3
Practical No 10
10. /* Program to Implement Code Optimization using Constant Folding */

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
struct ConstFold {
char new_str[10];
char str[10];
} Opt_Data[20];

void ReadInput(char Buffer[], FILE *Out_file);


int Gen_token(char str[], char Tokens[][10]);

int New_Index = 0;

int main() {
FILE *In_file, *Out_file;
char Buffer[100], ch;
int i = 0;
In_file = fopen("code.txt", "r");
Out_file = fopen("output.txt", "w");
while(1) {
ch = fgetc(In_file);
i = 0;
while(1) {
if(ch == '\n') break;
Buffer[i++] = ch;
ch = fgetc(In_file);
if(ch == EOF) break;
}
if(ch == EOF) break;
Buffer[i] = '\0';
ReadInput(Buffer, Out_file);
}
return 0;
}

void ReadInput(char Buffer[], FILE *Out_file) {


char temp[100], Token[10][10];
int n, i, j, flag = 0;
strcpy(temp, Buffer);
n = Gen_token(temp, Token);
for(i=0; i<n; i++) { if(!
strcmp(Token[i], "=")) {
if(isdigit(Token[i+1][0]) || Token[i+1][0] == '.')
{
flag = 1;
strcpy(Opt_Data[New_Index].new_str, Token[i-1]);
strcpy(Opt_Data[New_Index++].str, Token[i+1]);
}
}
}
if(!flag) {
for(i=0; i<New_Index; i++) {
for(j=0; j<n; j++) {
if(!strcmp(Opt_Data[i].new_str, Token[j]))
strcpy(Token[j], Opt_Data[i].str);
}
}
}

fflush(Out_file);
strcpy(temp, "");
for(i=0; i<n; i++) {
strcat(temp, Token[i]);
if(Token[i+1][0]!=',' || Token[i+1][0]!=';')
strcat(temp, "");
}
strcat(temp, "\n\0");
fwrite(&temp, strlen(temp), 1, Out_file);
}

int Gen_token(char str[],char Token[][10])


{
int i=0, j=0, k=0;
while(str[k]!= '\0') {
j=0;
while(str[k]==' ' || str[k] == '\t')
k++;
while(str[k]!= ' '&&str[k]!='\0'&&str[k]!='='&&str[k]!= '/'&&str[k]!='+
'&&str[k]!='-'&&str[k]!='*'&&str[k]!=','&&str[k]!= ';')

Token[i][j++] = str[k++];
Token[i++][j] = '\0';
if(str[k] == '=' || str[k] == '/' || str[k] == '+' || str[k] == '-' ||
str[k] == '*' || str[k] == ',' || str[k] == ';')
{
Token[i][0] = str[k++];
Token[i++][1] = '\0';
}
if(str[k] == '\0')
break;
}
return i;
}

Input.txt

#include<stdio.h>
main()
{
float pi=3.14,r,a;
a=pi*r*r;
printf("a=%f",a);
return 0;
}

OUTPUT:
$ cc codeop.c
$ ./a.out
$ vi output.txt
Output.txt
#include<stdio.h>
main()
{
floatpi=3.14,r,a;
a=3.14*r*r;
printf("a=%f",a);
return0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy