probably at the beach
probably at the beach

Reputation: 15217

Antlr undefined import in Antlrworks using composite grammars

I'm trying to use a composite grammar with Antlr 3.1 and Antlrworks 1.4.2. When I put the import statement in, it says 'undefined import'. I've tried a number of different combinations of lexer grammar and parser grammer but can't get it to generate the code. Am I missing something obvious? Am example is below.

grammar Tokens;

TOKEN   :   'token';

grammar Parser;
import Tokens;//gives undefined import error

rule    :   TOKEN+;

I'm referencing the documentation from

http://www.antlr.org/wiki/display/ANTLR3/Composite+Grammars

Thanks

Upvotes: 2

Views: 1619

Answers (2)

probably at the beach
probably at the beach

Reputation: 15217

Argghh It was something stupid. Antlrworks will underline the import and highlight all the tokens as undefined syntax errors but still allow you to generate the code if you try!

The reason it wasn't working the first time was the import was above the options as per Bart's suggestions.

Upvotes: 2

Bart Kiers
Bart Kiers

Reputation: 170227

When separating lexer- and parser grammars, you need to explicitly define what type of grammar it is.

Try:

parser grammar Parser;
import Tokens;//gives undefined import error

rule    :   TOKEN+;

and:

lexer grammar Tokens;

TOKEN   :   'token';

Note that from a combined grammar file Foo.g, the lexer and parser get a Parser and Lexer prefix by default: FooLexer.java and FooParser.java respectively. But in "explicit" grammars, the name of the .java file is that of the grammar itself: Parser.java and Tokens.java in your case. You might want to watch out calling a class Parser since that is the name of ANTLR's base parser class:

http://www.antlr.org/api/Java/classorg_1_1antlr_1_1runtime_1_1_parser.html

Also watch out to place the import statement below the options { ... } section, but before any tokens { ... } you may have defined, otherwise you might get strange errors.

Upvotes: 3

Related Questions