[antlr-interest] Getting text from variable length matches?

Anakreon Mejdi amejdi at ertonline.gr
Mon Dec 16 01:36:46 PST 2002


If rules receive paramaters in ANTLR (I think they do I would pass a 
StringBuffer object as parammeter.A String wouldn't do because is 
imunable,You can't modify a String)
In java I would do:
  rule2 returns [StringBuffer match] <=Make this parameter
: l3:LEXER_TOKEN_B {match.append(l3.getText();} (l4:LEXER_TOKEN_C 
{match.append(l4.getText();})?
       | l5:LEXER_TOKEN_B {match.append(l5.getText();}(l6:LEXER_TOKEN_D 
{match.append(l6.getText();})*

  rule1 returns [String final_match]
       { StringBuffer sb = new StringBuffer(); }
	: l1:LEXER_TOKEN_A (rule2[sb])+ l2:LEXER_TOKEN_A
sb would contain the string matched by rule2.
This is just the idea.
The implementation should be quite different.

elias_biris wrote:
> I am sure that this has been discussed in the past, but my searches in
> the archives and the faq did not come up with a quick response:
> 
> In my C++ parser I have rules that match 1+ or 0+ or 0/1 things ie
> 
> rule1 returns [string final_match]
>      : l1:LEXER_TOKEN_A (rule2)+ l2:LEXER_TOKEN_A
> 
> rule2 returns [string match]
>      : l3:LEXER_TOKEN_B (l4:LEXER_TOKEN_C)? 
>      | l5:LEXER_TOKEN_B (l6:LEXER_TOKEN_D)*
> 
> 
> Ambiguities and antlr-syntax errors aside (the above just give a
> flavor of the thing I want to do, they do not make a syntacticaly
> correct grammar spec), how can I get the strings matched by the ()+,
> ()*, or ()? patterns above? Do I have to build the strings using the
> labels for handles to getText(), or is there an  easier (more direct)
> way to do this? I am doing this all in C++ but I would be open to
> suggestions from Java people if they have any tips.
> 
> All tips will be gratefully appreciated :-)
> 
> Kind regards
> 
> Elias
>       
> 
> 
>  
> 
> Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ 
> 
> 



 

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ 



More information about the antlr-interest mailing list