0
|
1 %{
|
|
2 #include <cstdlib>
|
|
3 #include <cerrno>
|
|
4 #include <climits>
|
|
5 #include <string>
|
|
6 #include "calc-driver.h"
|
|
7 #include "calc-parser.hh"
|
|
8
|
|
9 #ifdef _MSC_VER
|
|
10 #pragma warning(disable:4018)
|
|
11 #pragma warning(disable:4102)
|
|
12 #pragma warning(disable:4244)
|
|
13 #pragma warning(disable:4267)
|
|
14 #pragma warning(disable:4996)
|
|
15 #endif
|
|
16
|
|
17 #undef yywrap
|
|
18 #define yywrap() 1
|
|
19
|
|
20 #define yyterminate() return token::TK_EOF
|
|
21 %}
|
|
22
|
|
23 %option noyywrap nounput batch
|
|
24 %option never-interactive
|
|
25 %option noyy_scan_buffer
|
|
26 %option noyy_scan_bytes
|
|
27 %option noyy_scan_string
|
|
28 %option nounistd
|
|
29
|
|
30 id [a-zA-Z_][a-zA-Z_0-9]*
|
|
31 int [1-9][0-9]*
|
|
32 blank [ \t]
|
|
33
|
|
34 %%
|
|
35 %{
|
|
36 typedef yy::calc_parser::token token;
|
|
37
|
|
38 std::string string_buffer;
|
|
39 %}
|
|
40
|
|
41 "list" return token::TK_LIST;
|
|
42 "print" return token::TK_PRINT;
|
|
43
|
|
44 [-+*/=()\n] return yy::calc_parser::token_type(yytext[0]);
|
|
45
|
|
46 {blank}+ ;
|
|
47 {int} {
|
|
48 errno = 0;
|
|
49 long n = strtol(yytext, NULL, 10);
|
|
50 if (n < LONG_MIN || n > LONG_MAX || errno == ERANGE)
|
|
51 driver.error("整数が範囲外です。");
|
|
52 yylval->ival = n;
|
|
53 return token::TK_IVAL;
|
|
54 }
|
|
55 "0" {
|
|
56 yylval->ival = 0;
|
|
57 return token::TK_IVAL;
|
|
58 }
|
|
59 {id} {
|
|
60 yylval->sval = new std::string(yytext);
|
|
61 return token::TK_IDENTIFIER;
|
|
62 }
|
|
63 . driver.error("この文字を識別子で使用することはできません。");
|
|
64
|
|
65 %%
|
|
66
|
|
67 void calc_driver::scan_begin()
|
|
68 {
|
|
69 if ((yyin = fopen(file.c_str(), "r")) == 0)
|
|
70 error(file + " がオープンできません。");
|
|
71 }
|
|
72
|
|
73 void calc_driver::scan_end()
|
|
74 {
|
|
75 fclose(yyin);
|
|
76 yylex_destroy();
|
|
77 }
|