Saturday, February 12, 2011

Javacc LOOKAHEAD - Lvalue in MiniJava

Sometimes you need to break a choice conflict out into a new function in order to
eliminate the choice conflict.

Choice conflict in [...] construct at line 298, column 5.
Expansion nested within construct and expansion following construct
have common prefixes, one of which is: "."
Consider using a lookahead of 2 or more for nested expansion.

This Grammar:
Factor -> ('-'|'!') Factor
| Lvalue ['.' "length" '(' ')' | '(' [Args] ')']
| '(' Expr ')'
| Literal

Lvalue -> ["this" '.'] {'[' Expr ']' | '.' }


Had to be expanded to :

Exp Factor(): {}
{
("-"|"!") Factor()
| Lvalue() ["." "length" "(" ")" | "(" [Args()] ")"]
| "(" Expr() ")"
| Literal()
}


//Lvalue -> ["this" '.'] {LvalueLower}
Exp Lvalue(): {}
{
["this" "."]
[ LOOKAHEAD(2) LvalueLower()]
}

// LvalueLower -> '[' Expr ']' | '.'
void LvalueLower():{}
{
"." | "[" Expr() "]"
}

https://bitbucket.org/aaron_skomra/minijava2/src/af557e14135e/proj3/parser/miniParser.jj