[英]ANTLR generating invalid java exceptions throws code
這些天我一直在使用ANTLRwork 1.5和antlr runtime 3.5。 這是我發現的一個奇怪的事情:Antlr為我生成這種java代碼:
public final BLABLABLAParser.addExpression_return addExpression() throws {
blablabla...
}
請注意,此函數不會拋出任何內容,這在java中無效。 所以我需要手動糾正這些錯誤。
誰知道為什么?
這里是示例語法,它直接取自書籍語言實現模式 。
// 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代碼
我使用了上面描述的確切配置,並使用了復制/粘貼語法。 為您提到的規則生成的簽名如下:
// $ANTLR start "addExpression"
// C:\\dev\\Cymbol.g:72:1: addExpression : postfixExpression ( '+' ^ postfixExpression )* ;
public final CymbolParser.addExpression_return addExpression() throws RecognitionException {
你可以發布生成文件的第一行嗎? 它應該以// $ANTLR 3.5
開頭,如下所示:
// $ANTLR 3.5 C:\\dev\\Cymbol.g 2013-02-13 09:55:44
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.