Question
Help with lex/yacc homework : You must fix the three problems in this program: 1) Unary minus does not work 2) parenthesis does not work
Help with lex/yacc homework :
You must fix the three problems in this program:
1) Unary minus does not work
2) parenthesis does not work ( must be able to: ( 1 +4 ) * 5 )
3) multiplication does not work
____________________________________________________________________
Your job is to
1) yacc -d fix.y
2) lex fix.l
3) gcc y.tab.c -o fix
You cannot have any warnings or errors.
____________________________________________________________________
____________________________________________________________________
I'm new to lex, if you could explain your solution it will be greatly appreciated!
____________________________________________________________________
%{
/* fix.y */
/* begin specs */ #include
void yyerror (s) char *s; { printf ("%s ", s); }
%}
%start list
%token INTEGER %token VARIABLE
%left '|' %left '&' %left '+' '-' %left '*' '/' '%' %left UMINUS
%% /* end of specs*/ /* begin rules */ list : /* empty */ | list stat ' ' | list error ' ' { yyerrok; } ; ;
stat : expr { fprintf(stderr,"is equal to: %d ", $1); } | VARIABLE '=' expr { regs[$1] = $3; } ;
expr : '(' expr ')' { $$ = $2; } | expr '-' expr { $$ = $1 - $3; } | expr '+' expr { $$ = $1 + $3;} | expr '*' expr { $$ = $1 * $3;} | expr '-' expr %prec UMINUS { $$ = -$2; } | VARIABLE { $$ = regs[$1]; fprintf(stderr,"found val of = %d ",$1); } | INTEGER {$$=$1; fprintf(stderr,"found a value: ");} ;
%% /* end of rules*/
int main() { yyparse(); } --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
/* fix.l */
### ### | VARIABLE '=' expr
#include
### ### ###
%{
int mydebug=1; #include "y.tab.h" %}
%%
[a-z] {if (mydebug) fprintf(stderr,"Letter found! "); yylval=*yytext-'a'; return(VARIABLE);} [0-9][0-9]* {if (mydebug) fprintf(stderr,"Digit found! "); yylval=atoi((const char *)yytext); return(INTEGER);} [ \t] {if (mydebug) fprintf(stderr,"Whitespace found! ");} [=\-+*/%&|] { if (mydebug) fprintf(stderr,"return a token! %c ",*yytext); return (*yytext);} { if (mydebug) fprintf(stderr,"cariage return! %c ",*yytext); return (*yytext);}
%%
int yywrap(void) { return 1;}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- /* makefile: */
all: fix
fix: fix.l fix.y
lex fix.l
yacc -d fix.y
gcc lex.yy.c y.tab.c -o fix
clean:
rm -f fix
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started