Question: 1 . ) Add a left - associative # operator, at lower precedence than any of the others. This is an imaginary new perator that

1.) Add a left-associative # operator, at lower precedence than any of the others.
This is an imaginary new perator that adds the second number+1.
2.) Then add a right-associative < operator, at higher precedence than any
of the others except () and -. This operator is also imaginary, but it is
the equivelent of the ^. I.e,3^4=3<4= powf(3,4)
Note: to do < use powf. You will need to include:
#include
Note: These operators must come last in your .l file. I.e:
[-+()*/#<]{return yytext[0];}
Build of off this code :
%{
#include
#include
extern int yylex();
void yyerror(char *msg);
%}
%union {
float f;
}
%token NUM
%type E T F
%%
S : E {printf("%f
", $1);}
;
E : E '+' T {$$ = $1+ $3; printf("%f +%f
", $1, $3);}
| E '-' T {$$ = $1- $3; printf("%f -%f
", $1, $3);}
| T {$$ = $1; printf("%f
", $1);}
;
T : T '*' F {$$ = $1* $3; printf("%f *%f
", $1, $3);}
| T '/' F {$$ = $1/ $3; printf("%f /%f
", $1, $3);}
| F {$$ = $1; printf("%f
", $1);}
;
F : '(' E ')'{$$ = $2; printf("(%f)
", $2);}
|'-' F {$$ =-$2; printf("%f
",-$2);}
| NUM {$$ = $1; printf("%f
", $1);}
;
%%
void yyerror(char *msg){
fprintf(stderr,"%s
", msg);
exit(1);
}
int main(){
return yyparse();
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!