comparison Bison-Flex/BasicCompiler-StackBase/UTF8/#script-scanner.ll# @ 4:805d39d28230

add Compiler-stackbase
author nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
date Tue, 17 May 2011 08:00:38 +0900
parents
children
comparison
equal deleted inserted replaced
3:3cea2e8a0e4b 4:805d39d28230
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 ":" return token::TK_COLON;
66 "goto" return token::TK_GOTO;
67
68 \\\n yylloc->lines();
69
70
71 \n { yylloc->lines(); return token::TK_NEWLINE; }
72 [-+*/%=,()<>] return yy::script_parser::token_type(yytext[0]);
73
74 "==" return token::TK_EQ;
75 "!=" return token::TK_NE;
76 ">=" return token::TK_GE;
77 "<=" return token::TK_LE;
78
79 {blank}+ yylloc->step();
80 {int} {
81 errno = 0;
82 long n = strtol(yytext, NULL, 10);
83 if (n < LONG_MIN || n > LONG_MAX || errno == ERANGE)
84 driver.error(*yylloc, "’À°’¿ô’¤¬’ÈÏ’°Ï’³°’¤Ç’¤¹’¡£");
85 yylval->ival = n;
86 return token::TK_IVAL;
87 }
88 "0" {
89 yylval->ival = 0;
90 return token::TK_IVAL;
91 }
92 {id} {
93 yylval->sval = new std::string(yytext);
94 return token::TK_IDENTIFIER;
95 }
96 . driver.error(*yylloc, "’¤³’¤Î’ʸ’»ú’¤ò’¼±’ÊÌ’»Ò’¤Ç’»È’ÍÑ’¤¹’¤ë’¤³’¤È’¤Ï’¤Ç’¤­’¤Þ’¤»’¤ó’¡£");
97 }
98 <COMMENT>{
99 [^\n]*
100 \n {
101 yylloc->lines();
102 yylloc->step();
103 BEGIN(INITIAL);
104 }
105 }
106 %%
107
108 void compiler::scan_begin()
109 {
110 if ((yyin = fopen(file.c_str(), "r")) == 0)
111 error(file + " ’¤¬’¥ª’¡¼’¥×’¥ó’¤Ç’¤­’¤Þ’¤»’¤ó’¡£");
112 }
113
114 void compiler::scan_end()
115 {
116 fclose(yyin);
117 yylex_destroy();
118 }