-
Notifications
You must be signed in to change notification settings - Fork 1
/
lex.l
107 lines (80 loc) · 2.82 KB
/
lex.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
/*
* dis6502 by Robert Bond, Udi Finkelstein, and Eric Smith
*
* $Id: lex.l 26 2004-01-17 23:28:23Z eric $
* Copyright 2001-2003 Eric Smith <eric@brouhaha.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation. Note that permission is
* not granted to redistribute this program under the terms of any
* other version of the General Public License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
*/
%{
#define YY_NO_INPUT
#undef ECHO
#include <limits.h>
#include <string.h>
#include "dis.h"
int lineno = 0;
static int parse_int(char *str, int base);
%}
%option nounput
digit [0-9]
hexdigit [0-9a-fA-F]
alpha [a-zA-Z]
alphanum [0-9a-zA-Z_]
%%
[ \t] { ; }
[\n] { lineno++; return '\n'; }
\.[Ee][Qq][Uu] { return EQ; }
\.[Ee][Qq] { return EQ; }
\.[Ee][Qq][Ss] { return EQS; }
\.[Oo][Ff][Ss] { return OFS; }
\.[Ll][Ii] { return LI; }
\.[Tt][Rr][Aa][Cc][Ee] { return TSTART; }
\.[Ss][Tt][Oo][Pp] { return TSTOP; }
\.[Rr][Tt][Ss][Tt][Aa][Bb]2 { return TRTSTAB2; }
\.[Rr][Tt][Ss][Tt][Aa][Bb] { return TRTSTAB; }
\.[Jj][Tt][Aa][Bb]2 { return TJTAB2; }
\.[Jj][Tt][Aa][Bb] { return TJTAB; }
{digit}+ {
token.ival = parse_int(yytext, 10);
return NUMBER;
}
\${hexdigit}+ {
token.ival = parse_int(yytext + 1, 16);
return NUMBER;
}
{alpha}{alphanum}* {
token.sval = emalloc(strlen(yytext) + 1);
strcpy(token.sval, yytext);
return NAME;
}
\*.* {
return COMMENT;
}
\;.* {
return COMMENT;
}
. { return yytext[0]; }
%%
static int parse_int(char *str, int base)
{
char *tail = NULL;
long value = 0;
errno = 0;
value = strtol(str, &tail, base);
if (tail == str || *tail != '\0' || errno != 0 || value < INT_MIN || value > INT_MAX)
crash("Error trying to convert %s to int", str);
return (int) value;
}