[antlr-interest] Problems customizing tree construction

Mathilde GUERIN mathilde.guerin at gmail.com
Wed Jul 30 00:51:45 PDT 2008


Hi,
I'm trying to write the grammar for the "Natural" Language
(http://en.wikipedia.org/wiki/Natural) and produce an AST.

Here's an example of a Natural program:
1 #FIELDA (A10)
1 #GROUPB
  2 #FIELDB-A (N10)
  2 #FIELDB-B (D)
1 #FIELDC (A15)
...
//The integer value represents the LEVEL of the variable to define the
data structure
//The (...) is used to define the FORMAT (type, length...) of the variable

To put it simply:
   - #FIELDA is a variable (level 1 : highest level)
   - #GROUPB is a group (level 1) composed of 2 variables //it can't
have a format... which is how i detect if this is a group or a
variable
       * #FIELDB-A (level 2): first variable of the group
       * #FIELDB-B (level 2): second variable of the group
   - #FIELDC is an other variable (level 1).

Here's a summary of what i've done so far:
//1°) Custom Node Classes
public abstract class ASTNode extends CommonTree;
public class ASTVariable extends ASTNode;
public class ASTGroup extends ASTNode;

//2°) Grammar sample:
grammar
    : (groupDefinition | varDefinition)+ ;
groupDefinition
    : INT ID endLine+ -> ^(ID<ASTGroup>[$INT.level]) ;
varDefinition
    : INT ID format endLine+ -> ^(ID<ASTVariable>[$INT.level] format) ;
...

//3°) produced AST:
ROOT <CommonTree>
  |-#FIELDA   (level=1)  <ASTVariable>
  |-#GROUPB   (level=1)  <ASTGroup>
  |-#FIELDB-A (level=2)  <ASTVariable>
  |-#FIELDA-B (level=2)  <ASTVariable>
  |-#FIELDC   (level=1)  <ASTVariable>
----------------------------------------------------------------

What i'd like to get is something like this:

ROOT
  |-#FIELDA        (level=1)  <ASTVariable>
  |-#GROUPB        (level=1)  <ASTGroup>
      |-#FIELDB-A  (level=2)  <ASTVariable>
      |-#FIELDA-B  (level=2)  <ASTVariable>
  |-#FIELDC        (level=1)  <ASTVariable>


My idea was to keep the ASTGroup node and the previous encountered
level as variables of the parser to use it to create the variable
node. Here's what I had in mind:
------------------------------------------------------------------------------------------------------

//-- ASTGroup creation --
/*generated code to create group node here...*/
// then initialize groupNode
groupNode = generatedNode;

/*...*/
//-- ASTVariable creation --
//if the variable level is lower than the group level,
if(groupNode != null && currentLevel < previousLevel){
   //then the variable parent is the group node
   adaptator.addChild(groupNode,varNode);
}
else {
   //otherwise, the variable is one of the root child nodes and
groupNode is reinitialized
   adaptator.addChild(root,varNode)
   groupNode = null;
}
------------------------------------------------------------------------------------------------------
However, i haven't found a way to keep the created group node as a
Parser variable... :/
I'm not even sure this is the right solution (or even if it'd work)
but I don't know how to create such an AST since the construction
depends on the level of variables.

If someone could help me, that'd be really great...

Thanks in advance

~MG~


More information about the antlr-interest mailing list