[antlr-interest] filter option in antlr3

Jean-Christophe Bach jeanchristophe.bach at inria.fr
Wed Jul 28 08:40:23 PDT 2010


Hello antlr users,

I am rewriting our parser and I have a question :

I have a HostParser and I call other subparsers when specific constructions are
detected. Then, I build a tree like this :
Program
  BlockList
    Construct1 ...  // sublanguage1 block
    Construct2 ...  // sublanguage2 block
    Construct1 ...
    Construct42 ... // sublanguage1 block
    ...
    HostBlock
    ...

A HostBlock is a block of host code (for instance Java). I do not want neither
to parse nor to modify it, I only want to keep it without any other treatment :
(BlockList
  (...)
  (HostBlock <big string representing a host code block> )
  (...)
)

In our previous antlr2 parser, we used the filter option in a combined grammar :

Lexer part :

options{
 filter=TARGET;
 }
...
protected
TARGET
  :
    ( . )
    {target.append($getText);}
  ;

where target is a StringBuffer.

Parser part :
 private String getCode() {
   String result = lexer.target.toString();
   lexer.clearTarget();
   return result;
 }

Then, we only called getCode() when building a HostBlock node.

Is it still possible with antlr3 ? I have noted a change, the option has to be
filter=true;
Since I am using a combined grammar (one file containing my parser and my
lexer), I am not sure how to use this option. How can I precise that it is a
lexer option ? (in global options, it does not work, because it applies only on
lexer). Do I have to add this option in the lexer rule ? (and how ?) :

fragment
TARGET : ( . ) {<my Java code>}

I tried to add the filter option in the rule, but I obtain errors, as if the
option did not exist.

Thanks in advance,

JC


More information about the antlr-interest mailing list