[antlr-interest] Composite grammars: compile error with @header tag.

Pavel Ganelin pganelin at gmail.com
Mon May 9 12:45:21 PDT 2011


I have 4 grammar file: lexer L.g, parser P.g and two grammars G1 and G2 (see
below). After running antlr to generate Java files compilation fails:

    [mkdir] Created dir: target\classes
    [javac] Compiling 10 source files to target\classes
    [javac] target\src\example\G1Lexer.java:18: cannot find symbol
    [javac] symbol  : class G1_L
    [javac] location: class G1Lexer
    [javac]     public G1_L gL;
    [javac]            ^
    [javac] target\src\example\G1Lexer.java:9: duplicate class: G1Lexer
    [javac] public class G1Lexer extends Lexer {
    [javac]        ^
    [javac] target\src\example\G1_L.java:22: cannot access example.G1Lexer
    [javac] bad class file: target\src\example\G1Lexer.java
    [javac] file does not contain class example.G1Lexer
    [javac] Please remove or make sure it appears in the correct
subdirectory of the classpath.
    [javac]     public G1Lexer gG1;
    [javac]            ^
    [javac] 3 errors

The workaround would be to move @lexer::header file to both G1.g and G2.g.
It works but the solution has 2 drawbacks:

1. Code duplication.
2. L.java can not be compiled as standalone file anymore because it lacks
package declaration.

==========================L.g================================

lexer grammar L;

@lexer::header {
package example;
}

IDENT : 'A-Z' +
;
==========================P.g==========================
parser grammar P ;

tokens {
  BEGIN = 'begin';
  END = 'end';
}

method: BEGIN IDENT END;
============================ G1.g ======================
grammar G1 ;
import L,P;

tokens {
  PROGRAM = 'program';
}


@header {
package example;
}

program: PROGRAM method;
===========================G2.g=====================
grammar G2 ;
import L,P;

tokens {
  FUNCTION = 'function';
}

@header {
package example;
}

function: FUNCTION method;
==================================================


More information about the antlr-interest mailing list