%{ // // sample.l // // A simple demonstration of features in YooLex // // This part is put in the beginning of the generated C++ source file. // using namespace std; %} IDENT [A-Za-z]+ NUMBER [0-9]+ %option ccfile = "sample.cc" hhfile = "sample.hh" class = "DefaultLex" %x IDENT NUMBER %% %{ // // This part is put in the beginning of the yycase functions, which // is used for switching the accepted states when a string is accepted. // Since in C++ variables can be declared anywhere, the significance of // region is to basically setup certain variables each time upon the // entry of the lex class // %} {IDENT} { ECHO; cout << endl << "switching to IDENT" << endl; BEGIN (IDENT); } {NUMBER} { ECHO; cout << endl << "switching to NUMBER" << endl; BEGIN (NUMBER); } \n ++_lineNumber; . ; <> { cout << "Total lines: " << _lineNumber << endl; // do not have to call yyterminate } { {NUMBER} { ECHO; cout << endl << "switching to INITIAL" << endl; BEGIN (INITIAL); } {IDENT} cout << "."; \n ++_lineNumber; . ; } { {NUMBER} { ECHO; cout << endl << "switching to INITIAL" << endl; BEGIN (INITIAL); } <> { cerr << "Unexpected end of file" << endl; // // return a non-negative value to force the // scanner to generate a <> token again // not possible if the yylex calling routine // takes seriously the return value :) // BEGIN (INITIAL); return 0; } ^. ECHO; \n ++_lineNumber; . ; } %% // // This part is put in the end of the C++ source file // int main () { DefaultLex scanner; while (scanner.yyLex () >= 0) ; return 0; }