Now here is my lex code
%{
#include <stdio.h>
#include "y.tab.h"
extern int yylval;
int yywrap();
%}
%%
[a-zA-Z] {yylval = *yytext; return ALPHABET;}
[0-9]+ {yylval = atoi(yytext); return NUMBER;}
[0-9]+"."[0-9]* {yylval = atof(yytext); return NUMBER;}
"==" return EQ;
"<=" return LE;
">=" return GE;
"!=" return NE;
[\t] ;
\n return 0;
. return yytext[0];
%%
int yywrap()
{
return 1;
}
and here is my yacc code
%{
#include<stdio.h>
extern int yylex();
extern int yyparse();
extern FILE* yyin;
int flag = 0;
%}
%token NUMBER
%token ALPHABET
%left '+''-'
%left '*''/''%'
%left '&''|''^''~'
%right EQ LE GE NE'<''>'
%left '('')'
%left UMINUS
%left UPLUS
%start check
%%
check : E { }
E:E '+' E {$$ = $1 + $3;}
|E '-' E {$$ = $1 - $3;}
|E '&' E {$$ = $1 & $3;}
|E '|' E {$$ = $1 | $3;}
|E '^' E {$$ = $1 ^ $3;}
|'~' E {$$ = ~$2;}
|E EQ E {$$ = (EQ, $1, $3);}
|E LE E {$$ = (LE, $1, $3);}
|E GE E {$$ = (GE, $1, $3);}
|E NE E {$$ = (NE, $1, $3);}
|E '<' E {$$ = ('<', $1, $3);}
|E '>' E {$$ = ('>', $1, $3);}
|'(' E ')' {$$ = $2;}
|'-' E %prec UMINUS
{$$ = - $2;}
|'+' E %prec UPLUS
{$$ = + $2;}
|NUMBER {$$ = $1;}
|ALPHABET {$$ = $1;}
;
%%
int main(int argc, char** argv)
{
char filename[30];
char line[300];
printf("\nEnter filename\n");
scanf("%s",filename);
yyin = fopen(filename, "r");
if(NULL == yyin)
{
fprintf(stderr,"Can't read file %s\n",filename);
return 1;
}
else
{
while(fgets(line, sizeof line, yyin) != NULL)
{
printf("%s\n", line);
}
yyparse();
fclose(yyin);
printf("\nValue of yyparse : %d\n",yyparse());
}
if(flag == 0)
printf("\nBoolean Arithmetic Expression is valid\n");
return 0;
}
void yyerror()
{
printf("\nBoolean Arithmetic expression is invalid\n\n");
flag = 1;
}
This is my main part for reading text file and do some operations, so anyone can tell me this how to read multiple line in text file using Yacc. Now I put my fully Yacc code and I try to check Boolean expression is correct or not my text file expressions are : -
a-b
a+b&c
(P!=F+E-O+F-(U>Y+I<N))
(((a+B)-7+4-(c-d))+((P^q)-(L|z))+(m&n)+(O-g)
((A-2)&(B+2)|(C-4)^(D+4)+(~E))==F+(G!=)-(i<j)-(K>M)
((((a+b)-(c+d))-((E-F)+(G-H)))+((a&B)+(c|d))-((e^f)+(~g)+(i==2)-(j!=2)+(k<=8)-(l>=17.98)+(M<N)-(O>p)-((-2+4)+(6-(-5)))))
So my code check only first expression. So my problem is that how to check all expressions line by line.
Now please check where is the problem for reading text line by line and give message expression is valid or not please help. Some expressions are valid and some are invalid so please check and tell me the problem and how to correct it.