3

I have the following grammar for expressions involving binary operators (| ^ & << >> + - * /):

expression       : expression BITWISE_OR xor_expression
                 | xor_expression
xor_expression   : xor_expression BITWISE_XOR and_expression
                 | and_expression
and_expression   : and_expression BITWISE_AND shift_expression
                 | shift_expression
shift_expression : shift_expression LEFT_SHIFT arith_expression
                 | shift_expression RIGHT_SHIFT arith_expression
                 | arith_expression
arith_expression : arith_expression PLUS term
                 | arith_expression MINUS term
                 | term
term             : term TIMES factor
                 | term DIVIDE factor
                 | factor
factor           : NUMBER
                 | LPAREN expression RPAREN

This seems to work fine, but doesn't quite match my needs because it allows outer parentheses e.g. ((3 + 4) * 2).

How can I change the grammar to disallow outer parentheses, while still allowing them within expressions e.g. (3 + 4) * 2, even redundantly e.g. (3 * 4) + 2?

user200783
  • 13,722
  • 12
  • 69
  • 135

1 Answers1

2

Add this rule to your grammar:

top_level : expression BITWISE_OR xor_expression
          | xor_expression BITWISE_XOR and_expression
          | and_expression BITWISE_AND shift_expression
          | shift_expression LEFT_SHIFT arith_expression
          | shift_expression RIGHT_SHIFT arith_expression
          | arith_expression PLUS term
          | arith_expression MINUS term
          | term TIMES factor
          | term DIVIDE factor
          | NUMBER

and use top_level where you want expressions without outer parens.

Ira Baxter
  • 93,541
  • 22
  • 172
  • 341
  • The original grammar is LALR(1) - adding this rule preserves that property. However, it is possible to transform the original grammar to be LL(1) by removing recursion (e.g. `expression : expression BITWISE_OR xor_expression | xor_expression` => `expression : xor_expression expression1; expression1 -> BITWISE_OR xor_expression expression1 | empty`). Is it possible to add a `top_level` rule to this transformed grammar while keeping it LL(1)? If not, what about LL(k)? – user200783 Mar 16 '16 at 11:51