Diolor
Diolor

Reputation: 13450

Antlr4 import grammar from path

Simple questions which I so far think the answer is no:

Is it possible to import grammars from a child directory? e.g.:

grammar literals;

import myFolder/IntegerLiterals;

with the following structure:

/ 
 literals.g4
 myFolder/
    IntegerLiterals.g4

Upvotes: 4

Views: 3487

Answers (2)

Sabed
Sabed

Reputation: 31

I had the same problem and found this site but got no solution. After some more googling I found a solution for this problem:

Actually there is now a way how to import grammars from other directories:

Use the -lib option when running antlr to define where your gramars are that you want to import. Here is the docu for the options: https://github.com/antlr/antlr4/blob/master/doc/tool-options.md

So you write your grammar:

grammar literals;
import IntegerLiterals;

Then you run in command line:

antlr4 -lib C:/.../myFolder literals.g4

Upvotes: 3

Lucas Trzesniewski
Lucas Trzesniewski

Reputation: 51430

There's an ANTLR 4 grammar written in ANTLR 4 on the examples repo.

Let's take a closer look:

delegateGrammars
    :   IMPORT delegateGrammar (COMMA delegateGrammar)* SEMI
    ;

delegateGrammar
    :   id ASSIGN id
    |   id
    ;

id  :   RULE_REF
    |   TOKEN_REF
    ;

So, looks like the answer really is no, as you can't put directory separators in there. The file name must be a valid rule/token name.

Upvotes: 2

Related Questions