%{ #define GE 1 #define EQ 2 unsigned int line_no = 1; %} letter [a-zA-Z_] digit [0-9] letter_or_digit [a-zA-Z_0-9] eol \n other . %% ">=" { printf("GE at line %d\n", line_no); } "==" { printf("EQ at line %d\n", line_no); } {letter}{letter_or_digit}* { printf("ID %s at line %d %d \n", yytext,line_no,yylineno); } {digit}+ {printf("DIGIT %s at line %d\n", yytext,line_no);} {eol} {line_no++;} {other} {} %% main() { yylex(); }
%{ unsigned charCount = 0, wordCount = 0, lineCount = 0; %} word [^ \t\n]+ eol \n %% {word} {wordCount++; charCount+=yyleng;} {eol} {lineCount++; charCount++;} . {charCount++;} %% main() { yylex(); printf("Words=%d Line=%d Chars=%d\n",wordCount,lineCount,charCount); }