Question
Help with lex/yacc homework : You must fix the three problems in this program: 1) Unary minus does not work (it is not working at
Help with lex/yacc homework :
You must fix the three problems in this program:
1) Unary minus does not work (it is not working at all)
2) parenthesis does not work ( must be able to: "( 1 +4 ) * 5" and get the right answer "25" currently it is answering this as "21" )
3) multiplication does not work (multiplication is working)
____________________________________________________________________
CODE GIVEN:
%{
/* 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;}
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