-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo3-lex.lpp
61 lines (50 loc) · 1.34 KB
/
demo3-lex.lpp
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
/* vim: set filetype=lex: */
%option noyywrap
%option prefix="demo3"
%option reentrant
%option warn nodefault
%option stack
%option bison-bridge bison-locations
%x STRING_ESCAPE
%top {
typedef union YYSTYPE YYSTYPE;
typedef struct YYLTYPE YYLTYPE;
}
%{
#include "demo3-parse.hpp"
%}
/* options to get rid of compiler warnings */
%option noinput nounput noyy_top_state noyyalloc noyyrealloc noyyfree
%{
#include <cstdlib>
// Hack to get rid of a compiler warning in yy_fatal_error
// since %option noyy_fatal_error doesn't (and can't) work.
#define fprintf yyscanner, fprintf
// These are, relatively speaking, not a hack.
void *yyalloc(size_t size, yyscan_t yyscanner)
{
(void) yyscanner;
return malloc(size);
}
void *yyrealloc(void *ptr, size_t size, yyscan_t yyscanner)
{
(void) yyscanner;
return realloc(ptr, size);
}
void yyfree(void *ptr, yyscan_t yyscanner)
{
(void) yyscanner;
free(ptr);
}
%}
%%
\n { return NEWLINE; }
\\ { yy_push_state(STRING_ESCAPE, yyscanner); }
. { yylval->c = yytext[0]; return CHAR; }
<STRING_ESCAPE>{
\\ { yy_pop_state(yyscanner); yylval->c = '\\'; return CHAR; }
\n { yy_pop_state(yyscanner); yylval->c = '\n'; return CHAR; }
. { abort(); }
}
%%
/* empty 3rd section */