-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlex.y
134 lines (109 loc) · 2.43 KB
/
lex.y
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
%local {
#include <stdio.h>
#include <string.h>
#define IDENTIFIER 300
#define NUMBER 301
#define STRING 302
static FILE *input;
static char lexema [100];
static int lexema_index;
static int lineno;
}
%scanner
%%
program :
| program {lexema_index=0;} lexema
;
lexema : identifier {lexema [lexema_index++] = 0;return IDENTIFIER;}
| number {lexema [lexema_index++] = 0;return NUMBER;}
| string {lexema [lexema_index++] = 0;return STRING;}
| comment
| space
| error
;
space : ' ' | '\t' | '\n' {lineno++;}
identifier : identifier (letter | digit)
| letter
;
letter : ('a'-'z' | 'A' - 'Z') {lexema[lexema_index++] = yysprev_char;}
;
digit : '0' - '9' {lexema[lexema_index++] = yysprev_char;}
;
number : number digit
| digit
;
string : '"' (('\1' -> '"' | '"' <- '\377')
{lexema[lexema_index++] = yysprev_char;} ) * '"'
;
comment : "/*" '\0' - '\377' * "*/" /* Conflict shift/reduce on / after * */
;
%%
#ifdef __cplusplus
class scanner: public yyscanner
{
public:
inline int yyslex (void);
void yyserror (const char *message);
scanner (int &error_flag) : yyscanner (error_flag) {}
};
int scanner::yyslex (void)
{
return fgetc (input);
}
void scanner::yyserror (const char *message)
{
fprintf (stderr, "illegal code %d on line %d\n", yyschar, lineno);
}
static scanner *scan_ptr;
#else
int yyslex (void)
{
return fgetc (input);
}
yyserror (const char *message)
{
fprintf (stderr, "illegal code %d on line %d\n", yyschar, lineno);
}
#endif
int
main (int argc, char **argv)
{
int token;
int error_flag;
if (argc != 2)
{
fprintf (stderr, "Usage: lex file\n");
exit (1);
}
if (strcmp (argv[1], "-") == 0)
input = stdin;
else
input = fopen (argv[1], "rb");
if (input == NULL)
{
perror (argv[1]);
exit (1);
}
#ifdef __cplusplus
scan_ptr = new scanner (error_flag);
if (error_flag)
{
fprintf (stderr, "no memory for object scanner");
exit (1);
}
lineno = 1;
while ((token = scan_ptr->yylex ()) > 0)
fprintf (stderr, "%d - %s\n", token, lexema);
#else
lineno = 1;
yylex_start (&error_flag);
if (error_flag)
{
fprintf (stderr, "no memory for scanner arrays");
exit (1);
}
while ((token = yylex ()) > 0)
fprintf (stderr, "%d - %s\n", token, lexema);
#endif
exit (0);
}