I'm trying to implement a grammar that allows multiplication by juxtaposition. This is for parsing polynomial inputs for a CAS.
It works quite well, except few edge cases, as far as I'm aware of. There are two problems I have identified:
- Conflict with other rules, e.g.,
a^2 b
is (erroneously) parsed as(^ a (* 2 b))
, not as(* (^ a 2) b)
. - yacc(bison) reports
28 shift/reduce conflicts
and8 reduce/reduce conflicts
.
I'm pretty sure properly resolving the first issue will resolve the second as well, but so far I haven't been successful.
The following is the gist of the grammar that I'm working with:
%start prgm
%union {
double num;
char *var;
ASTNode *node;
}
%token <num> NUM
%token <var> VAR
%type <node> expr
%left '+' '-'
%left '*' '/'
%right '^'
%%
prgm: // nothing
| prgm '\n'
| prgm expr '\n'
;
expr: NUM
| VAR
| expr '+' expr
| expr '-' expr
| expr '*' expr
| expr '/' expr
| expr '^' expr
| expr expr %prec '*'
| '-' expr
| '(' expr ')'
;
%%
Removing the rule for juxtaposition (expr expr %prec '*'
) resolves the shift/reduce & reduce/reduce warnings.
Note that ab
in my grammar should mean (* a b)
.
Multi-character variables should be preceded by a quote('
); this is already handled fine in the lex
file.
The lexer ignores spaces(
) and tabs(\t
) entirely.
I'm aware of this question, but the use of juxtaposition here does not seem to indicate multiplication.
Any comments or help would be greatly appreciated!
P.S. If it helps, this is the link to the entire project.