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

SS&ST Part A Lab Programs

Lab program of ss and SD

Uploaded by

shashanktpr4
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

SS&ST Part A Lab Programs

Lab program of ss and SD

Uploaded by

shashanktpr4
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

1.

a. Write a LEX program to recognize valid arithmetic expression.


Identifiers in the expression could be only integers and operators
could be + and *. Count the identifiers & operators present and
print them separately.
Program 1(a):
LEX Program
%{
#include<stdio.h>
int operator=0,identifier=0,braces=0;
%}
%%
[\+\-\*\%] { ++operator;printf("%s is a operator\n",yytext);}
[a-zA-Z]+ { ++identifier;printf("%s is identifier\n",yytext);}
[0-9]+ { ++identifier;printf("%s is digit\n",yytext);}
[(] {braces++;}
[)] {braces--;}
%%
int main()
{
printf("\n enter the expression:");
yylex();
if((operator>=1)&&(identifier==operator+1)&&(braces==0))
{
printf("\n valid expression\n");
printf("\n number of operator are:%d",operator);
printf("\n number of identifiers are :%d\n",identifier);
}
else
printf("\n invalid expression");
return 0;
}

OUTPUT:-
$vi 1a.l
$ lex 1a.l
$ cc lex.yy.c -ll

$ ./a.out
Case 1: Enter the expression:(a+b/w-2%d)
a is identifier
+ is a operator
b is identifier
/ is a operator
w is identifier
- is a operator
2 is digit
% is a operator
d is identifier
Valid Expression
Number of operators are: 4
Number of identifiers are :5

Case 2: $ ./a.out
Enter the expression:a+-b
a is identifier
+ is a operator
- is a operator
b is identifier
Invalid expression

b. Write YACC program to evaluate arithmetic expression involving operators: +, -, *, and /


LEX Program
%{
#include<stdio.h>
#include"y.tab.h"
extern int yylval;
%}
%%
[0-9]+ {yylval=atoi(yytext);return NUM; }
[\t] ;
\n return 0;
. return yytext[0];
%%

YACC Program
%{
#include<stdio.h>
#include<stdlib.h>
%}
%token NUM
%left '+' '-'
%left '*' '/'
%left '(' ')'
%%
expr: e { printf("Result:%d\n",$$); return 0;}
e:e'+'e {$$=$1+$3;}
|e'-'e {$$=$1-$3;}
|e'*'e {$$=$1*$3;}
|e'/'e {$$=$1/$3;}
|'('e')' {$$=$2;}
| NUM {$$=$1;}
;
%%
main()
{
printf("Enter the arithematic expression:\n");
yyparse();
printf("\nValid expression\n");
}
yyerror()
{
printf("\nInvalid expression\n");
exit(0);
}
OUTPUT:-
$ vi 1b.l
$ vi 1b.y
$ lex 1b.l
$ yacc -d 1b.y
$ cc y.tab.c lex.yy.c -ll
$ ./a.out
Case 1: Enter the arithematic expression:
3+3*5/2
Result: 10
Valid expression

Case 2: $ ./a.out
Enter the arithematic expression:
3+4-
Invalid expression

2.
Develop, Implement and Execute a program using YACC tool to recognize all strings
ending with b preceded by n a’s using the grammar an b (note: input n value)

LEX Program
%{
#include "y.tab.h"
%}
%%
a {return A;}
b {return B;}
[\n] return '\n';
%%
YACC Program
%{
#include<stdio.h>
#include<stdlib.h>
%}
%token A B
%%
input:s'\n' {printf("Successful Grammar\n");exit(0);}
s: A s1 B| B
s1: ; | A s1
%%
main()
{
printf("Enter A String\n");
yyparse();
}
int yyerror()
{
printf("Error \n");
exit(0);
}
Output
gedit 2.l
lex 2.l
cc lex.yy.c -ll
./a.out
case 1: enter a string
aaaaaaaaaaaaaaab
successful grammer
case 2:
enter a string
aaaaaaaaaabbb
error
3. Design, develop and implement C /JAVA program to demonstrate Shift Reduce Parsing
technique for the grammar rules: E →E+T | T, T →T*F | F, F → (E) | id and parse the

sentence: id + id * id.

