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

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

问题描述

如果我打电话:

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

...使用 antlr-3.1.3,解析器和词法分析器代码将在目录 outdir/sources/com/example 中生成.但是生成的类没有任何 package 语句.我需要它们在 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;
}

您可能也需要在词法分析器中使用它:

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天全站免登陆