tags:

views:

37

answers:

3

I have been keeping the shift/reduce errors away. Now finally i think i met my match.

Int[] a
a[0] = 1

The problem is int[] is defined as

Type OptSquareBrackets

while a[0] is defined as

Var | Var '[' expr ']'

Var and Type both are defined as VAR which is any valid variable [a-zA-Z][a-zA-Z0-9_]. Apart from adding a dummy token (such as **Decl** Type OptSquareBrackets instead) is there a way to write this to not have a conflict? From this one rule i get 1 shift/reduce and 1 reduce/reduce warning.

+1  A: 

Could you define a new Token

VarLBracket [a-zA-Z][a-zA-Z0-9_]*\[

And therefore define declaration

Type | VarLBracket ']';

and define assignment target as

Var | VarLBracket expr ']';
Simeon Pilgrim
Wrong but its close to the solution. Defining a separate token for [] since everywhere else it isnt [] but [var]
acidzombie24
+1  A: 

Technically, this problem stems from trying to tie the grammar to a semantic meaning that doesn't actually differ in syntax.

ISTM that you just need a single grammar construct that describes both types and expressions. Make the distinction in code and not in the grammar, especially if there is not actually a syntactic difference. Yacc is called a compiler generator but it is not the least bit true. It just makes parsers.

Having said that, recognizing [] as a terminal symbol might be an easier way to fix the problem and get on with things. Yacc isn't very good at ambiguous grammars and it needs to make early decisions on which path to follow.

DigitalRoss
A: 

Create a Lex rule with [] since [] is only used in declaration and everywhere else would use [var]

acidzombie24