Tutorial :Unusual ANTLR error when attempting to reorganize grammar into two files



Question:

I am reorganizing my grammar into two files in order to accomodate a tree grammar; Lua.g and LuaGrammar.g. Lua.g will have all of my lexer rules, and LuaGrammar.g will have all of my tree grammar and parser rules. However, when i try and compile LuaGrammar.g i get the following error:

[00:28:37] error(10):  internal error: C:\Users\RCIX\Desktop\AguaLua\Project\trunk\AguaLua\AguaLua\ANTLR Data\LuaGrammar.g : java.lang.IllegalArgumentException: Can't find template ruleRefBang.st; group hierarchy is [CSharp2]  org.antlr.stringtemplate.StringTemplateGroup.lookupTemplate(StringTemplateGroup.java:507)  org.antlr.stringtemplate.StringTemplateGroup.getInstanceOf(StringTemplateGroup.java:392)  org.antlr.stringtemplate.StringTemplateGroup.getInstanceOf(StringTemplateGroup.java:404)  org.antlr.stringtemplate.StringTemplateGroup.lookupTemplate(StringTemplateGroup.java:484)  org.antlr.stringtemplate.StringTemplateGroup.getInstanceOf(StringTemplateGroup.java:392)  org.antlr.stringtemplate.StringTemplateGroup.getInstanceOf(StringTemplateGroup.java:404)  org.antlr.stringtemplate.StringTemplateGroup.lookupTemplate(StringTemplateGroup.java:484)  org.antlr.stringtemplate.StringTemplateGroup.getInstanceOf(StringTemplateGroup.java:392)  org.antlr.stringtemplate.StringTemplateGroup.getInstanceOf(StringTemplateGroup.java:404)  org.antlr.grammar.v2.CodeGenTreeWalker.getRuleElementST(CodeGenTreeWalker.java:152)  org.antlr.grammar.v2.CodeGenTreeWalker.atom(CodeGenTreeWalker.java:1986)  org.antlr.grammar.v2.CodeGenTreeWalker.element(CodeGenTreeWalker.java:1708)  org.antlr.grammar.v2.CodeGenTreeWalker.element(CodeGenTreeWalker.java:1556)  org.antlr.grammar.v2.CodeGenTreeWalker.alternative(CodeGenTreeWalker.java:1306)  org.antlr.grammar.v2.CodeGenTreeWalker.block(CodeGenTreeWalker.java:1081)  org.antlr.grammar.v2.CodeGenTreeWalker.ebnf(CodeGenTreeWalker.java:1871)  org.antlr.grammar.v2.CodeGenTreeWalker.element(CodeGenTreeWalker.java:1704)  org.antlr.grammar.v2.CodeGenTreeWalker.alternative(CodeGenTreeWalker.java:1306)  org.antlr.grammar.v2.CodeGenTreeWalker.block(CodeGenTreeWalker.java:1081)  org.antlr.grammar.v2.CodeGenTreeWalker.rule(CodeGenTreeWalker.java:797)  org.antlr.grammar.v2.CodeGenTreeWalker.rules(CodeGenTreeWalker.java:588)  org.antlr.grammar.v2.CodeGenTreeWalker.grammarSpec(CodeGenTreeWalker.java:530)  org.antlr.grammar.v2.CodeGenTreeWalker.grammar(CodeGenTreeWalker.java:336)  org.antlr.codegen.CodeGenerator.genRecognizer(CodeGenerator.java:432)  org.antlr.Tool.generateRecognizer(Tool.java:641)  org.antlr.Tool.process(Tool.java:454)  org.antlr.works.generate.CodeGenerate.generate(CodeGenerate.java:104)  org.antlr.works.generate.CodeGenerate.run(CodeGenerate.java:185)  java.lang.Thread.run(Unknown Source)  

And, i'm getting the following error:

[00:34:58] error(100): C:\Users\RCIX\Desktop\AguaLua\Project\trunk\AguaLua\AguaLua\ANTLR Data\Lua.g:0:0: syntax error: codegen: <AST>:0:0: unexpected end of subtree  

when attempting to generate Lua.g. Why am i getting these errors, and how can i fix them? (Using ANTLR V3, am able to provide grammar files)

Update: here is the grammar file i am trying to compile.

