[antlr-interest] Tree grammar doesn't match what I want

Andrew House ahouse at eecg.toronto.edu
Sat Oct 6 12:26:27 PDT 2012


Hi all,

I am working on my own language application, and am following the 
Language Implementation Patterns and Definitive ANTLR Reference books as 
closely as I can.  I have run into an issue where my tree grammar seems 
unable to match my 'array' subtree (representing an array data type).

An example of what fails to match is as follows:
(array (SIZE (EXPR (to 1 (IDX N)))) (SCALAR (NAME (TYPEID Particle))))

Below, I have included relevant snippets of the grammar and tree 
grammar.  The tree grammar has no trouble recognizing the 'tuple' and 
'scalar' data type subtrees.

I know that the grammar allows for arrays of arrays (of arrays...) if I 
were so inclined, but I don't think that's the culprit.  As far as I can 
tell the grammar is unambiguous.  It just doesn't seem to match for arrays.

Any help would be greatly appreciated---I've been looking at this for 
several days now, and haven't managed to see the problem.

Sincerely,
-- Andrew House

Grammar ======================================================

varDeclaration
     :    'var' ID 'is' dataType ';'
         -> ^('var' ^(NAME ID) ^(VARTYPE dataType) )
     ;

dataType
     :    dataTypeID sizeDef?-> ^(SCALAR ^(NAME dataTypeID) sizeDef? )
     |    'array' arrayDims 'of' dataType -> ^('array' arrayDims dataType )
     |    'tuple' '(' argList ')' -> ^('tuple' argList)
     ;

argList
     :    argDeclaration (',' argDeclaration)* -> argDeclaration+
     ;

argDeclaration
     :    dataType ID -> ^(ARG ^(NAME ID) ^(VARTYPE dataType))
     ;

arrayDims
     :    sizeDef | regionDef
     ;

dataTypeID
     :    ID ('.' ID)* -> ^(TYPEID ID+)
     ;

dataTypeList
     :    dataType (',' dataType)* -> dataType+
     ;

sizeDef
     :    '[' expr ']' -> ^(SIZE expr)
     ;

regionDef
     :    '[' idList 'in' expr ']' -> ^(REGION expr idList)
     ;

Tree Grammar 
====================================================================

varDeclaration
     :    ^('var' ^(NAME ID) ^(VARTYPE dt=dataType) .*)
         {
             // Code to resolve data type
         }
     ;


dataType returns [ASymbol type, AIndexRange size] // updated for resolution
     :    (tsid=scalarDataType | taid=arrayDataType | ttid=tupleDataType )
         {
             // Code to determine whether it is tuple, scalar, or array 
type,
       // and pass up appropriate values.
         }
     ;

scalarDataType returns [ASymbol type, AIndexRange size]
     :    ^(SCALAR ^(NAME ^(TYPEID name+=ID+)) sz=sizeDef?)
         {
             // Resolve scalar data types
         }
     ;

arrayDataType returns [ASymbol type, AIndexRange size]
     :    ^(aid='array' dim=arrayDims dt=dataType)
         {
           // Resolve array dimensions and subtype
       // THIS NEVER SEEMS TO MATCH ANYTHING!!!!!!
            // A print statement in here never executes.
         }
     ;

tupleDataType returns [ASymbol type, AIndexRange size]
     :    ^(tid='tuple' .*)
         {
             // Returns appropriate tuple values
         }
     ;

arrayDims returns [AIndexRegion region]
     :    sz=sizeDef | rg=regionDef
         {
             // Figures out if it is a size or region
       // and returns a region.
         }
     ;

sizeDef returns [AIndexRange range]
     :    ^(SIZE ex=exprRoot)
     ;

regionDef returns [AIndexRegion region]
     :    ^(REGION ex=exprRoot ids+=ID+)
     ;


More information about the antlr-interest mailing list