view Bison-Flex/BasicCompiler-StackBase/UTF8/script-scanner.ll @ 3:3cea2e8a0e4b

add function goto
author nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
date Tue, 10 May 2011 10:48:02 +0900
parents fbe42292d479
children
line wrap: on
line source

%{
#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;
	":"			return token::TK_COLON;
	"goto"			return token::TK_GOTO;

	\\\n			yylloc->lines();


	\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();
}