Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 #include /*#include "lex.yy.c"*/ int regs[26]; int base, debugsw;

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 stat : expr | VARIABLE '=' expr | VARIABLE '=' expr { regs[$1] = $3; } { fprintf(stderr,"the anwser is%d ", $1); } { fprintf(stderr,"the anwser is%d ", $1); } stat : expr

### ### ###

%{

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Access Patterns Database Interactions In Object Oriented Applications

Authors: Clifton Nock

1st Edition

0321555627, 978-0321555625

More Books

Students also viewed these Databases questions

Question

8. Explain the contact hypothesis.

Answered: 1 week ago

Question

2. Define the grand narrative.

Answered: 1 week ago