0

I am writing a grammar where I'd like certain functions to be only at the top-level of the expression while arithmetic operations can be anywhere. E.g. 4 + (5 * 9) is correct, FUNC(2 + 4) * 8 - FUNC(4) is also correct, but FUNC(2 * FUNC(8)) is incorrect cause, nested FUNC is not top-level. Here's my grammar:

start : F

F : F + F
F : F - F
F : F * F
F : F / F
F : ( F )
F : FUNC( E )
F : E

E : E + E
E : E - E
E : E * E
E : E / E
E : ( E )
E : num

FUNC : MAX
FUNC : MIN

I am using YACC, and I'm getting shift/reduce conflict on above grammar. Seems like grammar has no ambiguities but still something is not right, since there are conflicts. I am wondering what would be the correct way to write such grammar?

Milos Ljumovic
  • 403
  • 4
  • 16
  • Show everything, including precedence declarations. As written, the grammar is certainly ambiguous – rici Apr 19 '21 at 23:12
  • Also, bison style is to write non-terminals in `lower_case` and reserve `UPPER_CASE` for tokens. If the token is a keyword, it's even better to use quotes (`"max"`) or, for single-character operators, single quotes: `'+'`. The double-quoted strings must be declared in a `%token` declaration if you want a lexer to be able to return them. – rici Apr 19 '21 at 23:14

0 Answers0