8

I have a large grammar file, and plan to split it into multiple ones, so that I can reuse some of those smaller files in another grammar file. I have tried doing it but failed. Can you please tell if such a feature is available, and if so, please direct me towards an example.

Nishanth Reddy
  • 589
  • 2
  • 14
  • 27

2 Answers2

7

If you want to split lexer and parser.

Lexer:

lexer grammar HelloLexer;
Hello : 'hello' ;
ID : [a-z]+ ;             // match lower-case identifiers
WS : [ \t\r\n]+ -> skip ; // skip spaces, tabs, newlines

Parser:

parser grammar HelloParser;
options { tokenVocab=HelloLexer; }
r  : Hello ID ;      

Remember to name the files HelloLexer.g4 and HelloParser.g4

if you want to import a whole grammar, then you should use the import keyword

grammar Hello;

import OtherGrammar;

Hello : 'hello' ;
ID : [a-z]+ ;             // match lower-case identifiers
WS : [ \t\r\n]+ -> skip ; // skip spaces, tabs, newlines    
r  : Hello ID ;
XS_iceman
  • 161
  • 1
  • 6
4

You did not mention ANTLR version, so I am going to assume you are using the current one - 4.x. In ANTLR4 grammars can be imported with import keyword. Something like this:

File: CommonLexerRules.g4

lexer grammar CommonLexerRules;

ID  :   [a-zA-Z]+ ;
...

File: MyParser.g4

grammar MyParser;      
import CommonLexerRules; //includes all rules from lexer CommonLexerRules.g4
...

Rules in the “main grammar” override rules from imported grammars to implement inheritance. See more details here: https://theantlrguy.atlassian.net/wiki/display/ANTLR4/Grammar+Structure#GrammarStructure-GrammarImports

user3890638
  • 164
  • 3
  • yes, the version I am using is antlr4.5-opt. When I try to reference a rule in an imported grammar in the main-grammar, I get this error-message: reference to undefined rule: temp_rule (com.tunnelvisionlabs:antlr4-maven-plugin:4.5:antlr4:default:generate-sources) – Nishanth Reddy Oct 08 '15 at 10:11
  • 1
    try to reduce your imported and main grammar files to the absolute minimum, just to demonstrate the error, and add files content in your original question – user3890638 Oct 09 '15 at 12:12
  • To me seems only one import per grammar? Only way I could make it work was main.g4 imports a.g4 which imports b.g4. Not nice. But main.g4 importing a.g4 and b.g4 does indeed cause reference to undefined rules. – KevinY Apr 07 '19 at 10:18
  • This might be irrelevant now, but for other viewers, to remove the error `undefined rule`, write `options { tokenVocab = YourLexerHere; }` after `grammar YourGrammarName;` instead of importing your Lexer Grammar. – Naman B Gor Aug 20 '22 at 10:25