4
|
1 %{
|
|
2 #include <cstdlib>
|
|
3 #include <errno.h>
|
|
4 #include <limits.h>
|
|
5 #include <string>
|
|
6 #include "compiler.h"
|
|
7 #include "script-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::END_OF_FILE
|
|
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 8bit
|
|
29 %option nounistd
|
|
30
|
|
31 id [a-zA-Z_][a-zA-Z_0-9]*
|
|
32 int [1-9][0-9]*
|
|
33 blank [ \t]
|
|
34
|
|
35 %x COMMENT
|
|
36
|
|
37 %{
|
|
38 #define YY_USER_ACTION yylloc->columns(yyleng);
|
|
39 %}
|
|
40 %%
|
|
41 %{
|
|
42 typedef yy::script_parser::token token;
|
|
43
|
|
44 yylloc->step();
|
|
45
|
|
46 std::string string_buffer;
|
|
47 %}
|
|
48 <INITIAL>{
|
|
49 "^#" { yylloc->step(); BEGIN(COMMENT); }
|
|
50 "'" { yylloc->step(); BEGIN(COMMENT); }
|
|
51 "rem" { yylloc->step(); BEGIN(COMMENT); }
|
|
52
|
|
53 "if" return token::TK_IF;
|
|
54 "then" return token::TK_THEN;
|
|
55 "else" return token::TK_ELSE;
|
|
56 "endif" return token::TK_ENDIF;
|
|
57 "for" return token::TK_FOR;
|
|
58 "to" return token::TK_TO;
|
|
59 "next" return token::TK_NEXT;
|
|
60 "while" return token::TK_WHILE;
|
|
61 "wend" return token::TK_WEND;
|
|
62 "end" return token::TK_END;
|
|
63 "rand" return token::TK_RAND;
|
|
64 "print" return token::TK_PRINT;
|
|
65
|
|
66 \\\n yylloc->lines();
|
|
67
|
|
68 : return token::TK_NEWLINE;
|
|
69 \n { yylloc->lines(); return token::TK_NEWLINE; }
|
|
70 [-+*/%=,()<>] return yy::script_parser::token_type(yytext[0]);
|
|
71
|
|
72 "==" return token::TK_EQ;
|
|
73 "!=" return token::TK_NE;
|
|
74 ">=" return token::TK_GE;
|
|
75 "<=" return token::TK_LE;
|
|
76
|
|
77 {blank}+ yylloc->step();
|
|
78 {int} {
|
|
79 errno = 0;
|
|
80 long n = strtol(yytext, NULL, 10);
|
|
81 if (n < LONG_MIN || n > LONG_MAX || errno == ERANGE)
|
|
82 driver.error(*yylloc, "整数が範囲外です。");
|
|
83 yylval->ival = n;
|
|
84 return token::TK_IVAL;
|
|
85 }
|
|
86 "0" {
|
|
87 yylval->ival = 0;
|
|
88 return token::TK_IVAL;
|
|
89 }
|
|
90 {id} {
|
|
91 yylval->sval = new std::string(yytext);
|
|
92 return token::TK_IDENTIFIER;
|
|
93 }
|
|
94 . driver.error(*yylloc, "この文字を識別子で使用することはできません。");
|
|
95 }
|
|
96 <COMMENT>{
|
|
97 [^\n]*
|
|
98 \n {
|
|
99 yylloc->lines();
|
|
100 yylloc->step();
|
|
101 BEGIN(INITIAL);
|
|
102 }
|
|
103 }
|
|
104 %%
|
|
105
|
|
106 void compiler::scan_begin()
|
|
107 {
|
|
108 if ((yyin = fopen(file.c_str(), "r")) == 0)
|
|
109 error(file + " がオープンできません。");
|
|
110 }
|
|
111
|
|
112 void compiler::scan_end()
|
|
113 {
|
|
114 fclose(yyin);
|
|
115 yylex_destroy();
|
|
116 }
|