/* K. Arvalis, A. Ntinakis, A. Poulis, P. Sarafis, 1996 */ %{ #include #include "token.h" %} int lineno=1; DIGIT [0-9] ID [a-zA-Z]([a-zA-Z0-9])*(_([a-zA-Z0-9])+)* %% {DIGIT}+ {tokenprint("INTCONST",yytext);} "'"(''|[^'\n])*"'" { cleanyytext(yytext); tokenprint("STRING",yytext); } "'"(''|[^'\n])*[\r\n] { int len=strlen(yytext); yytext[len-1]='\0'; printf("line %4i: ERROR at EOL Unterminated \"%s\"\n",lineno,yytext); } {ID} {int idnum; idnum=findsymbol(yytext); if (idnum != -1) tokenprint("RESERVED WORD",yytext); else tokenprint("IDENTIFIER",yytext); } "+" tokenprint ("PLUS" ,yytext); "-" tokenprint ("MINUS" ,yytext); "*" tokenprint ("MULTIPLY",yytext); "/" tokenprint ("DIVIDE" ,yytext); "<>" tokenprint ("NEQ" ,yytext); "<=" tokenprint ("LEQ" ,yytext); "<" tokenprint ("LES" ,yytext); ">=" tokenprint ("GE" ,yytext); ">" tokenprint ("GT" ,yytext); ":=" tokenprint ("ASSIGNOP",yytext); ":" tokenprint ("COLON" ,yytext); "=" tokenprint ("EQ" ,yytext); "," tokenprint ("COMMA" ,yytext); ";" tokenprint ("SEMI" ,yytext); "(" tokenprint ("OPENPAR" ,yytext); ")" tokenprint ("CLOSEPAR",yytext); "[" tokenprint ("LB" ,yytext); "]" tokenprint ("RB" ,yytext); "(*" { register int c; for(;;) { while((c=input()) != '*' && c!=EOF) if(c=='\n') lineno++; if (c=='*') { while((c=input())=='*'); if ( c==')' ) break; } if (c==EOF) { printf("line %4i: ERROR Unterminated comment at EOF\n",lineno); break; } } } [ \t] \n lineno++; . {printf("line %4i: LEXICAL ERROR,Unrecognized \"%s\"\n",lineno,yytext);} %% main(int argc, char **argv) { ++argv, --argc; if (argc>0) yyin=fopen( argv[0], "r"); /*Καθορισμός εισόδου είτε από αρχείο, είτε από την προκαθοριμένη είσοδο stdin*/ else yyin=stdin; yylex(); } yywrap() { }