views:

95

answers:

1

I have a simple ANTLR grammar, which I have stripped down to its bare essentials to demonstrate this problem I'm having. I am using ANTLRworks 1.3.1.

grammar sample;

assignment  :  IDENT ':=' NUM ';' ;

IDENT       :  ('a'..'z')+ ;

NUM         :  ('0'..'9')+ ;

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

Obviously, this statement is accepted by the grammar:

x := 99;

But this one also is:

x := @!$()()%99***;

Output from the ANTLRworks Interpreter:

ANTLR Interpreter diagram

What am I doing wrong? Even other sample grammars that come with ANTLR (such as the CMinus grammar) exhibit this behavior.

+2  A: 

If you look at the console of your ANTLRWorks IDE, you'll see a lot of lexer errors.

Try it on the command line:

grammar Sample;

@members {
  public static void main(String[] args) throws Exception {
    ANTLRStringStream in = new ANTLRStringStream("x := @!$()()\%99***;");
    SampleLexer lexer = new SampleLexer(in);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    SampleParser parser = new SampleParser(tokens);
    parser.assignment();
  }
}

assignment  :  IDENT ':=' NUM ';' ;

IDENT       :  ('a'..'z')+ ;

NUM         :  ('0'..'9')+ ;

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

and then:

// generate parser/lexer
java -cp antlr-3.2.jar org.antlr.Tool Sample.g

// compile
javac -cp antlr-3.2.jar *.java

// run Windows
java -cp .;antlr-3.2.jar SampleParser
// or run *nix/MacOS
java -cp .:antlr-3.2.jar SampleParser

will produce:

line 1:5 no viable alternative at character '@'
line 1:6 no viable alternative at character '!'
line 1:7 no viable alternative at character '$'
line 1:8 no viable alternative at character '('
line 1:9 no viable alternative at character ')'
line 1:10 no viable alternative at character '('
line 1:11 no viable alternative at character ')'
line 1:12 no viable alternative at character '%'
line 1:15 no viable alternative at character '*'
line 1:16 no viable alternative at character '*'
line 1:17 no viable alternative at character '*'
Bart Kiers
Exactly!! That's what I talked about in the comment!!
Winston Chen
@Wing, ANTLRWorks also shows these errors, but then in a different screen.
Bart Kiers
@Bart, yeah. Thank you for your info. But then I don't understand why Barry couldn't see the antlr err msgs. Is he using a different version of antlr?
Winston Chen
@Wing: no, he's using ANTLRWorks (which also uses a "normal" ANTLR version of course). And in ANTLRWorks, these error messages are not directly visible when interpreting ones grammar. But Barry *does* get them as we do.
Bart Kiers
@Bart, Thanks. I am using the plug-in in eclipse to edit my .g files. That's why I only see the err msgs at console.
Winston Chen
It must be a bug in ANTLRworks, then. I do see the messages in the Console, but two issues annoy me: a) I don't get the NoViableAltException node appearing in the parse tree, and b) the red warning text at the bottom of the window ("Errors reported in console") doesn't appear. This is unfortunate since I'm using ANTLRworks with my students and they're not experienced enough to tell whether the problem is with them or the tool.
Barry Brown
@Barry, I hear you. Unfortunately, that sometimes happens with ANTLRWorks. Now that Terence Parr finished his Language Patterns book, I've heard that he is going to spend some (more) time in the development of ANTLRWorks, but that is of little use to your current group of students though.
Bart Kiers