On Wed, Apr 21, 2010 at 2:41 AM, Andy Hull <[email protected]> wrote:
> Wow, thanks for the article. I was able to redefine the language to avoid
> the problem in order to keep the parser as simple as possible (now using
> "to" instead of "..." ).
>
> My parser needs to be able to handle nested array expressions like so
>
> {1,2,{5 to 10}, {3,6,9}, 4}
>
> I have the following grammar:
>
> arrayExpression
> : LEFT_BRACKET! arrayInitializer? RIGHT_BRACKET!;
> arrayInitializer
> : (e+=expression (',' e+=expression)*)+ -> ^(ELEMENTLIST $e*)
> | expression AUTO expression -> ^(AUTO expression expression)
> ;
>
> expression
> : arrayExpression
> /* | other types of expression */
> ;
>
> with the expected non-LL(*) grammar because "arrayInitializer" depends on
> the recursive rule expression. Setting backtrack to true doesn't resolve
> this as I expected.
>
> x={1,2,3,4};
>
> yields the correct tree but...
>
> x={1 to 3};
>
> yields the error:
>
> BR.recoverFromMismatchedToken
> line 1:5 mismatched input 'to' expecting RIGHT_BRACKET
>
> arrayInitializer behaves as expected when it contains only a single subrule
> (either the element list or the range initializer).
>
> Is backtracking the right solution to the non-determinism? I am doing
> something wrong?
>
How about something like this:
grammar Test;
parse
: array ';' EOF
;
array
: '{' (arrayAtom (',' arrayAtom)*)? '}'
;
arrayAtom
: Number
| array
| range
;
range
: Number 'to' Number
;
Number
: '0'..'9'+
;
Space
: (' ' | '\t' | '\r' | '\n') {skip();}
;
Regards,
Bart.
List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe:
http://www.antlr.org/mailman/options/antlr-interest/your-email-address
--
You received this message because you are subscribed to the Google Groups
"il-antlr-interest" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/il-antlr-interest?hl=en.