Flex source for parsing files with multiline comments

Discussion in 'C++' started by Uwe Ziegenhagen, Jul 18, 2003.

  1. Hello,

    my fellows and me implement a c++ tool that is able to divide blank/tab
    separated files into <number>, <text>, <c-singlelinecomment> and
    <multilinecomment>. So far it's not working bad, we have just one
    problem if I call the a.exe that gcc compiles with the following
    textfile (./a.exe < test.txt) he does not match the multiline comments
    correctly.

    *test.txt contains:

    123 456
    hello
    5674
    /* hello hello hellp
    something
    more */
    123

    *output of a.exe < test.txt is:

    Number
    Number

    Command

    Number

    hello hello hellp
    something
    more Comments
    Number

    That's bad the hellos should be suppressed, only "comments" should be
    returned.

    Any idea?

    UWe
    ################################## fourth.l ##############
    %{
    #define NUMBER 400
    #define COMMENT 401
    #define TEXT 402
    #define COMMAND 403
    #define schlump 405
    #define mlcomment 406

    /* <COMMENTS>\n {return mlcomment; }
    <COMMENTS>.\n {return mlcomment; }

    <COMMENTS>"*/"[ \t]*\n { BEGIN 0; return mlcomment;}
    <COMMENTS>"*/" { BEGIN 0; return mlcomment;}

    */
    %}

    %x COMMENTS

    %%
    [ \t]*"/*".*"*/"[ \t]*\n {return mlcomment; }
    [ \t]*"/*" { BEGIN COMMENTS;}

    <COMMENTS>.* "*/"[ \t]*\n { BEGIN 0; return mlcomment;}
    <COMMENTS>.*"*/" { BEGIN 0; return mlcomment;}

    [+-]?(([0-9]+)|([0-9]*\.[0-9]+)([eE][-+]?[0-9]+)?) {return NUMBER;}
    (\/\/)+[^\n]* {return COMMENT;}
    \"[^\"\n]*\" {return TEXT;}
    [0-9]+[a-zA-Z]+ {return schlump;}
    [a-zA-Z][a-zA-Z0-9]+ {return COMMAND;}
    .. ;
    ..*\n ;

    %%
    #include <stdio.h>

    int yywrap(){return 1;}

    main (argc,argv)
    int argc;
    char *argv[];

    {
    int val;

    //

    while (val = yylex()) {
    switch (val)
    {
    case 400 :
    {printf ("Number\n");break;}
    case 401 :
    {printf ("Comment\n");break;}
    case 402 :
    {printf ("Text\n");break;}
    case 403 :
    {printf ("Command\n");break;}
    case 405 :
    {printf ("Schlumpf\n");break;}
    case 406 :
    {printf ("Comments\n");break;}
    default :
    printf("etwas anderes\n");
    }
    }
    }
     
    Uwe Ziegenhagen, Jul 18, 2003
    #1
    1. Advertising

  2. Re: [OT] Flex source for parsing files with multiline comments

    "Uwe Ziegenhagen" <-berlin.de> wrote in message
    news:bf8lih$rdq$-berlin.de...
    | my fellows and me implement a c++ tool that is able to divide blank/tab
    | separated files into <number>, <text>, <c-singlelinecomment> and
    | <multilinecomment>. So far it's not working bad, we have just one
    | problem if I call the a.exe that gcc compiles with the following
    | textfile (./a.exe < test.txt) he does not match the multiline comments
    | correctly.
    NB: specific tools such as Flex are OT in this NG ... but I'll take on...

    | That's bad the hellos should be suppressed, only "comments" should be
    | returned.
    |
    | Any idea?

    I believe your multiline comment handling does not have to be that
    complicated.
    What about using a single rule such as:

    "/*"([^\*]|\*[^/])"*/" { return mlcomment; }

    hth,
    --
    Ivan Vecerina, Dr. med. <> http://www.post1.com/~ivec
    Brainbench MVP for C++ <> http://www.brainbench.com
     
    Ivan Vecerina, Jul 18, 2003
    #2
    1. Advertising

  3. Re: [OT] Flex source for parsing files with multiline comments

    Thank you very much, what is the best group for lex and bison stuff?


    UWe
     
    Uwe Ziegenhagen, Jul 18, 2003
    #3
  4. Uwe Ziegenhagen

    Jim Fischer Guest

    Re: [OT] Flex source for parsing files with multiline comments

    Uwe Ziegenhagen wrote:
    > Thank you very much, what is the best group for lex and bison stuff?


    I don't know of any newsgroups for these programs, but 'flex' and
    'bison' are GNU programs so you might take a look at the newsgroup

    gnu.utils.help

    FWIW, there are also mailing lists you can subscribe to -- i.e., you ask
    questions (and read/respond to other people's questions) via email
    rather than by posting messages to a newsgroup server. Visit the GNU
    'flex' and 'bison' web sites for more info:

    http://www.gnu.org/software/flex/flex.html
    http://www.gnu.org/software/bison/bison.html

    --
    Jim

    To reply by email, remove "link" and change "now.here" to "yahoo"
    jfischer_link5809{at}now.here.com
     
    Jim Fischer, Jul 18, 2003
    #4
  5. Re: [OT] Flex source for parsing files with multiline comments

    Uwe Ziegenhagen <-berlin.de> writes:

    > Thank you very much, what is the best group for lex and bison stuff?


    I'd try comp.compilers and comp.compilers.tools

    HTH & kind regards
    frank

    --
    Frank Schmitt
    4SC AG phone: +49 89 700763-0
    e-mail: frank DOT schmitt AT 4sc DOT com
     
    Frank Schmitt, Jul 22, 2003
    #5
  6. Uwe Ziegenhagen

    oceamus

    Joined:
    Jan 27, 2010
    Messages:
    1
    You wrote: "/*"([^\*]|\*[^/])"*/" { return mlcomment; }

    This will only allow comments of the form /*<single character>*/.

    I think you meant: "/*"([^\*]|\*[^/])*"*/"

    Cheers
     
    oceamus, Jan 27, 2010
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    1,183
  2. Stephane CHAZELAS

    Re: Stripping multiline C comments without using Lex

    Stephane CHAZELAS, Feb 4, 2004, in forum: C Programming
    Replies:
    3
    Views:
    960
    Jens Schweikhardt
    Feb 5, 2004
  3. Philippe C. Martin

    Re: Case Sensitive, Multiline Comments

    Philippe C. Martin, May 26, 2005, in forum: Python
    Replies:
    0
    Views:
    403
    Philippe C. Martin
    May 26, 2005
  4. dale zhang
    Replies:
    8
    Views:
    458
    Tintin
    Nov 30, 2004
  5. Replies:
    4
    Views:
    653
    Dr John Stockton
    Jun 3, 2006
Loading...

Share This Page