[antlr-interest] The NOT (~) Operator

Jim Idle jimi at temporal-wave.com
Sun Apr 13 16:03:24 PDT 2008


There are a few things going on here:

 

1) You have TAB*, which means that the lexer rule could be empty and therefore match infinitely anything;

2) You can only use ~ on character sets, not rules that have sequences with optional elements;

3) You should be able to do this in the parser, but I don't think you have to;

4) Your INDENTATION rule will match a sequence of tabs then one character other than a newline - I can't think that this is what you want?

 

Try:

 

INDENTATION : '\t'+ ;

 

Or, I think that you are trying to say 'only match the tabs if they are not on a blank line' so'

 

fragment

WS : ' ' | '\t';

 

INDENTATION   

                : '\t'+ (

                                   ('\n'|'\r') { $type = WS; $channel = HIDDEN; }

                                |

                            )

                ;

 

Without knowing exactly what you are trying to do, it is difficult to advise exactly, but that should get you on the right path.

 

You can do this:

 

.... ~NLCHARS ;

 

fragment

NLCHARS : '\n' | '\r' ;

 

 

Jim 

 

From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-bounces at antlr.org] On Behalf Of Dan
Sent: Sunday, April 13, 2008 9:42 AM
To: antlr-interest at antlr.org
Subject: Re: [antlr-interest] The NOT (~) Operator

 

Sven,

 

I'm still fairly new to ANTLR but, based on what I've learned, lexer rules are meant to be pretty dumb, and are not meant to handle alternatives based on other lexer rules.  For that, it seems to be recommended to use parser rules.

 

See pages 289-290 of "The Definitive ANTLR Reference" book, if you have access to it.

 

Although I haven't yet seen a clear explanation of why this is.

 

Why is making "INDENTATION" a parser rule not an option for you?

 

-Dan



 

On Sat, Apr 12, 2008 at 1:10 AM, Sven Busse <mail at ghost23.de> wrote:

Hi,

i have a problem with a grammar error message, i don't understand.
I have this grammar (reduced to the relevant bit):

grammar simpletest;

INDENTATION
: TAB* ~NEWLINE;

NEWLINE
: '\r'? '\n';

fragment
TAB : '\t';

Checking the grammar with ANTLRWorks gives me this error:

simpletest.g:0:0: syntax error: buildnfa: <AST>:6:11: unexpected AST node: ?

The problem seems to relate to the "~NEWLINE", because if i delete it, i
get no error. Also, if i change the "INDENTATION" to a parser rule
"indentation", i get no error, but that is not an option for me.

Can someone explain to me, what the reason behind this error is?

Thank you
Sven



-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080413/babdbae9/attachment.html 


More information about the antlr-interest mailing list