如何为ANTLR指定目标包? [英] How to specify a target package for ANTLR?

查看:84
本文介绍了如何为ANTLR指定目标包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我打电话:

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

...使用antlr-3.1.3将在目录<$中生成解析器和词法分析器代码C $ C> OUTDIR /来源/ COM /示例。但是生成的类没有任何语句。我需要它们在包中生活 com.example

...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?

推荐答案

ANTLR提供了一个标题工具,允许您包含包和导入。您将此包含在* .g语法文件中:

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;
}

您也可能需要在Lexer中使用它:

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

    }
}

这篇关于如何为ANTLR指定目标包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