-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscan.l
78 lines (76 loc) · 2.51 KB
/
scan.l
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
%{
#include <string.h>
#include "attr.h"
#include "parse.tab.h"
#define YY_SKIP_YYWRAP
%}
extern YYSTYPE yylval;
extern int yywrap() {return 1;};
extern int isatty();
int lines = 1;
D [0-9]
id [A-Za-z][A-Za-z0-9]*
qstring \'[^\'\n]*[\'\n]
%%
{D}+ {ECHO;
yylval.token.num = atoi(yytext);
return(ICONST);}
:= {ECHO;return(ASG);}
"==" {ECHO;return(EQ);}
"!=" {ECHO;return(NEQ);}
"<=" {ECHO;return(LEQ);}
"<" {ECHO;return(LT);}
">=" {ECHO;return(GEQ);}
">" {ECHO;return(GT);}
\. {ECHO;return(PERIOD);}
"/*" {
int c1 = 0; int c2 = input();
ECHO;
for(;;) {
if (c2 == EOF) {
printf("ERROR: EOF detected in comment\n");
yyterminate();
}
fprintf(yyout,"%c",c2);
if (c1 == '*' && c2== '/') break;
c1 = c2;
c2 = input();
if (c1 == '\n' && c2 != EOF)
{lines++; fprintf(yyout,"%d\t",lines);}
}
}
[\[\]*+(),:;=-] {ECHO;return(yytext[0]);}
program {ECHO;return(PROG);}
var {ECHO;return(VAR);}
array {ECHO;return(ARRAY);}
integer {ECHO;return(INT);}
boolean {ECHO;return(BOOL);}
begin {ECHO;return(BEG);}
end {ECHO;return(END);}
print {ECHO;return(PRINT);}
while {ECHO;return(WHILE);}
do {ECHO;return(DO);}
if {ECHO;return(IF);}
then {ECHO;return(THEN);}
else {ECHO;return(ELSE);}
and {ECHO;return(AND);}
or {ECHO;return(OR);}
of {ECHO;return(OF);}
true {ECHO;return(TRUE);}
false {ECHO;return(FALSE);}
{id} {ECHO;
yylval.token.str = (char *) malloc(strlen(yytext)+1);
strcpy(yylval.token.str,yytext);
return(ID);
}
\n {int c;
ECHO;
c = input();
if (c != EOF) {unput(c); lines++; fprintf(yyout,"%d\t",lines);}
else yyterminate();
}
[ \t] {ECHO;}
[!@#$%^&|?] {ECHO;
printf("\nERROR: %c is an illegal character\n",yytext[0]);
}
%%