Lex Yacc Code To Find Declared Initialised Used Variables
Lex Yacc Code To Find Declared Initialised Used Variables
15.2.17
AIM:
Write a lex and yacc program to perform syntax analysis and do the following:
Determine the variables that are declared and not initialized
Determine the variables that are declared but not used
CODE:
LEX :
%{
#include "y.tab.h"
extern int yylval;
%}
%%
(int|float|char) {return BUILTIN;}
"," {return COMMA;}
";" {return SC;}
"=" {return EQ;}
[0-9]+ {return DIGIT;}
[a-zA-Z] {yylval=yytext[0]; return ID;}
("+"|"-"|"/") {return OPR;}
"\n" {return NL;}
%%
YACC:
%{
#include<stdio.h>
extern FILE *yyin;
char ni[20];
char nu[20];
int i=0,j=0,x=0,y=0;
%}
%%
void yyerror() {}
int yywrap() {return 1;}
void main(int argc, char *argv[])
{
yyin=fopen("ex1.txt","r");
yyparse();
printf("not initialised");
for(x=0;x<i;x++)
{
printf("%c\n",ni[x]);
}
printf("not used");
for(y=0;y<j;y++)
{
printf("%c\n",nu[y]);
}
}
INPUT FILE:
int a,x;
float y;
int b=10;
int c=5;
float e=5;
float m=10;
a=b+c;
x=a+m;
OUTPUT:
RESULT:
Thus the lex and yacc program to determine declared but uninitialized and unused
variables were verified successfully.