Using @header in ANTLR
Asked Answered
S

1

11

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.

Specific answered 1/11, 2010 at 1:56 Comment(0)
U
13

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

Undercast answered 1/11, 2010 at 2:8 Comment(1)
I believe that in the next version of ANTLR (v4), the order of these blocks does not matter.Reduction

© 2022 - 2024 — McMap. All rights reserved.