[antlr-interest] AST->Template: Not Getting It

Martijn Reuvers martijn.reuvers at gmail.com
Thu May 14 11:29:30 PDT 2009


For the list... I always lose it in a conversation. :S
------


Hi Christopher,

Sorry I read too fast, you were on the right track. Here is a simple
example, which compiles and runs fine (change the Java stuff to C#
where needed), I am not sure if this is the best way to do this as I
am not very familiar with stringtemplating either.

Following 4 files, parser grammar, tree grammer, string template file
and Test file (all put them in the same directory), don't forget to
change things for C#. Once you run it, you can enter assignments: int
a = 10; int b = 100; etc. At the end it prints the templates for all
these statements. Goodluck!

Martijn

Example.g
---------------------------------
grammar Example;

options {
       output=AST;
}

tokens {
       VARDEF;
}

start
       : assignment* -> assignment*
       ;

assignment
       : INT_TYPE VAR '=' INT ';' -> ^(VARDEF INT_TYPE VAR INT)
       ;


INT_TYPE
       :       'int'
       ;

INT
       :       ('0' .. '9')+
       ;

VAR
       :       ('a' .. 'z' | '0' .. '9')+
       ;

WS
       :       (' '|'\t'|'\n'|'\r')+ { skip(); }
       ;

-------------------------------------------
ExampleTree.g
-------------------------------------------
tree grammar ExampleTree;

options {
       tokenVocab=Example;
       ASTLabelType=CommonTree;
       output=template;
}

start returns [java.util.List values]
@init {
       $values = new java.util.ArrayList();
}
       : a+=assignment* { $values.add($a); }
       ;

assignment
       :       ^(VARDEF INT_TYPE VAR INT) ->
assign(type={$INT_TYPE.text},name={$VAR.text},val={$INT.text})
       ;

--------------------------------------------
ExampleTree.stg
--------------------------------------------
group ExampleTree;
assign(type,name,val) ::= "<type> <name> = <val>;"

--------------------------------------------
Test.java
--------------------------------------------

import java.io.FileReader;
import org.antlr.runtime.ANTLRInputStream;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.tree.CommonTree;
import org.antlr.runtime.tree.CommonTreeNodeStream;
import org.antlr.stringtemplate.StringTemplateGroup;

/**
 *
 * @author mreuvers
 */
public class Test {

 public static void main(String[] args) throws Exception {
   ANTLRInputStream input = new ANTLRInputStream(System.in);

   ExampleLexer lexer = new ExampleLexer(input);
   CommonTokenStream tokens = new CommonTokenStream(lexer);

   ExampleParser parser = new ExampleParser(tokens);
   CommonTree tree = (CommonTree) parser.start().getTree();
   CommonTreeNodeStream treeStream = new CommonTreeNodeStream(tree);
   treeStream.setTokenStream(tokens);

   ExampleTree eval = new ExampleTree(treeStream);

   FileReader f = new FileReader("ExampleTree.stg");
   StringTemplateGroup templates = new StringTemplateGroup(f);
   f.close();
   eval.setTemplateLib(templates);

   System.out.println("TEST="  + eval.start().values);
       }
}

--------------------------------------------


More information about the antlr-interest mailing list