Search code examples
c#luaantlrtree-grammar

ANTLR - Writing a tree grammar for an AST


I have an AST outputted for some Lua code by my grammar file, which currently does parsing and lexing for me. I want to add a tree grammar to this, but since i'm using C# i'm not sure how to do it. What's the basic process for generating tree grammar code when you already have a parser and lexer written?

UPDATE: I have the following grammar file:

tree grammar LuaGrammar;

options {
  backtrack=true;
  language=CSharp2;
  //output=AST;
  tokenVocab=Lua;
  filter=true;
  ASTLabelType=CommonTree;
}
@lexer::namespace{/*my namespace*/}
@parser::namespace{/*my namespace*/}

dummyRule
    :   ^('=' x=. y=.) {};

placed in the same directory as my main grammar file, which generates fine. However, when trying to compile this i get the following errors:

[02:54:06] error(143): C:\Users\RCIX\Desktop\AguaLua\Project\trunk\AguaLua\AguaLua\ANTLR Data\LuaGrammar.g:12:18: unknown or invalid action scope for tree grammar: lexer
[02:54:06] error(143): C:\Users\RCIX\Desktop\AguaLua\Project\trunk\AguaLua\AguaLua\ANTLR Data\LuaGrammar.g:13:19: unknown or invalid action scope for tree grammar: parser

Am i on the right track or totally off?


Solution

  • I have not encountered this error but there are 2 things I would try.

    1) remove the @lexer and @parser namespace lines.

    2) If they are necessary then move them until after the Tokens {...} section of your grammar, ie just before the rules.