Question

I'm writing a parser in PLY for a language that consists of two sublanguages: the "normal" expression language, and the language of type annotations. The problem is that they share some tokens, and that precedence differs between the two languages.

For example, in the expression language a | b, c should be equivalent to (a | b), c (and means the same as in Python), while in the type language the same should be equivalent to a | (b, c) (either type a or type b, c, that is a tuple with members of type b and type c).

The real problem is a bit more complicated than that, but it's still basically the same.

Would it be possible in PLY to temporarily change precedence? If not, would there be another solution that I can apply?

Was it helpful?

Solution

It turns out, you can use %prec fakeToken at the end of the production with different precedence, and insert facetoken in the right place in the precedence list.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top