[antlr-interest] Cannot understand why interpreter accepts bad input...

Bern.McCarty at bentley.com Bern.McCarty at bentley.com
Fri Dec 7 06:38:00 PST 2007


I'm really struggling with trying to use the AntlrWorks interpreter and
greatly appreciate any help I can get.  The first grammer below does NOT
require Gavin's trick of defining a "IllegalChar: .;" rule in order to
get a NoViableAltException when feeding bad input like "a" into the
interpreter.  The 2nd grammer DOES require Gavin's trick in order to get
a NoViableAltException out of the same bad input.  I don't get it.  Is
there a way to tell the interpreter to please always give me errors when
I feed it bad input? Otherwise I get tricked into taking my grammer down
a bad path because I do not realize anything is wrong.  Would I be
better off forgoing the IDE and just running the command-line tool to
test my grammar each time I change it?  I just can't seem to make heads
or tails of the interpreter/Debugger combo.

 

// First grammer.  Correctly provides an exception for bad input like
"a":

 

grammar AtLeastThisOneComplainsAboutBadInput;

 

top         : atomList EOF;

 

atomList:             atom (',' atom)*;

 

atom     :               ASPECTID

                |              '(' atomList ')'

                ;

 

ASPECTID

                :               'PP_ID_FeatureAspects' ('_' ASPECTNAME)+

                ;

 

fragment ASPECTNAME

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

                ;

 

WS         :  (' '|'\r'|'\t'|'\u000C'|'\n') {$channel=HIDDEN;}

                ;

 

Second Grammer.  Provides no Exception for bad input such as "a".  What
is so different about this grammer than the first?  The input is clearly
illegal is it not?  What is the interpreter for again?

 

grammar PPExpr;

 

top         : expr EOF;

 

expr       : unaryExp | equalityExp

                ;

 

unaryExp:            '!' equalityExp

                ;

 

equalityExp        :               andOrExpr (('=='|'!=') andOrExpr)*

                ;

 

andOrExpr

                :               atom (('||'|'&&') atom)*

                ;

 

atom     :

                |              ASPECTID

                |              '(' expr ')'

                ;

 

ASPECTID

                :               'PP_ID_FeatureAspects' ('_' ASPECTNAME)+

                ;

 

fragment ASPECTNAME

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

                ;

 

WS         :  (' '|'\r'|'\t'|'\u000C'|'\n') {$channel=HIDDEN;}

                ;

 

 

 

From: antlr-interest-bounces at antlr.org
[mailto:antlr-interest-bounces at antlr.org] On Behalf Of G R
Sent: Friday, December 07, 2007 8:03 AM
To: antlr-interest at antlr.org
Subject: Re: [antlr-interest] Cannot understand why parser accepts
input...

 

Try using the debugger and take a look at the output section.

G.R.

2007/12/7, Bern.McCarty at bentley.com < Bern.McCarty at bentley.com
<mailto:Bern.McCarty at bentley.com> >:

Yes that is a big help.  Thanks. At least now I see a
NoViableAltException in the interpreter output "diagram".  However,
there is still no indication of anything being amiss at all in the
console window.  Shouldn't the console window have something to say
about an exception that has caused parsing to fail?

 

-Bern

 

From: antlr-interest-bounces at antlr.org [mailto:
antlr-interest-bounces at antlr.org
<mailto:antlr-interest-bounces at antlr.org> ] On Behalf Of G R
Sent: Friday, December 07, 2007 7:26 AM
To: antlr at mirality.co.nz; antlr-interest at antlr.org


Subject: Re: [antlr-interest] Cannot understand why parser accepts
input...

 

Try using this lexer rule at the end of your grammar :

OtherChar :.;

This will define every character, and if you don't mention in your
grammar that you want a "OtherChar", then, an exception will be raised. 
Hope, this helps you.

G.R.

2007/12/7, Bern.McCarty at bentley.com < Bern.McCarty at bentley.com
<mailto:Bern.McCarty at bentley.com> >:

...but when I enter a clearly invalid input into the interpreter such as
simply "3" or "B=3C the console contains no errors or warnings at all.

How could a mode that allows incorrect input without even issuing a
complaint/warning possibly be helpful? Is there a way to turn it off? 

Thanks,
-Bern



-----Original Message-----
From: antlr-interest-bounces at antlr.org
[mailto: antlr-interest-bounces at antlr.org
<mailto:antlr-interest-bounces at antlr.org> ] On Behalf Of Gavin Lambert
Sent: Friday, December 07, 2007 3:23 AM
To: Bern McCarty; antlr-interest at antlr.org
Subject: Re: [antlr-interest] Cannot understand why parser accepts 
input...

At 17:45 7/12/2007, Bern.McCarty at bentley.com wrote:

>Why does the below grammer successfully parse the input " B ==3C"
>When there are no numerals allowed anywhere in the grammer? 

It's an automatic error-recovery thing.  When ANTLRv3 encounters a
character it can't figure out, it prints an error to the console
(by default), drops the character, and then tries again.

 

 

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


More information about the antlr-interest mailing list