tree grammar LuaGrammar;    options {    backtrack=true;    language=CSharp2;    output=AST;    tokenVocab=Lua;    filter=true;    ASTLabelType=CommonTree;  }      assignment       :         ^('=' left=NAME right=NAME) {Ast. };  /*        chunk : (stat (';'!)?)* (laststat (';'!)?)?;    block : chunk;    stat :  varlist1 '='^ explist1 |       functioncall |       doblock |       'while'^ exp doblock |       'repeat'^ block untilrule |       'if'^ exp thenchunk elseifchunk* elsechunk? 'end'! |       'for'^ forinitializer doblock |       'for'^ namelist inlist doblock |       'function'^ funcname funcbody |       'local' 'function' NAME funcbody |       'local'^ namelist localstat? ;    localstat       :   '='^ explist1;    untilrule      :   'until'^ exp;    elseifchunk      :   'elseif'^ exp thenchunk;    thenchunk      :   'then'^ block;    elsechunk      :   'else'^ block;    forinitializer      :   NAME '='^ exp ','! exp (','! exp)?;    doblock      :   'do'^ block 'end'!;    inlist      :   'in'^ explist1;    laststat : 'return'^ (explist1)? | 'break';          dotname : '.'! funcname;    colonname      :   ':' NAME;    funcname :   NAME^ (dotname | colonname)?;          varlist1 : var (','! var)*;      namelist : NAME (','! NAME)*;    explist1 : (exp ','!)* exp;  */  /*  exp :  expelement (binop^ exp)* ;    expelement      :   ('nil' | 'false' | 'true' | number | stringrule | '...' | /*function |*\ prefixexp | tableconstructor | unop exp);  var: (namevar | dotvar | expvar | arrayvar)?;    namevar      :   NAME^ var;    dotvar      :   '.'! var;    expvar      :   '('^ exp ')'! var;    arrayvar      :   '['^ var ']'! var;    varSuffix: nameAndArgs* ('[' exp ']' | '.' NAME);    prefixexp: varOrExp nameAndArgs*;    functioncall: varOrExp nameAndArgs+;    varOrExp: var | '('! exp ')'!;      nameAndArgs: (':' NAME)? argsrule;    argsrule :  '(' (explist1)? ')' | tableconstructor | stringrule ;    function : 'function' funcbody;    funcbody :  funcparams funcblock;    funcblock      :   ')'^ block 'end'!;    funcparams      :   '('^ parlist1? ;    parlist1 : namelist (','! '...')? | '...';    tableconstructor : '{'^ (fieldlist)? '}'!;    fieldlist : field (fieldsep! field)* (fieldsep!)?;    field : '['! exp ']'! '='^ exp | NAME '='^ exp | exp;  */  fieldsep : ',' | ';';    binop : '+' | '-' | '*' | '/' | '^' | '%' | '..' |            '<' | '<=' | '>' | '>=' | '==' | '~=' |            'and' | 'or';    unop : '-' | 'not' | '#';    number : INT | FLOAT | EXP | HEX;    stringrule  : NORMALSTRING | CHARSTRING | LONGSTRING;  

Lua.g:

