Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

If I call:

java org.antlr.Tool -o outdir sources/com/example/Java5.g

...with antlr-3.1.3 the parser and lexer code will be generated in the directory outdir/sources/com/example. But the generated classes don't have any package statement. I need them to life in the package com.example.

Is there a way to specify the target package?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
198 views
Welcome To Ask or Share your Answers For Others

1 Answer

ANTLR provides a header tool which allows you to include package and imports. You include this in your *.g grammar file:

@header {
    package org.xmlcml.cml.converters.antlr;
    import java.util.HashMap;
}

And you may need it in the Lexer as well:

@lexer::header {package org.xmlcml.cml.converters.antlr;}

and in case you need to add some members and code:

@members {
    HashMap<String, Object> objectMap = new HashMap<String, Object>();
    //...

    private void addArrayValue(String content) {
    //... code required by snippets in the grammar

    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...