[antlr-interest] Lexer grammar for filtering

Balazs Varnai bvarnai at gmail.com
Mon Oct 17 02:34:05 PDT 2011


Hi Bart,

Thanks! Adding a WS* solved the problem.
I use the ELSE because it's kind of reverse filter, I specify what to
exclude from input to output.

Regards,
Balazs

On Mon, Oct 17, 2011 at 11:20 AM, Bart Kiers <bkiers at gmail.com> wrote:

> Hi Balazs,
>
> Since PC is not a parser rule, you need to account for the space(s) between
> 'PC_HASH_VALUE' and DIGIT.
> And since you've set `filter=true`, you don't need a fall-through rule
> ELSE, AFAIK.
>
> Regards,
>
> Bart.
>
>
> On Mon, Oct 17, 2011 at 11:15 AM, Balazs Varnai <bvarnai at gmail.com> wrote:
>
>> Hi All,
>>
>> I have a simple grammar to collapse white-spaces and comment from a c
>> source
>> code input. Also I would like to filter out some variables with a specific
>> name. These have a strict format, so no "real" C parsing needed.
>> Works fine but for example a line "#define PC_HASH_VALUE 1" is not
>> recognized. As far I remember from previous ANTLR usage, this was working
>> straight away. Any suggestions? Thanks!
>>
>> /* **** [ CODE ] **** */
>> lexer grammar Collapse;
>>
>> options {
>>  language = Java;
>>  filter = true;
>> }
>> @header {
>> package rewriter;
>> import java.util.*;
>> import java.io.*;
>>
>> }
>>
>> @members {
>> PrintStream out;
>>
>> public Collapse(CharStream input, PrintStream out) {
>>    this(input);
>>    this.out = out;
>> }
>> }
>>
>> PC: 'PC_HASH_VALUE' text=DIGIT {$channel=HIDDEN;};
>>
>> fragment
>> DIGIT: '0'..'9';
>>
>> COMMENT
>>    :   '//' ~('\n'|'\r')* '\r'? '\n' {$channel=HIDDEN;}
>>    |   '/*' ( options {greedy=false;} : . )* '*/' {$channel=HIDDEN;}
>>    ;
>>
>> WS  :   ( ' '
>>        | '\t'
>>        | '\r'
>>        | '\n'
>>        ) {$channel=HIDDEN;}
>>    ;
>>
>> ELSE : c=. {out.print((char)$c);} ; // match any char and emit
>> /* **** [ END ] **** */
>>
>> 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