[antlr-interest] Keeping AST state between parsing and walker

cristiano at defaveri.com.br cristiano at defaveri.com.br
Fri Aug 24 16:45:32 PDT 2012


  

Hi All, 

We are working on a language translator with antlr 3.x
and we divided our job in 3 passes (lexer/parsing, walker (tree grammar)
to perform some transformation and semantic checks and a generator to
write our final code) 

In order to perform semantic analysis, we
created heterogeneous AST nodes on phase 1 to keep additional
information depending on the token type. On the phase 2 (walker), we
would use that information to check some dependencies. Our doubt is how
could we keep these specific node on the construction of the AST of the
second phase ?  

We realized the dupNode() method from CommonTree
swallow the current node and create another one using the adaptor. One
solution we thought about is to override the adaptor and rewrite the
dupNode(Object), loading the necessary information to this new node.
Thus, since we have different node type, this would be a very hard work
do to.  

Does anyone know another solution or could share some design
you have used for semantic checking between phases ? 

Thank you in
advance. 

Example (phase1) : the findClause rewrites the FIND to node
to be of FindClause type. @after fills some information in the object of
FindClause type. 

... 

findClause 

@after { 

 registerFind
((CommonTree)$findClause.tree, $bo);  

 } 

 : FIND bindingObject (','
bindingObject)* -> ^(FIND["FIND"] bindingObject+)
 ; 

bindingObject
 :
(objectType ID*) 
 ; 

objectType 

 : PACKAGE | CLASS | ASPECT |
INTERFACE 

 ; 

resultsClause 

 : RESULTS ID* -> ^(RESULTS ID+) 

 ; 


This is the AST after parsing : 

Parser => (FIND (aspect a)) (results
a) 

--- WALKER --- 

... 

findClause 

 : ^(FIND bindingObject+)  

 ;


bindingObject 

 : ^(objectType ID*)  

 ; 

objectType 

 : PACKAGE |
CLASS | ASPECT | INTERFACE  

 ; 

resultsClause 

 : ^(RESULTS ID*)  


;  

--- FindClause class --- 

public class FindClause extends
CommonTree { 

 protected void registerBinding (List binding) {}  


public FindClause(Token token) { 

  this.token = token; 

 } 

 public
FindClause(int tType, String text) { 

  this(new CommonToken(tType,
text)); 

 } 

 public FindClause(CommonTree node) { 

  super(node); 


} 

 public Tree dupNode() {  

  return new FindClause(this);  

 }  


public String toString() {  

  return token.getText(); 

 } 

} 

  


More information about the antlr-interest mailing list