On Thursday 02 October 2008 09:43:44 pm Randall R Schulz wrote:
> Hi,
>
> Sorry for shouting, but I really would like a definitive answer to this
> question. ("Steamroller" referring to creating a flat AST.)
>
> If one is to parse an infix operator notation, exemplified by this
> input:
>
>       1 OP 2 OP 3 OP 4 OP 5
>
> and produce from it this AST:
>
>       (OP 1 2 3 4 5)
>
> How, if it is indeed possible, would one do this?
>
> It seems simple enough, but so far, no suggestion offered nor idea of my
> own has succeeded in producing anything other than one of these results:
>
> Result 1:
>       (OP 1 (OP 2 (OP 3 (OP 4 5))))
>
> Result 2:
>       (OP (OP (OP (OP 1 2) 3) 4) 5))))
>
> Result 3:
>
> Exception in thread "main"
> org.antlr.runtime.tree.RewriteEmptyStreamException: token OP
>
>
> Any definitive answers out there? Terence??
>
> Thanks.
>
>
> Randall Schulz
>
> List: http://www.antlr.org:8080/mailman/listinfo/antlr-interest
> Unsubscribe:
> http://www.antlr.org:8080/mailman/options/antlr-interest/your-email-address

Solution attached.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"il-antlr-interest" group.
To post to this group, send email to il-antlr-interest@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/il-antlr-interest?hl=en
-~----------~----~----~----~------~----~------~--~---

grammar Test;

options {
	output = AST;
	ASTLabelType = CommonTree;
}

@members {
    private static final String [] x = new String[]{
        "s", "p | q | r | s"
    };

    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 : o EOF! ;

o : (t+=a->a) ( ('|' t+=a)+ -> ^('|' $t+) )? ;

a : 'p' | 'q' | 'r' | 's' ;

WS : ' '+ { $channel=HIDDEN; } ;
List: http://www.antlr.org:8080/mailman/listinfo/antlr-interest
Unsubscribe: 
http://www.antlr.org:8080/mailman/options/antlr-interest/your-email-address

Reply via email to