[antlr-interest] Semantic Analysis via TreeWalker

Micha micha-1 at fantasymail.de
Wed May 6 15:30:48 PDT 2009


On Wednesday 06 May 2009 23:05:09 Tilman Bender wrote:
>
> 1. How can I throw a detailed Error Message saying something like "The
> operator + is invald for the types of "+ $a.returntype + " and "+
> $b.returntype

either throw an exception (in your if-statements) or print to stderr (or use 
java logging) to print the error (your if statements catch the incorrect 
combinations)

> 2. How can I modifiy the tree returned by $a so that I can replace
> every token like INT:3 with a token of FLOAT:3.0.  So that (+ 3 4.0)
> becomes (+ 3.0 4.0)

I haven't done this with antlr, but in this case just generate a new tree as 
output (OUTPUT= AST) of the pass over the first tree, even if only few parts of 
tree actually changes. After that you have a tree with casting inserted.
I don't know if a treeparser can generate a new tree, but more than one passes 
are common.

 Michael



More information about the antlr-interest mailing list