9
votes

I'm having trouble getting the "@header" or any other @ rule to work in ANTLR. With a very basic grammer, like this:

grammar test;

options {
     language = CSharp2;
}

@header {
    using System.Collections.Generic;
}

tokens {
  TEST;
}

something : TEST*;

TEST : '0' .. '9'; 

This seems to adhere to the ANTLR documentation and various other examples online, but when I try to generate the output through either ANTLRWorks v1.4 or the ANTLR jar v3.2, I get the following error:

Cannot generate the grammar because:

error(100): [path]\test.g:11:1 syntax error: antlr: [path]\test.g:11:1 unexpected token: tokens {

Which I cannot seem to shake. I am able to generate the lexer and parser successfully without the @header included in the grammar. I have the latest version of Java on Windows:

java version "1.6.0_22" Java(TM) SE Runtime Environment (build 1.6.0_22-b04) Java HotSpot(TM) Client VM (build 17.1-b03, mixed mode, sharing)

I assume this is something stone simple and I'm just blind to it, so please bring on the obvious answers.

1

1 Answers

11
votes

The error is because those blocks are required to appear in a certain order.

  1. options
  2. tokens
  3. @header
  4. @members

then your rules