Mercurial > hg > Members > nobuyasu > SampleSource
diff Bison-Flex/BasicCompiler-StackBase/script-scanner.ll @ 0:db40c85cad7a default tip
upload sample source
author | nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Mon, 09 May 2011 03:11:59 +0900 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Bison-Flex/BasicCompiler-StackBase/script-scanner.ll Mon May 09 03:11:59 2011 +0900 @@ -0,0 +1,116 @@ +%{ +#include <cstdlib> +#include <errno.h> +#include <limits.h> +#include <string> +#include "compiler.h" +#include "script-parser.hh" + +#ifdef _MSC_VER +#pragma warning(disable:4018) +#pragma warning(disable:4102) +#pragma warning(disable:4244) +#pragma warning(disable:4267) +#pragma warning(disable:4996) +#endif + +#undef yywrap +#define yywrap() 1 + +#define yyterminate() return token::END_OF_FILE +%} + +%option noyywrap nounput batch +%option never-interactive +%option noyy_scan_buffer +%option noyy_scan_bytes +%option noyy_scan_string +%option 8bit +%option nounistd + +id [a-zA-Z_][a-zA-Z_0-9]* +int [1-9][0-9]* +blank [ \t] + +%x COMMENT + +%{ +#define YY_USER_ACTION yylloc->columns(yyleng); +%} +%% +%{ + typedef yy::script_parser::token token; + + yylloc->step(); + + std::string string_buffer; +%} +<INITIAL>{ + "^#" { yylloc->step(); BEGIN(COMMENT); } + "'" { yylloc->step(); BEGIN(COMMENT); } + "rem" { yylloc->step(); BEGIN(COMMENT); } + + "if" return token::TK_IF; + "then" return token::TK_THEN; + "else" return token::TK_ELSE; + "endif" return token::TK_ENDIF; + "for" return token::TK_FOR; + "to" return token::TK_TO; + "next" return token::TK_NEXT; + "while" return token::TK_WHILE; + "wend" return token::TK_WEND; + "end" return token::TK_END; + "rand" return token::TK_RAND; + "print" return token::TK_PRINT; + + \\\n yylloc->lines(); + + : return token::TK_NEWLINE; + \n { yylloc->lines(); return token::TK_NEWLINE; } + [-+*/%=,()<>] return yy::script_parser::token_type(yytext[0]); + + "==" return token::TK_EQ; + "!=" return token::TK_NE; + ">=" return token::TK_GE; + "<=" return token::TK_LE; + + {blank}+ yylloc->step(); + {int} { + errno = 0; + long n = strtol(yytext, NULL, 10); + if (n < LONG_MIN || n > LONG_MAX || errno == ERANGE) + driver.error(*yylloc, "整数が範囲外です。"); + yylval->ival = n; + return token::TK_IVAL; + } + "0" { + yylval->ival = 0; + return token::TK_IVAL; + } + {id} { + yylval->sval = new std::string(yytext); + return token::TK_IDENTIFIER; + } + . driver.error(*yylloc, "この文字を識別子で使用することはできません。"); +} +<COMMENT>{ + [^\n]* + \n { + yylloc->lines(); + yylloc->step(); + BEGIN(INITIAL); + } +} +%% + +void compiler::scan_begin() +{ + if ((yyin = fopen(file.c_str(), "r")) == 0) + error(file + " がオープンできません。"); +} + +void compiler::scan_end() +{ + fclose(yyin); + yylex_destroy(); +}