/* Scanner for a toy Pascal-C-like language * Exemple initialement pris des man-pages de lex (flex) * et detruit par felipe@dift2030 * * Date: 09/09/2002 * * Objet: illustrer ce que fait - en gros - un analyseur lexical * Note: Il ne vous est pas demande de savoir utliser lex !!! * * compilation: lex (ou flex) ift2030.lex * gcc -o essai lex.yy.c -lfl (ensuite) */ /* * ---------------------------- * Premiere Partie: definitions * ---------------------------- */ %{ // pas utile ici, mais illustratif #include %} DIGIT [0-9] LETTER [a-zA-Z] ID {LETTER}[{LETTER}{DIGIT}]* /* * ---------------------------- * Deuxieme Partie: les regles * ---------------------------- */ %% {DIGIT}+ printf( "LITERAL INT\t%s\n", yytext); {DIGIT}+"."{DIGIT}* printf( "LITERAL FLOAT\t%s\n", yytext); true|false printf( "LITERAL BOOLEEN\t %s\n", yytext ); \".*\" printf("CHAINE\t%s\n",yytext); = printf("AFFECTATION\n"); ; printf("SEPARATEUR INSTRUCTION\n"); , printf("SEPARATEUR DECLARATION\n"); \( printf("PARENTHESE OUVRANTE\n"); \) printf("PARENTHESE FERMANTE\n"); "/*" { int c; /* pris du man page de lex */ for ( ; ; ) { while ( (c = input()) != '*' && c != EOF ) ; /* eat up text of comment */ if ( c == '*' ) { while ( (c = input()) == '*' ) ; if ( c == '/' ) break; /* found the end */ } if ( c == EOF ) { error( "EOF in comment" ); break; } } } boolean|int|float printf( "TYPE \t %s\n", yytext ); if|then|else|begin|end|write|writeln|while printf( "KEYWORD\t %s\n", yytext ); \<|\>|==|\<=|\>= printf("COMPARATEUR\t%s\n", yytext); {ID} printf( "IDENTIFICATEUR\t%s\n", yytext ); "+"|"-"|"*"|"/" printf("OPERATEUR\t%s\n", yytext ); [ \t\n]+ /* eat up whitespace */ . printf( "Unrecognized character: %s\n", yytext ); %% /* * ------------------------------------- * Troisieme Partie: le code utilisateur * ------------------------------------- */ int main( int argc, char **argv ) { if ( argc > 0 ) yyin = fopen( argv[1], "r" ); else yyin = stdin; yylex(); }