SS&ST Part A Lab Programs
SS&ST Part A Lab Programs
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
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++;
}
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