This project has moved and is read-only. For the latest updates, please go here.

Differences between infix and prefix + or -

Dec 6, 2012 at 4:54 PM
Edited Dec 6, 2012 at 4:54 PM

I'm writing a parser for a language based off it's spec. In the spec it states the precedence is as follows:

1. **

2. prefix +, -

3. *, /, div, mod

4. infix +,-

5. <, >, =, <=, >=, not=

6. not

7. and

8. or

Do I need to explicitly declare that the prefix + and - have higher precedence than *, while infix has lower than *, and how would I do that? I can't seem to come up with any instances where the grammar could be ambiguous about that (it should already be clear from context whether something is infix or prefix). The operators combine to very simple expressions, that conform the same way most languages do (it's probably most similar to python), with the only oddball in the expressions is that ** is left associative (usually exponents are right associative)

Would the following be correct for this language:


RegisterOperators(80, Associativity.Left, "**");
RegisterOperators(60, "*","/","div","mod");
RegisterOperators(50, "+", "-");
RegisterOperators(40, "<", ">", "=", "<=", ">=", "not=");
RegisterOperators(30, "not");
RegisterOperators(20, "and");
RegisterOperators(10, "or");
Dec 6, 2012 at 5:31 PM

There's a hint for this variable precedence, use Grammar.ImplyPrecedenceHere(precValue) (smth like this, do not remember name exactly), use it before the operator symbol in grammar rules. Try this


Dec 6, 2012 at 6:13 PM
Edited Dec 6, 2012 at 6:14 PM

Non-terminals can be set precedence, but I wasn't sure if that was what would work.


It also applies that to all the infix operators, unless I make a new non-terminal just for + and - infix.