#include<string.h>
#include<stdio.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()
{
printf("GRAMMAR is E->E+T|T \n T->T*F|F \n F->(E)|id \n");
printf("enter input string ");
scanf("%s",a);
c=strlen(a);
strcpy(act,"SHIFT->");
printf("stack \t input \t action");
printf("\n$\t%s\t",a);
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 ");
for(z=0; z<c; z++)
if(stk[z]=='i' && stk[z+1]=='d')
{
stk[z]='F';
stk[z+1]='\0';
printf("\n$%s\t%s$\t%sF",stk,a,ac);
j++;
}

for(z=0; z<c; z++)


if(stk[z]=='T' && stk[z+1]=='*' && stk[z+2]=='F')
{
stk[z]='T';
stk[z+1]='\0';
stk[z+2]='\0';
printf("\n$%s\t%s$\t%sT",stk,a,ac);
i=i-2;
}
else if(stk[z]=='F')
{
stk[z]='T';
printf("\n$%s\t%s$\t%sT",stk,a,ac);
}

for(z=0; z<c; z++)


if(stk[z]=='(' && stk[z+1]=='E' && stk[z+2]==')')
{
stk[z]='F';
stk[z+1]='\0';
stk[z+1]='\0';
printf("\n$%s\t%s$\t%sF",stk,a,ac);
i=i-2;
}
for(z=0;z<c;z++)
{
if(stk[z]=='E' && stk[z+1]=='+' && stk[z+2]=='T' && stk[z+3]=='*')
break;
if(stk[z]=='E' && stk[z+1]=='+' && stk[z+2] == 'T')
if(a[j+1]=='*')
break;
else
{
stk[z]='E';
stk[z+1]='\0';
stk[z+2]='\0';
printf("\n$%s\t%s$\t%sE",stk,a,ac);
i=i-2;
}
else if(stk[z]=='T')
{
stk[z]='E';
printf("\n$%s\t%s$\t%sE",stk,a,ac);
}
}}
Output : gedit 3.c
Cc 3.c
./a.out
GRAMMAR is E->E+T|T
T->T*F|F
F->(E)|id
enter input string id+id*id
stack input action
$id +id*id$ SHIFT->id
$F +id*id$ REDUCE TO F
$T +id*id$ REDUCE TO T
$E +id*id$ REDUCE TO E
$E+ id*id$ SHIFT->symbols
$E+id *id$ SHIFT->id
$E+F *id$ REDUCE TO F
$E+T *id$ REDUCE TO T
$E+T* id$ SHIFT->symbols
$E+T*id $ SHIFT->id
$E+T*F $ REDUCE TO F
$E+T $ REDUCE TO T
$E $ REDUCE TO E
4.
Design, develop and implement a C/Java program to generate the machine code using
Triples for the statement A = -B * (C +D) whose intermediate code in three-address
form:
T1 = -B
T2 = C + D
T3 = T1 + T2
A = T3
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
char tset[4][3][3]={{"-","B","?"},
{"+","C","D"},
{"*","O","1"},
{"=","A","2"},
};
int main()
{
int row,col;
printf("input statement is A:-B*(C+D)\n");
printf("intermediate code in three addresss form is:\nT1=-B\nT2=C+D\nT3=T1+T2\nA=T3\
n");
printf("triplets are\n");
for(row=0;row<4;row++)
{
if(row==0)printf("0\t-\tB\t?\n");
if(row==1)printf("1\t+\tC\tD\n");
if(row==2)printf("2\t*\tT1\tT2\n");
if(row==3)printf("3\t=\tA\tT3\n");
}
printf("machine code generated is: \n");
for(row=0;row<4;row++)
{
col=2;
if(tset[row][col][0]=='?')
{
printf("\nLD R0,%s%s",tset[row][0],tset[row][1]);
}
else
{
if(tset[row][0][0]=='+')
{
printf("\nLD R1,%s",tset[row][1]);
printf("\nLD R2,%s",tset[row][2]);
printf("\nADD R1,R1,R2");
}
else
{
if(tset[row][0][0]=='*')
{
printf("\nMUL R0,R0,R1");
}
else
{
printf("\nST %s, R0",tset[row][1]);
}
}
}
}
printf("\n");
return 0;
}
Ouput:
gedit 4.c
cc 4.c
./a.out
input statement is A:-B*(C+D)
intermediate code in three addresss form is:
T1=-B
T2=C+D
T3=T1+T2
A=T3
triplets are
0 - B ?
1 + C D
2 * T1 T2
3 = A T3
machine code generated is:

LD R0,-B
LD R1,C
LD R2,D
ADD R1,R1,R2
MUL R0,R0,R1
ST A, R0
5a.
Write a LEX program to eliminate comment lines in a C program and copy the resulting
program into a separate file.

%{
#include<stdio.h>
int a=0;
%}
%%
“/*”[^*/]*”*/” {a++;}
“//”.* {a++;}
%%
int main(int argc , char **argv)
{
FILE *f1, *f2;
if(argc>1)
{
f1=fopen(argv[1],”r”);
if(!f1)
{
printf(“file error\n”);
exit(1);
}
yyin=f1;
f2=fopen(argv[2],”w”);
if(!f2)
{
printf(“error”);
exit(1);
}
yyout=f2;
yylex();
printf(“number of comment lines=%d\n”,a);
}
return 0;
}

Output command

gedit 5a.l
lex 5a.l
cc lex.yy.c -ll
./a.out a.c b.c
5b.
Write YACC program to recognize valid identifier, operators and keywords in the given text (C
program) file.

LEX Program
%{
#include <stdio.h>
#include "y.tab.h"
extern yylval;
%}
%%
[ \t] ;
[+|-|*|/|=|<|>] {printf("operator is %s\n",yytext);return OP;}
[0-9]+ {yylval = atoi(yytext); printf("numbers is %d\n",yylval); return DIGIT;}
int|char|bool|float|void|for|do|while|if|else|return|void {printf("keyword is %s\
n",yytext);return KEY;}
[a-z A-Z0-9]+ {printf("identifier is %s\n",yytext);return ID;}
.;
%%

YACC Program
%{
#include <stdio.h>
#include <stdlib.h>
int id=0, dig=0, key=0, op=0;
%}
%token DIGIT ID KEY OP
%%
input:
DIGIT input { dig++; }
| ID input { id++; }
| KEY input { key++; }
| OP input {op++;}
| DIGIT { dig++; }
| ID { id++; }
| KEY { key++; }
| OP { op++;}
;
%%
#include <stdio.h>
extern int yylex();
extern int yyparse();
extern FILE *yyin;
main() {
FILE *myfile = fopen("sam_input.c", "r");
if (!myfile) {
printf("I can't open sam_input.c!");
return -1;
}
yyin = myfile;
do {
yyparse();
} while (!feof(yyin));
printf("numbers = %d\nKeywords = %d\nIdentifiers = %d\noperators = %d\n",
dig, key,id, op);
}
void yyerror() {
printf("EEK, parse error! Message: ");
exit(-1);
}

Ouput commands
Gedit 5b.l
Gedit 5b.y
Lex 5b.l
Yacc -d 5b.y
Cc y.tab.c lex.yy.c -ll
./a.oput a.c

You might also like