Re: Preserve C/C++ comments in ANTLR v3

Joel Yliluoma <bisqwit@iki.fi>
17 Jan 2008 12:54:34 GMT

          From comp.compilers

Related articles
Preserve C/C++ comments in ANTLR v3 akhailtash@gmail.com (Amal) (2008-01-06)
Re: Preserve C/C++ comments in ANTLR v3 bisqwit@iki.fi (Joel Yliluoma) (2008-01-17)
| List of all articles for this month |
From: Joel Yliluoma <bisqwit@iki.fi>
Newsgroups: comp.compilers
Date: 17 Jan 2008 12:54:34 GMT
Organization: TDC Internet Services
References: 08-01-010
Keywords: parse
Posted-Date: 17 Jan 2008 08:53:20 EST

On Sun, 6 Jan 2008 18:02:27 -0500 (EST), Amal wrote:
> I need to extract comments from C/C++/Java source file. Normally
> comments are thrown away and they are defined as lexer rules
> (in ANTLR v3):


Is there a reason why you can't make the lexer capture the comments?
Then in the context where a comment may have some effect, check using
the parser action whether a comment was saved by the lexer.
If the comment always may have an action regardless of context, attach
the action to the lexer rule.


For example, in http://bisqwit.iki.fi/src/qbc.html, I used
the following lex rules (for QuickBasic):




\' { BEGIN(STATE_COMMENT); }


<STATE_COMMENT>{
    \$DYNAMIC {
        SetMetaDynamic(); /* Perform action */
        /* Don't return anything */
    }
    \$STATIC {
        SetMetaStatic(); /* Perform action */
        /* Don't return anything */
    }
    \$INCLUDE:\ *\' BEGIN(STATE_INCLUDE);
    \n+ BEGIN(INITIAL); Return CRLF;
    [^$\n]+ /* Ignore longest possible blobs until $ */
    . /* Ignore other */
}


And the parser remains oblivious to the fact that comments
were present, unless it chooses to check the status set by
those lexer actions.


--
Joel Yliluoma - http://iki.fi/bisqwit/


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.