[antlr-interest] filter option in antlr3

Jean-Christophe Bach jeanchristophe.bach at inria.fr
Tue Aug 3 00:40:11 PDT 2010


Hi all,

My question was maybe unclear, or the answer obvious. But I admit that the use
of filter option is not very clear for me. I will try to rephrase my last email.

> 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.

I think my problem is very classic and standard methods must exist but it is not
clear for me. I only want to build a Tree composed of blocks where host code
blocks are not parsed but kept without any modification. Specific language
blocks are parsed and specific tree are built (my parser seems to work fine).
After compilation, these specific blocks will obviously translated into the same
language as the host code.
Is there anyone with an experience with this type of problem ? How do you handle
fuzzy parsing ? Is there any way to use the filter option in a combined grammar
or have I to split my file ?
If anyone has an idea, do not hesitate to give me a helping hand :)

Thanks in advance,
 
JC


More information about the antlr-interest mailing list