[antlr-interest] newbie grammar question

Jim Idle jimi at temporal-wave.com
Mon Jun 8 12:56:46 PDT 2009


Bruce Bauman wrote:
> I am trying to automate the translation of a huge base of Pascal source
> code from one dialect (MetaWare) to another (FreePascal).
>
> One change I need to make is to modify comments like this:
>
> -- This is a comment 
>
> To this
>
> // This is a comment
>
> My current rule looks like this:
>
> SL_COMMENT_2
>     :   '--' (options {greedy=false;} : . )* '\n'
>         {$channel=HIDDEN;}
>     ;
>
> What's the easiest way to modify the rule so that '--' is replaced by
> '//'?
>   
Easiest:

{ $text = '//' + $text.substring(2); }

But, you could also just do that when you come to actually sending out 
the tokens as a rewritten stream.

Jim


More information about the antlr-interest mailing list