%{
/* Program to recognize a C program */
#include <stdio.h>
#include <stdlib.h>
int COMMENT = 0;
int cnt = 0;
%}
identifier [a-zA-Z][a-zA-Z0-9]*
%%
#.* { printf("\n%s is a PREPROCESSOR DIRECTIVE", yytext); }
int
|float|char
|double
|while
|for
|do
|if
|break
|continue
|void
|switch
|case
|long
|struct
|const
|typedef
|return
|else
|goto
{ printf("\n\t%s is a KEYWORD", yytext); }
\/\* { COMMENT = 1; }
\*\/ { COMMENT = 0; cnt++; }
{identifier}\( { if (!COMMENT) printf("\n\nFUNCTION\n\t%s", yytext); }
\\{ { if (!COMMENT) printf("\nBLOCK BEGINS"); }
\\} { if (!COMMENT) printf("\nBLOCK ENDS"); }
{identifier}(\[[0-9]*\])? { if (!COMMENT) printf("\n%s IDENTIFIER", yytext); }
\".*\" { if (!COMMENT) printf("\n\t%s is a STRING", yytext); }
[0-9]+ { if (!COMMENT) printf("\n\t%s is a NUMBER", yytext); }
\)(\;)? { if (!COMMENT) printf("\n\t"); ECHO; printf("\n"); }
= { if (!COMMENT) printf("\n\t%s is an ASSIGNMENT OPERATOR", yytext); }
\<=|\>=|\<|==|\> { if (!COMMENT) printf("\n\t%s is a RELATIONAL OPERATOR", yytext); }
%%
int main(int argc, char **argv) {
if (argc > 1) {
FILE *file;
file = fopen(argv[1], "r");
if (!file) {
printf
("Could not
open %s\n", argv[1]); exit(0);
}
yyin = file;
}
yylex();
printf("\n\nTotal No. of comments are %d", cnt);
return 0;
}
int yywrap() {
return 1;
}
JXsKLyogUHJvZ3JhbSB0byByZWNvZ25pemUgYSBDIHByb2dyYW0gKi8KI2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KaW50IENPTU1FTlQgPSAwOwppbnQgY250ID0gMDsKJX0KCmlkZW50aWZpZXIgW2EtekEtWl1bYS16QS1aMC05XSoKCiUlCiMuKiB7IHByaW50ZigiXG4lcyBpcyBhIFBSRVBST0NFU1NPUiBESVJFQ1RJVkUiLCB5eX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