[antlr-interest] Number of children

Nazim Oztahtaci nazim_oztahtaci at hotmail.com
Mon Feb 15 04:42:10 PST 2010


Thanks for your answer. I will test your grammar also. Can you write a test string for that? I added that to my C# project successfuly, however I got errors even I try with (a AND b). I guess I should put a character to show that it is end of string. Error I receive is loop did not match... As I said, I just started Antlr couple of days ago so I apologize this is a simple error to explain. 
Apart from this I would like to paste the grammar I use: grammar 

nazimg;

options
{
  language = CSharp2;
  output = AST;
} 
LPAREN : '(' ;
RPAREN : ')' ;
AND : 'AND';
ANDNOT : 'ANDNOT';
ORNOT : 'ORNOT';
NOT     : 'NOT';
OR : 'OR';
WS :  ( ' ' | '\t' | '\r' | '\n') {$channel=HIDDEN;}  ;  
WORD :  (~( ' ' | '\t' | '\r' | '\n' | '(' | ')' ))*;  


expr :andexpr;
andexpr : orexpr (AND^ orexpr)* | orexpr (ANDNOT^ orexpr)*;
orexpr : atom (OR^ atom)* | atom (ORNOT^ atom)*;
atom : WORD | LPAREN! expr RPAREN! | WORD NOT;

I have fixed the tree structure problem which I asked prevously. I just made a small tree conversion so problem is solved. However now I try to add NOT operator also. So I defined NOT, ANDNOT and ORNOT operators. I thought that with just simple | action. I can make copy of expression like this:

andexpr : orexpr (AND^ orexpr)* | orexpr (ANDNOT^ orexpr)*; but output is not produced. The reason of why I use NOT is as I said, my expression will be like (a AND b)' and I will apply de-morgan. When I come to ANDNOT node, I will turn it to OR and also add NOT value to all its children.

Best regards & Im sorry for interruption
Nazim


> Date: Mon, 15 Feb 2010 14:12:06 +0200
> From: antlr at shmuelhome.mine.nu
> To: antlr-interest at antlr.org
> Subject: Re: [antlr-interest] Number of children
> 
> On 2/15/2010 10:22 AM, Nazim Oztahtaci wrote:
> > Hello,
> >
> > I have written a grammar based on an example on the Net to parse an expression like (a OR B)AND(C OR D) to a tree like
> >       AND
> > OR       OR
> > A B       C D
> > It works fine but if expression has more then 2 children, like (A OR B OR C)AND(D OR E OR F)
> > Then it creates problem because it does something like
> > AND
> >
> > D     OR
> >         E    F
> > I want it to be like
> >
> > OR
> > D  E  F
> >
> > In other words, a tree node might have capability to have more then 2 nodes as children. How can I set this? I work on C# but couldnt find any setter function in library.
> > Best regards,
> > Nazim
> >   		 	   		
> >    
> I am not sure if this is too simple for your needs but this allows a 
> more generic tree structure for your example.
> 
> grammar test;
> options
> {
>    output = AST;
> }
> expr : andexpr EOF;
> andexpr : orexpr (AND orexpr)+ -> ^(AND orexpr+);
> orexpr : WORD (OR WORD)+ -> ^(OR WORD+)
>      | LPAREN! orexpr RPAREN!;
> 
> LPAREN : '(' ;
> RPAREN : ')' ;
> AND : 'AND';
> OR : 'OR';
> WS :  ( ' ' | '\t' | '\r' | '\n') {$channel=HIDDEN;}  ;
> WORD :  (~( ' ' | '\t' | '\r' | '\n' | '(' | ')' ))*;
> 
> 
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
 		 	   		  
_________________________________________________________________
Windows Live Hotmail: Arkadaşlarınız Facebook'taki güncellemelerinizi doğrudan Hotmail®'den alır.
http://www.microsoft.com/windows/windowslive/see-it-in-action/social-network-basics.aspx?ocid=PID23461::T:WLMTAGL:ON:WL:tr-tr:SI_SB_4:092009


More information about the antlr-interest mailing list