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 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 #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;}

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- /* 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

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

Database Processing Fundamentals Design

Authors: Marion Donnie Dutton Don F. Seaman

14th Edition Globel Edition

1292107634, 978-1292107639

More Books

Students also viewed these Databases questions