[antlr-interest] options greedy : getting the tokens consumed during the greedy match

Bart Kiers bkiers at gmail.com
Mon May 30 13:42:06 PDT 2011


Hi Vijay,

You could grab all matched text in the `@after` block using the `getText()`
method:

COMMENT
@init{
  boolean isJavaDoc = false;
  System.out.println("Entering comment");
}
@after {
  System.out.println("Leaving comment, matched: " + getText());
}
  :  '/*'
     {
       if((char)input.LA(1) == '*') {
         isJavaDoc = true;
       }
     }
     (options {greedy=false;} : . )*
     '*/'
  ;


Regards,

Bart Kiers.



On Mon, May 30, 2011 at 8:08 PM, Vijay Raj <call.vijayraj at yahoo.com> wrote:

> Hi -
>    I am trying to parse a given java file, with a code fragment that
> consumes comments as below: ( Code fragment got from Java.g , pasted in the
> antlr site, to give credit where it is due).
>
>
> COMMENT
>          @init{
>             boolean isJavaDoc = false;
>             System.out.println("Entering comment");
>         }
>     :   '/*'
>             {
>                 if((char)input.LA(1) == '*'){
>                     isJavaDoc = true;
>                 }
>             }
>         (options {greedy=false;} : . )*
>         '*/'
>   ...
>      ;
>
>
> I am trying to get all the characters mapped by the wildcard regex , as in
> 'options greedy' line in the grammar file and get the string into the Java
> world for further processing.   What hidden system variables/ grammar should
> I use to take care of the same ?
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe:
> http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>


More information about the antlr-interest mailing list