-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmsta-8.html
162 lines (135 loc) · 3.15 KB
/
msta-8.html
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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>
<META NAME="GENERATOR" CONTENT="LinuxDoc-Tools 0.9.66">
<TITLE>MSTA (syntax description translator): Appendix 2 - Example of MSTA scanner description file</TITLE>
<LINK HREF="msta-9.html" REL=next>
<LINK HREF="msta-7.html" REL=previous>
<LINK HREF="msta.html#toc8" REL=contents>
</HEAD>
<BODY>
<A HREF="msta-9.html">Next</A>
<A HREF="msta-7.html">Previous</A>
<A HREF="msta.html#toc8">Contents</A>
<HR>
<H2><A NAME="s8">8.</A> <A HREF="msta.html#toc8">Appendix 2 - Example of MSTA scanner description file</A></H2>
<P>
<BLOCKQUOTE><CODE>
<PRE>
%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);
};
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
void
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 yyscanner (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);
}
</PRE>
</CODE></BLOCKQUOTE>
</P>
<HR>
<A HREF="msta-9.html">Next</A>
<A HREF="msta-7.html">Previous</A>
<A HREF="msta.html#toc8">Contents</A>
</BODY>
</HTML>