-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscanner.l
171 lines (144 loc) · 3.56 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
162
163
164
165
166
167
168
/**
* Copyright 2023 Slawomir Maludzinski
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
%{
#include <stdio.h>
#include <string.h>
#include "scanner.h"
unsigned int line_no = 1;
extern void token_delete(token * tokp)
{
switch (tokp->type)
{
case TOK_ANON:
case TOK_VAR:
case TOK_ATOM:
case TOK_FAIL:
free(tokp->val.string_val);
break;
}
}
const char * token_to_str(token * tokp)
{
switch (tokp->type)
{
case TOK_ANON: return tokp->val.string_val;
case TOK_VAR: return tokp->val.string_val;
case TOK_ATOM: return tokp->val.string_val;
}
return "unknown";
}
%}
DIGIT [0-9]
HEX_DIGIT [0-9a-fA-F]
ID [a-zA-Z_]
SPECIAL [.,()=|\[\]+\-*/<>]
%option pointer
%option noyylineno
%option noyywrap
%%
<INITIAL>{
[ \t\r]+ /* remove white space */
\n {
line_no++;
}
"_" {
tokp->type = TOK_ANON;
tokp->line_no = line_no;
tokp->val.string_val = (char *)strdup(yytext);
return TOK_ANON;
}
"<=" {
tokp->type = TOK_ARR;
tokp->line_no = line_no;
tokp->val.string_val = (char *)yytext;
return TOK_ARR;
}
":-" {
tokp->type = TOK_IMPL;
tokp->line_no = line_no;
tokp->val.string_val = (char *)yytext;
return TOK_IMPL;
}
"?-" {
tokp->type = TOK_QUERY;
tokp->line_no = line_no;
tokp->val.string_val = (char *)yytext;
return TOK_QUERY;
}
"!" {
tokp->type = TOK_CUT;
tokp->line_no = line_no;
tokp->val.string_val = (char *)yytext;
return TOK_CUT;
}
"is" {
tokp->type = TOK_IS;
tokp->line_no = line_no;
tokp->val.string_val = (char *)yytext;
return TOK_IS;
}
"fail" {
tokp->type = TOK_FAIL;
tokp->line_no = line_no;
tokp->val.string_val = (char *)strdup(yytext);
return TOK_FAIL;
}
[A-Z]({ID}|{DIGIT})* {
tokp->type = TOK_VAR;
tokp->line_no = line_no;
tokp->val.string_val = (char *)strdup(yytext);
return TOK_VAR;
}
[a-z]({ID}|{DIGIT})* {
tokp->type = TOK_ATOM;
tokp->line_no = line_no;
tokp->val.string_val = (char *)strdup(yytext);
return TOK_ATOM;
}
{DIGIT}+ {
int result;
tokp->line_no = line_no;
sscanf(yytext, "%d", &result);
tokp->val.int_val = result;
return TOK_INT;
}
0x{HEX_DIGIT}+|0X{HEX_DIGIT}+ {
int result;
tokp->line_no = line_no;
sscanf(yytext, "%x", &result);
tokp->val.int_val = result;
return TOK_INT;
}
{SPECIAL} {
tokp->line_no = line_no;
tokp->val.char_val = yytext[0];
return yytext[0];
}
<<EOF>> {
yyterminate();
return yytext[0];
}
. {
printf("%d unknown char %s\n", line_no, yytext);
}
}
%%