[antlr-interest] Nested structures

John B. Brodie jbb at acm.org
Mon Mar 22 17:44:19 PDT 2010


Greetings!
On Tue, 2010-03-23 at 01:03 +0100, Stephanie wrote:
> Hi,
> 
> As I understood, I can use syntactic predicates to deal with non-regular
> structures. Unfortunately, I don't get it right. Here is an excerpt of my
> grammar (without syntactic predicates):
> 
> type
> : moldType
> | setType
> ;
> 
> moldType
> : 'Mold' '<' Identifier '>'
> ;
> 
> setType
> : 'Set' '<' Identifier '>'
> | 'Set' '<' pairType  '>'
> ;
> 
> pairType
> : 'Pair' '<' ( Identifier | pairType ) ',' ( Identifier | pairType ) '>'
> ;
> 
> In my grammar, type is used as part of variable / method declarations. So,
> type declarations can be: Mold<X>, Set<X>, Set<Pair<X,X>>,
> Set<Pair<X,Pair<X,X>>>, ...
> 
> I get an "non-LL(*) decision error" reported. I assume the problem is due to
> the recursion in pairType. How can I fix this grammar?
> 

I tried your above fragment (see attached) and experienced no problems.
Something else in your grammar is apparently the cause of your issue.

Hope this helps.
   -jbb

-------------- next part --------------
grammar Test;

options {
   output = AST;
   ASTLabelType = CommonTree;
}

@members {
   private static final String [] x = new String[] {
      "Mold<id>"
   };

   public static void main(String [] args) {
      for( int i = 0; i < x.length; ++i ) {
         try {
            System.out.println("about to parse:`"+x[i]+"`");
            TestLexer lexer = new TestLexer(new ANTLRStringStream(x[i]));
            CommonTokenStream tokens = new CommonTokenStream(lexer);

            TestParser parser = new TestParser(tokens);
            TestParser.start_return p_result = parser.start();

            CommonTree ast = p_result.tree;
            if( ast == null ) {
               System.out.println("resultant tree: is NULL");
            } else {
               System.out.println("resultant tree: " + ast.toStringTree());
            }
            System.out.println();
         } catch(Exception e) {
            e.printStackTrace();
         }
      }
   }
}

start : type EOF!;

type
: moldType
| setType
;

moldType
: 'Mold' '<' Identifier '>'
;

setType
: 'Set' '<' Identifier '>'
| 'Set' '<' pairType  '>'
;

pairType
: 'Pair' '<' ( Identifier | pairType ) ',' ( Identifier | pairType ) '>'
;

Identifier : 'id' ;


More information about the antlr-interest mailing list