[antlr-interest] Please help to left-refactor this simple ebnf grammar

Martijn Reuvers martijn.reuvers at gmail.com
Sun Sep 20 04:22:02 PDT 2009


Hello Artem,

I think the issue is with your tokens defined at top as well as not
starting your rules with a lowercase. Tokens must start with
uppercase, while rules must start with a lowercase letter. I see no
issue in your grammar besides this.
For the rules use things like:

orlist
	: list (ORKEYWORD list)*
	;

list
	: (mutliplication)+
	;
etc.

Define your tokens defined above, as real lexer tokens below:

OPENBRACKET
	:	'('
	;

CLOSEBRACKET
	:	')'
	;

etc.

You can use literals in rules and then refer to them as tokens at top
(you do it in the reverse). I prefer real lexer tokens though in
general.

Martijn

On Sun, Sep 20, 2009 at 12:45 PM, Artem Golubev
<artemkagolubev at gmail.com> wrote:
> I just can't get how I can left-refactor the following grammar. Please,
> help.
> Currently I get "(211): ebnf.g:1:8: [fatal] rule Tokens has non-LL(*)
> decision due to recursive rule invocations reachable from alts 8,9. Resolve
> by left-factoring or using syntactic predicates or using backtrack=true
> option." error with it.
> grammar ebnf;
>
> options {
> output = AST;
> language = CSharp2;
> }
>
> tokens {
> OPENBRACKET = '(';
> CLOSEBRACKET = ')';
> ONEORMOREKEYWORD= '+';
> ANYKEYWORD = '*';
> OPTIONALKEYWORD = '?';
> ORKEYWORD = '|';
> }
>
> /*------------------------------------------------------------------
> * PARSER RULES
> *------------------------------------------------------------------*/
> OrList : List (ORKEYWORD List)*;
>
> List : (Mutliplication)+;
>
> Mutliplication : Atom ( ONEORMOREKEYWORD | OPTIONALKEYWORD | ANYKEYWORD )?;
>
> Atom : IDENTIFIER | Group;
>
> Group : OPENBRACKET OrList CLOSEBRACKET;
>
> /*------------------------------------------------------------------
> * LEXER RULES
> *------------------------------------------------------------------*/
>
> IDENTIFIER : (DIGIT)+ ;
>
> WHITESPACE : ( '\t' | ' ' | '\r' | '\n' )+ { $channel = HIDDEN; } ;
>
> fragment DIGIT : '0'..'9' ;
>
>
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe:
> http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>
>


More information about the antlr-interest mailing list