I'm trying to evaluate and expression of the form
#SomeFunc[expr][expr]expr
expr can be either a string composed from certain characters or a function as above. So this could look something like
#SomeFunc[#SomeFunc[#SomeFunc[nm^2][nn]][nm]][n]...
The problem is that if I brake into tokens in the form of
"#"SomeFunc {yylval.fn=F_some; return FUNC;}
m|n|ms {return TEXT;}
"^" {yylval.fn=F_pow; return FUNC;}
[1-9]+ {yylval=atoi(yytext); return NUMBER;}
I have issues building a grammar if I have something like
#SomeFunc[#SomeFunc[nm^2][nn]][n]
calc:
| calc expr EOL { eval($2); }
expr: TEXT {$$= add it to ast-leaf }
| FUNC '[' expr ']' '[' expr ']' {$$= add ast(func,$3,$6) }
| expr expr {$$= add to ast('*',$1,$2 }
and I'm not quite sure if the grammar is wrong or my implementation of an AST.
I find my logic flawed because in the case of nm expr will be expr expr which will return the the value of n*m which is still nm. will this cause an infinite loop? How should i parse such an expression.
Don't throw stones. Bison newbie
Later edit I managed to clean up and test the code behind the AST and some linked lists. The only problem remains the grammar.
%union { struct ast *a; char *strval; int ival; }
%type <a> exp fact
%token <strval> ISU
%token <ival> NUMBER
%token FUNC POW
%token EOL OP CP
%%
calclist: | calclist exp EOL { printf("result >",eval($2));};
exp: fact | exp fact {$$ = newast('*', $1,$2);} ;
fact: FUNC OP exp CP OP exp CP { $$ = newast('/',$3,$6);}
| ISU POW NUMBER { $$ = newnum($1, $3);}
| ISU { $$ = newnum($1,1);};
This grammar fails for an expr like Frac[m^2][m^4] node / node K m^4 node K m^4