生成无效java异常的ANTLR会抛出代码 [英] ANTLR generating invalid java exceptions throws code

查看:181
本文介绍了生成无效java异常的ANTLR会抛出代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这些天我一直在使用ANTLRwork 1.5和antlr runtime 3.5。这是我发现的一个奇怪的事情:
Antlr为我生成这种java代码:

I've been using ANTLRwork 1.5 these days, together with antlr runtime 3.5. Here is a weird thing i found: Antlr is generating this kind of java code for me:

public final BLABLABLAParser.addExpression_return addExpression() throws {
    blablabla...
}   

通知这个函数什么都不抛出,这在java中是无效的。所以我需要手动纠正这些错误。

notice that this function throws nothing, and this is invalid in java. So I need to correct these mistakes manually.

任何人都知道为什么?

这里是示例语法,它直接来自书籍语言实现模式

here is the sample grammar, it's directly taken from the book Language implementation patterns.

// START: header
// START: header
grammar Cymbol; // my grammar is called Cymbol
options {
output = AST;
ASTLabelType = CommonTree;
}

tokens{
METHOD_DECL;
ARG_DECL;
BLOCK;
VAR_DECL;
CALL;
ELIST;
EXPR;
}
// define a SymbolTable field in generated parser

compilationUnit // pass symbol table to start rule
:   (methodDeclaration | varDeclaration)+ // recognize at least one variable declaration
;
// END: header

methodDeclaration 
:   type ID '(' formalParameters? ')' block
    -> ^(METHOD_DECL type ID formalParameters? block)
;

formalParameters
:   type ID (',' type ID)* -> ^(ARG_DECL type ID)+
;

// START: type   
type
:   'float' 
|   'int' 
|   'void'  
;
// END: type   

block   :   '{' statement* '}' -> ^(BLOCK statement*)
;

// START: decl
varDeclaration
:   type ID ('=' expression)? ';' -> ^(VAR_DECL type ID expression?)// E.g., "int i = 2;", "int i;"
;
// END: decl

statement
:   block
|   varDeclaration
|   'return' expression? ';' -> ^('return' expression?)
|   postfixExpression
    (
        '=' expression -> ^('=' postfixExpression expression)
        | -> ^(EXPR postfixExpression)
    ) ';'
;



 expressionList
:   expression(',' expression)* -> ^(ELIST expression+)
| -> ELIST
;

expression
:   addExpression -> ^(EXPR addExpression)
;
addExpression
:   postfixExpression('+'^ postfixExpression)*
;
postfixExpression
:   primary (lp='('^ expressionList ')'! {$lp.setType(CALL);})*
;
// START: primary
primary
:   ID // reference variable in an expression
|   INT
|   '(' expression ')' -> expression
;
// END: primary

// LEXER RULES

ID  :   LETTER (LETTER | '0'..'9')*
    ;

fragment
LETTER  :   ('a'..'z' | 'A'..'Z')
     ;

INT :   '0'..'9'+
    ;

WS  :   (' '|'\r'|'\t'|'\n') {$channel=HIDDEN;}
    ;

SL_COMMENT
    :   '//' ~('\r'|'\n')* '\r'? '\n' {$channel=HIDDEN;}
    ;


推荐答案

编辑:这是ANTLRWorks 1.5中的一个错误,已在下一版本中修复。

#5:ANTLRworks无法生成正确的Java代码

This is a bug in ANTLRWorks 1.5 that has already been fixed for the next release.
#5: ANTLRworks fails to generate proper Java Code

我使用了上面描述的确切配置,并使用了复制/粘贴语法。为您提到的规则生成的签名如下:

I used the exact configuration you described above, with a copy/pasted grammar. The signature generated for the rule you mention was the following:

// $ANTLR start "addExpression"
// C:\\dev\\Cymbol.g:72:1: addExpression : postfixExpression ( '+' ^ postfixExpression )* ;
public final CymbolParser.addExpression_return addExpression() throws RecognitionException {

你能发布第一行吗?生成的文件?它应该以 // $ ANTLR 3.5 开头,如下所示:

Can you post the first line of the generated file? It should start with // $ANTLR 3.5 like the following:

// $ANTLR 3.5 C:\\dev\\Cymbol.g 2013-02-13 09:55:44

这篇关于生成无效java异常的ANTLR会抛出代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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