Tony Ennis
Tony Ennis

Reputation: 12289

Why does my antlr grammar give me an error?

I have the little grammar below. node is the start production. When my input is (a:b) I get an error: line 1:1 extraneous input 'a' expecting {':', INAME}

Why is this?

EDIT - I forgot that the lexer and parser run as a separate phases. By the time the parser runs, the lexer has completed. When the lexer runs it has no knowledge of the parser rules. It has already made the TYPE/INAME decision choosing TYPE per @bart's reasoning below.

grammar g1;

TYPE: [A-Za-z_];
INAME: [A-Za-z_];

node: '(' namesAndTypes ')';

namesAndTypes:
               INAME ':' TYPE
             | ':' TYPE
             | INAME
             ;

Upvotes: 1

Views: 107

Answers (1)

Bart Kiers
Bart Kiers

Reputation: 170138

That is because the lexer will never produce an INAME token. The lexer works in the following was:

  1. try to match as much characters as possible
  2. when 2 or more lexer rules match the same characters, let the one defined first "win"

Because the input "a" and "b" both match the TYPE and INAME rules, the TYPE rule wins because it is defined first. It doesn't matter if the parser is trying to match an INAME rule, the lexer will not produce it. The lexer does not "listen" to he parser.

You could create some sort of ID rule, and then define type and iname parser rules instead:

ID: [A-Za-z_];

node
 : '(' namesAndTypes ')'
 ;

namesAndTypes
 : iname ':' type
 | ':' type
 | iname
 ;

type
 : ID
 ;

iname
 : ID
 ;

Upvotes: 1

Related Questions