[antlr-interest] your Or ast question
John B. Brodie
jbb at acm.org
Thu Oct 2 19:22:54 PDT 2008
On Thursday 02 October 2008 09:34:11 pm Randall R Schulz wrote:
> On Thursday 02 October 2008 17:54, you wrote:
> > Mr. Schulz :-
>
> I'm sorry. My father isn't here right now...
I happen to believe that everyone deserves respect...
> > Sorry for this disconnected reply to your last message to me.
>
> It's OK. I hope you don't mind my sending the reply back to the list.
It is okay with me.
> I'm surprised this seems so difficult (not yet conceding
> impossibility!), so I think it would be generally interesting to have
> the whole list membership observe the attempts.
>
I do not think that your question is really all that difficult.
> > ...
> >
> > recapping from memory, you want
> >
> > p | q | r | s
> >
> > to be represented, essentially, as an ast of
> >
> > (| p q r s)
>
> Precisely.
>
> > ...
> >
> > maybe try this
> >
> > o : (a->a) ( ('|' a)+ -> ^('|' a+) )? ;
>
> The result is the same:
>
> (Or p (Or q (Or r (Or s))))
Not for me. I use the toStringTree method of the java CommonTree to obtain my
result - is that what you are using?
this grammar file, Test.g :
-----begin cut here ----- cut here ----- cut here
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 : (a->a) ( ('|' a)+ -> ^('|' a+) )? ;
a : 'p' | 'q' | 'r' | 's' ;
WS : ' '+ { $channel=HIDDEN; } ;
-----end cut here ----- cut here ----- cut here -----
using this command line in Ubuntu 8.04 Linux :
-----begin cut here ----- cut here ----- cut here
java org.antlr.Tool Test.g ; javac *.java ; java TestParser
-----end cut here ----- cut here ----- cut here -----
gets this result printed on the unix standard output device:
-----begin cut here ----- cut here ----- cut here
ANTLR Parser Generator Version 3.1 (August 12, 2008) 1989-2008
about to parse:`s`
resultant tree: s
about to parse:`p | q | r | s`
resultant tree: (| q r s)
-----end cut here ----- cut here ----- cut here -----
do you get anything similiar to the above?
still not what you want, but perhaps closer.
so, i think, we just need to work out an appropriate use of the ANTLR tree
building operator += on the Left Hand Side of the -> operator in order to
compose a complete list of sub-terms.
Does this seem reasonable?
Is that really so hard?
>....remainder of quoted message snipped....
More information about the antlr-interest
mailing list