- %{
- int id_count = 0, keyword_count = 0, operator_count = 0, number_count = 0;
- %}
- identifier [a-zA-Z_][a-zA-Z0-9_]*
- keyword "auto"|"break"|"case"|"char"|"const"|"continue"|"default"|"do"|"double"|"else"|"enum"|"extern"|"float"|"for"|"goto"|"if"|"int"|"long"|"register"|"return"|"short"|"signed"|"sizeof"|"static"|"struct"|"switch"|"typedef"|"union"|"unsigned"|"void"|"volatile"|"while"
- operator "+"|"-"|"*"|"/"|"="|"=="|"!="|"<"|">"|"<="|">="|"&&"|"||"|"!"|"++"|"--"|"&"|"|"|"^"|"~"|"<<"|">>"|"["|"]"|"("|")"|";"|","|"."|"->"|"?"|":"|"{"|"}"
- number [0-9]+
- %%
- {identifier} { id_count++; }
- {keyword} { keyword_count++; }
- {operator} { operator_count++; }
- {number} { number_count++; }
- .|\n { /* Ignore other characters */ }
- %%
- int main() {
- yylex();
- printf("Identifiers: %d\n", id_count);
- printf("Keywords: %d\n", keyword_count);
- printf("Operators: %d\n", operator_count);
- printf("Numbers: %d\n", number_count);
- return 0;
- }
- int yywrap() {
- return 1;
- }
[text] github
Viewer
Editor
You can edit this paste and save as new: