%{ #include #include #include "lingo.tab.h" int linecount = 1; int errors; %} white [ \t] letter [A-Za-z] digit [0-9] hex {digit}|[a-fA-F] valid_char [^\n\"\'\\]|\\n|\\t|\\\'|\\\"|\\\\|\\{hex}{2} invalid_char \\[^nt\'\"\\] startcom "(*" endcom "*)" identifier {letter}({letter}|{digit})* number {digit}+ %x COMMENT %% {white}* { /* white space. Do nothing */ } \n { linecount++; } byte { return BYTE; } delete { return DELETE; } else { return ELSE; } false { return FALSE; } if { return IF; } int { return INT; } new { return NEW; } null { return NULL_WORD; } proc { return PROC; } reference { return REFERENCE; } return { return RETURN; } while { return WHILE; } with { return WITH; } true { return TRUE; } "+" { return '+'; } "-" { return '-'; } "*" { return '*'; } "/" { return '/'; } "%" { return '%'; } "=" { return '='; } "==" { return EQUAL; } "!=" { return NOTEQUAL; } ">=" { return GREQUAL; } "<=" { return LEEQUAL; } ">" { return '>'; } "<" { return '<'; } "|" { return '|'; } "&" { return '&'; } "!" { return '!'; } "(" { return '('; } ")" { return ')'; } "[" { return '['; } "]" { return ']'; } "}" { return '}'; } "{" { return '{'; } "," { return ','; } ";" { return ';'; } ":" { return ':'; } {number} { if (atol(yytext)<=32768) return NUMBER; errors++; return INVALID_NUMBER; } \'{valid_char}?\' { return CONST_BYTE; } \"{valid_char}*\" { return CONST_STRING; } \"({valid_char}|{invalid_char})+\" { errors++; return INVALID_STRING; } \"|\' { errors++; return UNMATCHED_QUOTE; } {identifier} { return IDENTIFIER;} {digit}({digit}|{letter})* { errors++; return ILLEGAL_IDENTIFIER; } {startcom} { BEGIN(COMMENT); } {endcom} { BEGIN(INITIAL); } \n { linecount++; } {startcom} { errors++; return NESTED_COMMENT; } <> { errors++; return NO_END_OF_COMMENT; } . { /* inside comment. Do nothing */ } {endcom} { errors++; return NO_START_OF_COMMENT; } . { errors++; return GENERIC_ERROR; } %% /* Gia th fash ths syntaktikhs analyshs h main 9a anaptyx9ei ek neou sto arxeio prodiagrafwn tou syntaktikou analyth. Edw thn exw afhsei ypo morfh sxoliwn gia na deite pws htan h main toy lektikou analyth */ int main(int argc, char *argv[]) { char buffer[80]; int token=0; FILE *in_file; argc--; argv++; if (!argc) { sprintf(buffer,"Dose onoma arxeioy\n"); write(1,buffer,strlen(buffer)); return 1; } if ( (in_file = fopen(argv[0],"r")) == NULL ) { perror("Error opening file"); return 1; } yyrestart(in_file); while ( ( token = yylex() ) != 0 ) { if (token == -4) { sprintf(buffer,"No end of comment\n"); write(1,buffer,strlen(buffer)); return 1; } sprintf(buffer,"(%d)Token %d : %s\n",linecount,token,yytext); write(1,buffer,strlen(buffer)); } sprintf(buffer,"Arithmos grammon : %d\n",linecount); write(1,buffer,strlen(buffer)); sprintf(buffer,"Arithmos lathon : %d\n",errors); write(1,buffer,strlen(buffer)); return 0; }