0

我正在使用 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;
4

1 回答 1

0

您的“组合”语法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;
}

// ...

有关的:

于 2013-02-08T08:10:42.387 回答