Antlr bug or wrong use?

61 views
Skip to first unread message

Nils Henrik Lorentzen

unread,
Aug 13, 2016, 2:55:06 PM8/13/16
to antlr-discussion
Hi, the grammar below gives me the output

line 1:8 extraneous input '<' expecting {HIALPHA, LOALPHA}
line 1:12 mismatched input '<EOF>' expecting '<'

## enter name:  <foo

for the input

keyword <foo

while I would have expected this 'name' rule never to have be  called since '<' is not part of valid characters for 'name' rule. Is there something wrong in my grammar or is this a bug in Antlr?

BTW if I change the 'name' after LBRACKET to 'number' and input to "keyword <123", name rule is called back with empty string as I suppose should be.

Grammar:

grammar
AntlrTest;

program
: KEYWORD name LBRACKET name;

name
: (HIALPHA | LOALPHA)+;

number
: DIGIT+;

KEYWORD        
: 'keyword';

HIALPHA        
: [A-Z];
LOALPHA    
: [a-z];
DIGIT        
: [0-9];

LBRACKET  
: '<';

WHITESPACE
 
:  (' '|'\t'|'\n'|'\r')+ -> skip
 
;





Main program:

public class Main {
   
public static void main(String [] args) {

       
final ANTLRInputStream input = new ANTLRInputStream("keyword <foo");
       
       
final AntlrTestLexer lexer = new AntlrTestLexer(input);
       
       
final CommonTokenStream stream = new CommonTokenStream(lexer);
       
       
final AntlrTestParser parser = new AntlrTestParser(stream);
       
       
final ParserRuleContext program = parser.program();
             
       
ParseTreeWalker.DEFAULT.walk(new AntlrTestBaseListener() {

           
@Override
           
public void enterName(NameContext ctx) {
               
super.enterName(ctx);
               
               
System.out.println("## enter name:  " + ctx.getText());
           
}
       
}, program);
   
}
}



Mike Lischke

unread,
Aug 14, 2016, 6:12:14 AM8/14/16
to antlr-di...@googlegroups.com

> line 1:8 extraneous input '<' expecting {HIALPHA, LOALPHA}
> line 1:12 mismatched input '<EOF>' expecting '<'
>
> ## enter name: <foo
>
> for the input
>
> keyword <foo

Which is correct since '<' is not valid after 'keyword'.

>
> while I would have expected this 'name' rule never to have be called since '<' is not part of valid characters for 'name' rule. Is there something wrong in my grammar or is this a bug in Antlr?

Imagine how the parser works. After matching 'keyword' it continued with the next expected input, which is the name rule, so it entered it, which is why your enterName function has been called. At this point it's not clear yet if that rule matches or not. Which is why I prefer to implement the exit* variants instead. Using enter* is probably more useful if you need to set up something that gets modified during the match process in that particular rule (or subsequent rules called by it).

I don't know however why the context gives you '<foo' as text. In fact, it shouldn't have any text yet as we just entered the name rule and the context has just been freshly created without any further updates. The text for a context is created by concatenating all text from its children separated by comma. On enter however there shouldn't be any children yet. If you can step through the ANTLR runtime I'd recommend to do so to see from which context this text actually comes from.

Mike
--
www.soft-gems.net

Reply all
Reply to author
Forward
0 new messages