Preserve C/C++ comments in ANTLR v3

Amal <akhailtash@gmail.com>
Sun, 6 Jan 2008 18:02:27 -0500 (EST)

          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: Amal <akhailtash@gmail.com>
Newsgroups: comp.compilers
Date: Sun, 6 Jan 2008 18:02:27 -0500 (EST)
Organization: Compilers Central
Keywords: C, lex
Posted-Date: 06 Jan 2008 18:02:27 EST

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):


    // Single-line comments
    SL_COMMENT
        : '//' ( ~('\r'|'\n')* ) '\r'? '\n' {skip();}
        ;


    // Multi-line comments
    ML_COMMENT
        : '/*' (options {greedy=false;} : .)* '*/' {skip();}
        ;


Basically I need something like this:


    comment : sl_comment | ml_comment ;


    sl_comment : '//' COMMENT_LINE ;


    ml_comment : '/*' COMMENT_TEXT '*/' ;


How do I define COMMENT_LINE and COMMENT_TEXT? Especially if I am
ignoring
whitespace, it becomes more confusing.


Any hints or pointers would be appreciated,
-- Amal



Post a followup to this message

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