0

I've got a basic ANTLR-4 grammar at the moment in my Eclipse IDE for Java, and have the following:

// Parser

importDeclaration:
    'use' name=FQN ';'
;

// Lexer and terminals

fragment LETTER: [a-zA-Z_];
fragment LETTER_OR_DIGIT: [a-zA-Z0-9_];
ID: LETTER LETTER_OR_DIGIT*;
FQN: ID ('.' ID)*;

WS: [ \t\r\n]+ -> skip ; // skip spaces, tabs, newlines
COMMENT: '/*' .*? '*/' -> channel(HIDDEN);
LINE_COMMENT: '//' ~[\r\n]* -> channel(HIDDEN);

Hopefully a short and sweet question, when I do the following:

use test;

I get:

line 1:4 mismatched input 'test' expecting FQN

And name throws a NullPointerException if I try to use it.
Except it works fine when I do:

use test.test;

This has to do with the ('.' ID)* portion, but have no idea why it's happening.

Any ideas?

Edit:

Just realised my code was wrong, had (FQN|ID) which was my bad workaround. Fixed it now.

  • The input "test" will always be tokenized as an ID token, not a FQN. For an explanation, see: https://stackoverflow.com/questions/27541957/antlr4-lexer-rules-dont-work-as-expected – Bart Kiers Jul 19 '17 at 12:36
  • @BartKiers Thanks Bart. I've seen the answer and it provides a worthy explanation. Is there any possibility you can *help* me, so I can fix it? The other answer does not provide that information. –  Jul 19 '17 at 13:04
  • 1
    Sure, I edited the other question. In your case, the solution would be to change FQN from a lexer rule to a parser rule. This is also how Java and C# parse their imports/using names. HTH – Bart Kiers Jul 19 '17 at 13:14
  • @BartKiers Beautiful! Thankyou very much Bart. If you didn't close it I would of spammed the accept answer button. :) –  Jul 19 '17 at 13:27
  • 1
    No problem @finnrayment! – Bart Kiers Jul 19 '17 at 13:36

0 Answers0