我正在使用ANTLRWorks 1.5 for C(ANTLR 3.5)。
我创建了一个词法分析器和解析器文件。
尝试生成代码时,它会返回错误<[18:52:50] error(100): Script.g:57:2: syntax error: antlr: MissingTokenException(inserted [@-1,0:0='<missing EOF>',<-1>,57:1] at options {)>
。
以下是代码,请告诉我缺少什么。
/* ############################## L E X E R ############################ */
grammar Lexer;
options {
language = C;
output = AST; //generating an AST
ASTLabelType = pANTLT3_BASE_TREE; //specifying a tree walker
k=1; // Only 1 lookahead character required
}
// Define string values - either simple unquoted or complex quoted
STRING : ('a'..'z'|'A'..'Z'|'0'..'9'|'_' | '+')+
| ('"' (~'"')* '"');
// Ignore all whitespace
WS :(' '
| '\t'
| '\r' '\n' { newline(); }
| '\n' { newline(); }
)
{ $setType(Token.SKIP); } ;
// TODO:Single-line comment
LINE_COMMENT : '/*' (~('\n'|'\r'))* ('\n'|'\r'('\n')?)?
{ $setType(Token.SKIP); newline(); } ;
// Punctuation
LBRACE : '<';
RBRACE : '>';
SLASH : '/';
EQUALS : '=';
SEMI : ';';
TRIGGER : ('Trigger');
TRIGGERTYPE : ('Fall') SLASH ('Rise')|('Rise') SLASH ('Fall')|('Fall')|('Rise');
DEFAULT : ('Default TimeSet');
TIMESETVAL : ('TSET_')('0..9')*;
/* ############################## P A R S E R ############################ */
grammar Script;
options {
language=C;
output=AST; // Automatically build the AST while parsing
ASTLabelType=pANTLR3_BASE_TREE;
//k=2; // Need lookahead of two for props without keys (to check for the =)
}
/*tokens {
SCRIPT; // Imaginary token inserted at the root of the script
BLOCK; // Imaginary token inserted at the root of a block
COMMAND; // Imaginary token inserted at the root of a command
PROPERTY; // Imaginary token inserted at the root of a property
}*/
/** Rule to parse Trigger line
*/
trigger : TRIGGER EQUALS TRIGGERTYPE SEMI;
/** Rule to parse TimeSet line
*/
timeset : DEFAULT TIMESETVAL;
答案 0 :(得分:0)
你的“组合”语法Lexer
只有lexer规则,而当你只定义grammar
时,ANTLR至少需要1个解析器规则。
有3种不同类型的语法
grammar Foo
,生成:
class FooParser extends Parser
和class FooLexer extends Lexer
parser grammar Bar
,生成:
class Bar extends Parser
lexer grammar Baz
,生成:
class Baz extends Lexer
因此,在您的情况下,将grammar Lexer;
更改为lexer grammar ScriptLexer;
(不要命名您的词法分析器语法Lexer
,因为它是ANTLR中的基础词法分析器类!)并导入此词法分析器你的解析器语法:
parser grammar ScriptParser;
import ScriptLexer;
options {
language=C;
output=AST;
ASTLabelType=pANTLR3_BASE_TREE;
}
// ...
相关: