[antlr-interest] ANTLRWorks 1.4.2: Simple grammar doesn't work

Jim Idle jimi at temporal-wave.com
Tue Jul 19 09:24:28 PDT 2011


Your lexer rules are fragments only, so there are no real lexer rules to
match anything, then you try and construct parser rules from the fragment
rules.

Hence ANTLRWorks has no idea what you are doing because your grammar is so
broken. It also sounds like you were debugging a prior version, then
changed to this version (which will hang as the lexer consumes nothing),
then after a restart, managed to rebuild the whole thing and saw that the
generated code points out that you have no visible lexer rules. So, it is
not the tool, but the workman. Make sure however, to regenerate the whole
of your grammar for debug as ANTLRWorks can sometimes miss that the
compilation failed and try to run something that was not compiled.

The fragment rules are goof for readability in complex formulations, but
in simple ranges, you are creating lots of method calls and hoping that
the JIT will inline them. It is just as simple to do this:

ID : ('a'..'z' | 'A'..'Z') ('a'..'z' | 'A'..'Z' | '0'..'9')* ;


Which is what you are trying to do. Remember that Lexer rules start with
an UPPER case letter and parser rules with a lower case letter (ironic
given your rules right?); and that fragment rules are not visible to the
parser and do not create tokens.

Perhaps you should run through the 10 minute tutorials and perhaps load an
existing working grammar in to ANTLRWorks before attempting your own.
Also, always compare what you are trying to achieve with an existing
grammar, and you may find you can steal the technique.

Jim


> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of Udo Weik
> Sent: Tuesday, July 19, 2011 8:40 AM
> To: antlr-interest at antlr.org
> Subject: [antlr-interest] ANTLRWorks 1.4.2: Simple grammar doesn't work
>
> Hello,
>
> what's wrong with the following grammar, tested with: abcd
>
>
> grammar Problem1b ;
>
> fragment
> Cl :  'a'..'z' ;
> fragment
> Cu :  'A'..'Z' ;
> fragment
> INT : '0'..'9' ;
>
> id_Chars :               Cl | Cu ;
> id_NumbersChars  : INT | Cl | Cu ;
>
> test : id_Chars ( id_NumbersChars )* ;
>
>
> My main problem is that every time I'm working with ANTLRWorks 1.4.2
> its behaviour is different. Now I restartet ANTLRWorks and I got an
> error [17:35:45]
> F:\ProgLang\Parsers\ANTLR3\Projects\Tutorial\Test\IntegerZero\output\__
> Test__.java:14: cannot find symbol [17:35:45] symbol  : method Cl()
> [17:35:45] location: class Problem1bParser
> [17:35:45]             g.Cl();
> [17:35:45]              ^
> [17:35:45] 1 error
>
> Some minutes ago the debugger blocked, before only 'a' was recognized.
>
>
> Many thanks and greetings
> Udo
>
> 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