Reputation: 61
I tried everything and I kept getting the following errors:
(line)20:31: error: request for member ‘val’ in something not a structure or union
(line)22:38: error: request for member ‘val’ in something not a structure or union
(line)27:108: error: request for member ‘val’ in something not a structure or union
%{
#include "y.tab.h"
%}
%option noyywrap
%option yylineno
%%
0|[1-9][0-9]* {yylval.val=atoi(yytext); return NUM;}
\*|\+ {yylval.val=yytext[0]=='+'?0:1; return OP;}
\(|\) return yytext[0];
[ \t\n] ;
. yyerror("caracter invalido");
%%
And the yacc:
%{
#include<stdio.h>
#include<stdlib.h>
extern int yylineno;
%}
%union {struct nodo{int val; struct nodo *next} *p; int val;}
%start lexp
%token<val> OP NUM
%type<p> larg arg
%type<val> exp
%%
lexp : lexp exp
|
;
exp : '(' OP larg ')' {struct nodo *p=$3->next;int val=$3->val;
while(p){
val=$2.val?(val+p->val):(val*p->val);
p=p->next;}
printf("R:%d\n", val); $$.val=val;}
;
larg : arg larg {$$=$1; $1->next=$2;}
| arg arg {$$=$1; $1->next=$2;}
;
arg : NUM {$$= (struct nodo *) malloc(sizeof(struct nodo)); $$->next=0; $$->val=$1.val;}
| exp {$$= (struct nodo *) malloc(sizeof(struct nodo)); $$->next=0; $$->val=$1.val;}
;
%%
int yyerror(char *s){fprintf(stderr, "linha %d: %s\n", yylineno,s); return 0;}
int main(){
yyparse();
return 0;
}
Upvotes: 2
Views: 726
Reputation:
Did you try removing the .val
from things like $2.val
? Because of your %token
and %type
lines and the <tag>
construct, the particular field of the union is already determined. In other words, you're doing things like (yylval.val).val
in the generated C code.
Upvotes: 1