I started a project with a grammar that used % (and the word mod) for modulus operators, and now I would like to add % as a trailing unary operator to divide by 100.
A few notes, I don't work with a C-based language, I have implemented my own tokenizer/compiler using the XML output from bison. And my choice of steps are critial for my implementation.
Is there a way I can make my grammar to compile without any shift/reduce errors in a LALR(1) compiler?
Basically the following statements are all valid:
5%-> 0.055%%5-> 0.05 mod 55%%%5-> 0.0005 mod 5 etc.
I just don't know how to formulate this into my grammar:
%token S_NUM
%%
default: mod_term ;
mod_term: _mod_value
| percent_term ;
_mod_value: mod_term O_PERCENT percent_term ;
percent_term: _percent_value
| value ;
_percent_value: value O_PERCENT ;
value: S_NUM ;
%%
I also compile it using the following statement:
bison -v --report=all --warnings=no-other -Werror=conflicts-sr --xml test.y -o test.y.xml
(Where I force shift/reduce as errors because of my environment)
Any ideas? I've played around with %left and %right specifiers, but no luck.
6 % 5, it would be 6 modulus 5. If the input was8%% % 5, it would be 8 times 0.01 times 0.01 modulus 5. I'm not sure there is a way to make that unambiguous. When you get to the second%in8%% % 5, you have to look two tokens ahead to determine that there is a number after the next percent — which means your grammar is not LALR(1). Looking at the first percent, you have to look three tokens ahead to know what you're dealing with. I think you need to redesign your language. - Jonathan Leffler%as modulus and use onlymod, or I can redesign the language and implement the parser again to compensate. I was just hoping there was something easier... low hanging fruit. For now I am usingmodas modulus and%as percent, and hoping there was no implmentation using this. - Marius%works. - Jonathan Leffler