Sunday, July 15, 2007

Lex Example: A C Comment Recognizer

The following is a lex program that will recognize a multi-line comment in C:

---Program Starts Here---

%option noyywrap

sc "/*"
ec "*/"
string \"[^"]*\"
inside [^*]|"*"[^/]|{string}
comment {sc}{inside}*{ec}

%%

{comment} { printf("<<<%s>>>", yytext); }
. { printf("%s", yytext); }

%%

int main ()
{
yylex();
return 0;
}


---Program Ends Here---

Note: Check your google group. I uploaded the flex and bison binary files.

No comments: