-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanner.l
161 lines (145 loc) · 5.54 KB
/
scanner.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
%{
/*
* scanner.l
*
* lex input file for pascal scanner
*
*/
#include <stdio.h>
#include <string.h>
#include "parser.h"
int fileno(FILE *);
#define YY_USER_ACTION chr_no += yyleng; // note this line
#define MAX_LINE_LENG 1000
#define LIST strcat(buffer, yytext)
#define LIST_FLUSH do{ if(opt_list) printf("%s", buffer); *buffer = 0; }while(0)
#define LOG(TYPE) do{ LIST; if(tokenon == 0){fprintf(stderr, "token(type:%-10s) on line %4d, %3d : %s\n", #TYPE, line_no, chr_no - yyleng + 1, yytext);}} while(0)
#define COMMENT do{ LIST; if(tokenon == 0){fprintf(stderr, "[INFO ] line %4d:%3d comment string\n", line_no, chr_no-(int)yyleng+1); }} while(0)
int opt_list = 1, opt_token = 0;
int line_no = 1, chr_no = 0;
char buffer[MAX_LINE_LENG];
int flag = 0;
int turnon = 0;
int tokenon = 1;
%}
%option nounput
%option noinput
A [aA]
B [bB]
C [cC]
D [dD]
E [eE]
F [fF]
G [gG]
H [hH]
I [iI]
J [jJ]
K [kK]
L [lL]
M [mM]
N [nN]
O [oO]
P [pP]
Q [qQ]
R [rR]
S [sS]
T [tT]
U [uU]
V [vV]
W [wW]
X [xX]
Y [yY]
Z [zZ]
%%
/* v could do something */
{P}{R}{O}{G}{R}{A}{M} {LOG(KEYWORD);return(PROGRAM); }
{V}{A}{R} {LOG(KEYWORD); return(VAR); }
{A}{R}{R}{A}{Y} {LOG(KEYWORD); return(ARRAY); }
{O}{F} {LOG(KEYWORD); return(OF); }
{I}{N}{T}{E}{G}{E}{R} {LOG(KEYWORD); return(INTEGER); }
{R}{E}{A}{L} {LOG(KEYWORD); return(REAL); }
{S}{T}{R}{I}{N}{G} {LOG(KEYWORD); return(STRING); }
{F}{U}{N}{C}{T}{I}{O}{N} {LOG(KEYWORD); return(FUNCTION); }
{P}{R}{O}{C}{E}{D}{U}{R}{E} {LOG(KEYWORD); return(PROCEDURE); }
{B}{E}{G}{I}{N} {LOG(KEYWORD); return(PBEGIN); }
{E}{N}{D} {LOG(KEYWORD); return(END); }
{I}{F} {LOG(KEYWORD); return(IF); }
{T}{H}{E}{N} {LOG(KEYWORD); return(THEN); }
{E}{L}{S}{E} {LOG(KEYWORD); return(ELSE); }
{W}{H}{I}{L}{E} {LOG(KEYWORD); return(WHILE); }
{D}{O} {LOG(KEYWORD); return(DO); }
{N}{O}{T} {LOG(KEYWORD); return(NOT); }
{A}{N}{D} {LOG(KEYWORD); return(AND); }
{O}{R} {LOG(KEYWORD); return(OR); }
"(" {LOG(KEYWORD); return(LPAREN); }
")" {LOG(KEYWORD); return(RPAREN); }
";" {LOG(KEYWORD); return(SEMICOLON); }
"." {LOG(KEYWORD); return(DOT); }
"," {LOG(KEYWORD); return(COMMA); }
":" {LOG(KEYWORD); return(COLON); }
"[" {LOG(KEYWORD); return(LBRACE); }
"]" {LOG(KEYWORD); return(RBRACE); }
".." {LOG(KEYWORD); return(DOTDOT); }
":=" {LOG(KEYWORD); return(ASSIGNMENT); }
"+" {LOG(KEYWORD); return(ADDOP); }
"-" {LOG(KEYWORD); return(SUBOP); }
"*" {LOG(KEYWORD); return(MULOP); }
"/" {LOG(KEYWORD); return(DIVOP); }
">" {LOG(KEYWORD); return(GTOP); }
"<" {LOG(KEYWORD); return(LTOP); }
"=" {LOG(KEYWORD); return(EQOP); }
">=" {LOG(KEYWORD); return(GETOP); }
"<=" {LOG(KEYWORD); return(LETOP); }
"!=" {LOG(KEYWORD); return(NEQOP); }
[a-z][a-z|A-Z|0-9]*([_]+[a-z|A-Z|0-9]+)* {LOG(IDENTIFIER); return(IDENTIFIER);}
[0-9]+("."[0-9]+)*([E|e][+|-]*[0-9]+)* {LOG(NUMBER); return(NUMBER);}
[0-9]+"."[0-9]+ {LOG(REALNUMBER); return(REALNUMBER);}
[0-9]+"."[0-9]+[e|E][+|-][0-9]+ {LOG(SCIENTIFIC); return(SCIENTIFIC);}
\"([^\\\"]|\\.)*\" {LOG(LITERALSTR); return(LITERALSTR);}
("//")|("//"[^\n]+) {COMMENT;}
"/*"([^(\*/)]+((\*[^/])|(("//")*[^(\*/)]+)|[\)])*)* {do{
LIST;
if(tokenon == 0)
{
fprintf(stderr, "[INFO ] line %4d:%3d comment string start\n", line_no, chr_no-(int)yyleng+1);
}
int flag = 0;
for(int i = 0; i < yyleng; i++)
{
if(yytext[i] == '\n')
{
line_no++;
chr_no = 0;
flag = 1;
}
else
{
if(flag == 1)
{
chr_no++;
}
}
}
if(turnon == 0)
LIST_FLUSH;
} while(0);
}
"*/" {do{
LIST;
if(tokenon == 0)
{
fprintf(stderr, "[INFO ] line %4d:%3d comment string end\n", line_no, chr_no-(int)yyleng+1); }
} while(0);
}
[ \t\f\r] LIST;
\n {
LIST;
if(turnon == 0)
LIST_FLUSH;
line_no++, chr_no = 0;
}
. { LIST; fprintf(stderr, "[ERROR] line %4d:%3d lexical analyzer error %s\n", line_no, chr_no-(int)yyleng+1, yytext); }
["#"][" "]*pragma[" "]*list[" "]*on { *buffer = 0; turnon = 0; LIST; LIST_FLUSH;}
["#"][" "]*pragma[" "]*list[" "]*off { turnon = 1;}
["#"][" "]*pragma[" "]*token[" "]*on { *buffer = 0; tokenon = 0; LIST; LIST_FLUSH;}
["#"][" "]*pragma[" "]*token[" "]*off { tokenon = 1;}