/*   * Lua 5.1 grammar   *    * Nicolai Mainiero   * May 2007   *    * This is a Lua (http://www.lua.org) grammar for the version 5.1 for ANTLR 3.   * I tested it with basic and extended examples and it worked fine. It is also used   * for LunarEclipse (http://lunareclipse.sf.net) a Lua editor based on Eclipse.   *    * Thanks to Johannes Luber and Gavin Lambert who helped me with some mutually left recursion.   *     */    grammar Lua;    options {    backtrack=true;    language=CSharp2;    //output=AST;    //ASTLabelType=CommonTree;  }  @lexer::namespace{AguaLua}      chunk : (stat (';'!)?)* (laststat (';'!)?)?;    block : chunk;    stat :  varlist1 '='^ explist1 |       functioncall |       doblock |       'while'^ exp doblock |       'repeat'^ block untilrule |       'if'^ exp thenchunk elseifchunk* elsechunk? 'end'! |       'for'^ forinitializer doblock |       'for'^ namelist inlist doblock |       'function'^ funcname funcbody |       'local' 'function' NAME funcbody |       'local'^ namelist localstat? ;    localstat       :   '='^ explist1;    untilrule      :   'until'^ exp;    elseifchunk      :   'elseif'^ exp thenchunk;    thenchunk      :   'then'^ block;    elsechunk      :   'else'^ block;    forinitializer      :   NAME '='^ exp ','! exp (','! exp)?;    doblock      :   'do'^ block 'end'!;    inlist      :   'in'^ explist1;    laststat : 'return'^ (explist1)? | 'break';          dotname : '.'! funcname;    colonname      :   ':' NAME;    funcname :   NAME^ (dotname | colonname)?;          varlist1 : var (','! var)*;      namelist : NAME (','! NAME)*;    explist1 : (exp ','!)* exp;    exp :  expelement (binop^ exp)* ;    expelement      :   ('nil' | 'false' | 'true' | number | stringrule | '...' | function | prefixexp | tableconstructor | unop exp);    var: (namevar | dotvar | expvar | arrayvar)?;    namevar      :   NAME^ var;    dotvar      :   '.'! var;  expvar      :   '('^ exp ')'! var;  arrayvar      :   '['^ var ']'! var;    varSuffix: nameAndArgs* ('[' exp ']' | '.' NAME);    prefixexp: varOrExp nameAndArgs*;    functioncall: varOrExp nameAndArgs+;    varOrExp: var | '('! exp ')'!;    nameAndArgs: (':' NAME)? argsrule;    argsrule :  '(' (explist1)? ')' | tableconstructor | stringrule ;    function : 'function' funcbody;    funcbody :  funcparams funcblock;    funcblock      :   ')'^ block 'end'!;    funcparams      :   '('^ parlist1? ;    parlist1 : namelist (','! '...')? | '...';    tableconstructor : '{'^ (fieldlist)? '}'!;    fieldlist : field (fieldsep! field)* (fieldsep!)?;    field : '['! exp ']'! '='^ exp | NAME '='^ exp | exp;    fieldsep : ',' | ';';    binop : '+' | '-' | '*' | '/' | '^' | '%' | '..' |            '<' | '<=' | '>' | '>=' | '==' | '~=' |            'and' | 'or';    unop : '-' | 'not' | '#';    number : INT | FLOAT | EXP | HEX;    stringrule  : NORMALSTRING | CHARSTRING | LONGSTRING;    // LEXER    NAME    :('a'..'z'|'A'..'Z'|'_')(options{greedy=true;}: 'a'..'z'|'A'..'Z'|'_'|'0'..'9')*      ;    INT : ('0'..'9')+;    FLOAT   :INT '.' INT ;    EXP : (INT| FLOAT) ('E'|'e') ('-')? INT;    HEX :'0x' ('0'..'9'| 'a'..'f')+ ;    NORMALSTRING      :  '"' ( EscapeSequence | ~('\\'|'"') )* '"'       ;    CHARSTRING     :    '\'' ( EscapeSequence | ~('\''|'\\') )* '\''     ;    LONGSTRING      :   '['('=')*'[' ( EscapeSequence | ~('\\'|']') )* ']'('=')*']'      ;    fragment  EscapeSequence      :   '\\' ('b'|'t'|'n'|'f'|'r'|'\"'|'\''|'\\')      |   UnicodeEscape      |   OctalEscape      ;    fragment  OctalEscape      :   '\\' ('0'..'3') ('0'..'7') ('0'..'7')      |   '\\' ('0'..'7') ('0'..'7')      |   '\\' ('0'..'7')      ;    fragment  UnicodeEscape      :   '\\' 'u' HexDigit HexDigit HexDigit HexDigit      ;    fragment  HexDigit : ('0'..'9'|'a'..'f'|'A'..'F') ;      COMMENT      :   '--[[' ( options {greedy=false;} : . )* ']]' {Skip();}      ;    LINE_COMMENT  : '--' (~ NEWLINE)* {Skip();};  fragment NEWLINE : '\r'|'\n' | '\r\n' ;    WS  :  (' '|'\t'|'\u000C') {Skip();};  

(both are based off of a grammar produced by Nicolai Mainero and available at ANTLR's site, Lua 5.1 grammar) If i uncomment anymore than this, it comes up with the error above.


Solution:1

Okay, a 'Can't find template ruleRefBang.st' has something to do with the illegal use of a "tree exclude" operator: !. Usually, it is a contradicting rewrite rule: somewhere you have a ! and then rewrite it using -> but use that ignored token anyway. Since I cannot see a -> in your grammar, that can't be the case (unless you simplified the tree grammar to post here and removed some rewrite rules?).

Anyway, I'd start by removing all ! operators in your tree grammar and if your grammar then works put them, one by one, back in again. Then you should be able to pin point the place in your grammar that houses the illegal !.

Good luck!


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »