[antlr-interest] Refactoring grammar

Omry Yadan omry at yadan.net
Mon Feb 12 08:59:56 PST 2007


here is a rule from my line parser.

the line parser identifies the type of the line, and also break it into 
components.

this part identifies the type of command this line represents, and keep 
the command text as a member:

command

    {String s = "";}
    :
    (
        "if" {m_type = TYPE_IF;m_text = "if";}|
        "ifdef" {m_type = TYPE_IFDEF;m_text="ifdef";}|
        "ifndef" {m_type = TYPE_IFNDEF;m_text="ifndef";}
    )
    s = cmd_args
    {
        m_text += s;
    };



is there a generic way to do it (I got about 8 more such rules) ?

I mean, something that will not require me to type each command twice.

I tried something like:


    (
        c:"if" {m_type = TYPE_IF;}|
        c:"ifdef" {m_type = TYPE_IFDEF;}|
        c:"ifndef" {m_type = TYPE_IFNDEF;}
    )
    {m_text=c.getText();}

But antlr complained that c is already defined (although the code it 
would have generated should have compiled fine).
any other ideas?



More information about the antlr-interest mailing list