crowbar.l(Ver.0.1.01)

  1: %{
  2: #include <stdio.h>
  3: #include <string.h>
  4: #include "DBG.h"
  5: #include "crowbar.h"
  6: #include "y.tab.h"
  7: 
  8: int
  9: yywrap(void)
 10: {
 11:     return 1;
 12: }
 13: 
 14: static void
 15: increment_line_number(void)
 16: {
 17:     crb_get_current_interpreter()->current_line_number++;
 18: }
 19: %}
 20: %start COMMENT STRING_LITERAL_STATE
 21: %%
 22: <INITIAL>"function"     return FUNCTION;
 23: <INITIAL>"if"           return IF;
 24: <INITIAL>"else"         return ELSE;
 25: <INITIAL>"elsif"        return ELSIF;
 26: <INITIAL>"while"        return WHILE;
 27: <INITIAL>"for"          return FOR;
 28: <INITIAL>"return"       return RETURN_T;
 29: <INITIAL>"break"        return BREAK;
 30: <INITIAL>"continue"     return CONTINUE;
 31: <INITIAL>"null"         return NULL_T;
 32: <INITIAL>"("            return LP;
 33: <INITIAL>")"            return RP;
 34: <INITIAL>"{"            return LC;
 35: <INITIAL>"}"            return RC;
 36: <INITIAL>";"            return SEMICOLON;
 37: <INITIAL>","            return COMMA;
 38: <INITIAL>"&&"           return LOGICAL_AND;
 39: <INITIAL>"||"           return LOGICAL_OR;
 40: <INITIAL>"="            return ASSIGN;
 41: <INITIAL>"=="           return EQ;
 42: <INITIAL>"!="           return NE;
 43: <INITIAL>">"            return GT;
 44: <INITIAL>">="           return GE;
 45: <INITIAL>"<"            return LT;
 46: <INITIAL>"<="           return LE;
 47: <INITIAL>"+"            return ADD;
 48: <INITIAL>"-"            return SUB;
 49: <INITIAL>"*"            return MUL;
 50: <INITIAL>"/"            return DIV;
 51: <INITIAL>"%"            return MOD;
 52: <INITIAL>[A-Za-z_][A-Za-z_0-9]* {
 53:     yylval.identifier = crb_create_identifier(yytext);
 54:     return IDENTIFIER;
 55: }
 56: <INITIAL>[1-9][0-9]* {
 57:     Expression  *expression = crb_alloc_expression(INT_EXPRESSION);
 58:     sscanf(yytext, "%d", &expression->u.int_value);
 59:     yylval.expression = expression;
 60:     return INT_LITERAL;
 61: }
 62: <INITIAL>"0" {
 63:     Expression  *expression = crb_alloc_expression(INT_EXPRESSION);
 64:     expression->u.int_value = 0;
 65:     yylval.expression = expression;
 66:     return INT_LITERAL;
 67: }
 68: <INITIAL>[0-9]+\.[0-9]+ {
 69:     Expression  *expression = crb_alloc_expression(DOUBLE_EXPRESSION);
 70:     sscanf(yytext, "%lf", &expression->u.double_value);
 71:     yylval.expression = expression;
 72:     return DOUBLE_LITERAL;
 73: }
 74: <INITIAL>\" {
 75:     crb_open_string_literal();
 76:     BEGIN STRING_LITERAL_STATE;
 77: }
 78: <INITIAL>[ \t] ;
 79: <INITIAL>[ \t\n] {increment_line_number();}
 80: <INITIAL>#     BEGIN COMMENT;
 81: <INITIAL>.      {
 82:     crb_compile_error(CHARACTER_INVALID_ERR,
 83:                       CHARACTER_MESSAGE_ARGUMENT, "bad_char", yytext[0],
 84:                       MESSAGE_ARGUMENT_END);
 85: }
 86: <COMMENT>\n     {
 87:     increment_line_number();
 88:     BEGIN INITIAL;
 89: }
 90: <COMMENT>.      ;
 91: <STRING_LITERAL_STATE>\"        {
 92:     Expression *expression = crb_alloc_expression(STRING_EXPRESSION);
 93:     expression->u.string_value = crb_close_string_literal();
 94:     yylval.expression = expression;
 95:     BEGIN INITIAL;
 96:     return STRING_LITERAL;
 97: }
 98: <STRING_LITERAL_STATE>\n        {
 99:     crb_add_string_literal('\n');
100:     increment_line_number();
101: }
102: <STRING_LITERAL_STATE>\\\"      crb_add_string_literal('"');
103: <STRING_LITERAL_STATE>\\n       crb_add_string_literal('\n');
104: <STRING_LITERAL_STATE>\\t       crb_add_string_literal('\t');
105: <STRING_LITERAL_STATE>\\\\      crb_add_string_literal('\\');
106: <STRING_LITERAL_STATE>.         crb_add_string_literal(yytext[0]);
107: %%
戻る