質問

I am using ANTLRWorks 1.5 for C (ANTLR 3.5). I created a lexer and parser file. When trying to generate code, it returns as error <[18:52:50] error(100): Script.g:57:2: syntax error: antlr: MissingTokenException(inserted [@-1,0:0='<missing EOF>',<-1>,57:1] at options {)>.

Here is the code, please tell what am I missing.

/* ############################## 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;
役に立ちましたか?

解決

Your "combined" grammar Lexer only has lexer rules, while when you only define grammar, ANTLR expects at least 1 parser rule.

There are 3 different types of grammars

  • combined grammar: grammar Foo, generates:
    • class FooParser extends Parser and
    • class FooLexer extends Lexer
  • parser grammar: parser grammar Bar, generates:
    • class Bar extends Parser
  • lexer grammar: lexer grammar Baz, generates:
    • class Baz extends Lexer

So, in your case, change grammar Lexer; into lexer grammar ScriptLexer; (don't name your lexer grammar Lexer since it is the base lexer class in ANTLR!) and import this lexer in your parser grammar:

parser grammar ScriptParser;

import ScriptLexer;    

options { 
  language=C;
  output=AST;
  ASTLabelType=pANTLR3_BASE_TREE;
}

// ...

Related:

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top