[antlr-interest] Lexer grammar for filtering

Balazs Varnai bvarnai at gmail.com
Mon Oct 17 02:15:48 PDT 2011


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 ] **** */


More information about the antlr-interest mailing list