Search code examples
cregexcompiler-constructionlexical-analysis

How to REGEX // in C? Single line comments


I used the following to get it to work partially:

        %{
        #define OR 2

        #define AND 3
        .........
        .........
        %}

        delim     [ \t]
        ws        {delim}*
        letter    [A-Za-z]
        digit     [0-9]
        comments  [/]+({letter}|{digit}|{delim})*

    %%

    {comments} {return(COMMENT);}
    ......................
    ......................    
    %%
int main()
{
    int tkn = 0;
    while (tkn = yylex())
     {
          switch (tkn)
          {

case COMMENT:
printf("GOT COMMENT");
          }
         }
}

This is working fine. The problem is that the regex obviously does not recognize special characters because [/]+({letter}|{digit}|{delim})* does not consider special characters. How to change the regex to accommodate more characters till end of line?


Solution

  • Couldn't you just use

    [/]+.*
    

    It will match some number of / and then anything till the end of line. Of course this will not cover comments like /* COMMENT */.