user34537
user34537

Reputation:

%prec doesn't seem to work?

Here is a sample of my y file.

| rval '<' rval
| rval '<' '<' rval
| rval '>' rval
| rval '>' '>' rval

| rval LSHIFT rval
| rval RSHIFT rval

I'd like to allow < < and > > to work like << and >>. I thought they would be difficult but found "Context-Dependent Precedence" http://www.gnu.org/software/bison/manual/html_node/Contextual-Precedence.html

However it doesnt seem to work. Writing | rval '<' '<' rval %prec LSHIFT

seems to do nothing. I tried putting it here where * = "%prec LSHIFT" except i only had one at a time

| * rval * '<' * '<' * rval  *

Every slot didnt seem to work. I didnt notice any warnings except when I had more than one %prec LSHIFT in a rule.

How do I get bison to treat > > and << as >> and <<. Note: I can't make rshift >\s*> in the lexer because that would interfere with other things

Upvotes: 0

Views: 250

Answers (1)

Chris Dodd
Chris Dodd

Reputation: 126488

You can't do this in the parser (without %glr-parser), as it would require two tokens of lookahead. The problem is that the "precedence rules" aren't really doing precedence resolution. They're just a hack to resolve shift/reduce conflicts in favor of one token over another. In this case the relevant comparison is between the token inside the left rval and the '<' or '>' token. The parser needs to compare these to determine whether to reduce whatever rule is "inside" that rval, or whether to shift. Without more lookahead it has no idea whether the '<' is followed by another '<' or something else.

Upvotes: 2

Related Questions