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>"true"         return TRUE_T;
 33: <INITIAL>"false"        return FALSE_T;
 34: <INITIAL>"("            return LP;
 35: <INITIAL>")"            return RP;
 36: <INITIAL>"{"            return LC;
 37: <INITIAL>"}"            return RC;
 38: <INITIAL>";"            return SEMICOLON;
 39: <INITIAL>","            return COMMA;
 40: <INITIAL>"&&"           return LOGICAL_AND;
 41: <INITIAL>"||"           return LOGICAL_OR;
 42: <INITIAL>"="            return ASSIGN;
 43: <INITIAL>"=="           return EQ;
 44: <INITIAL>"!="           return NE;
 45: <INITIAL>">"            return GT;
 46: <INITIAL>">="           return GE;
 47: <INITIAL>"<"            return LT;
 48: <INITIAL>"<="           return LE;
 49: <INITIAL>"+"            return ADD;
 50: <INITIAL>"-"            return SUB;
 51: <INITIAL>"*"            return MUL;
 52: <INITIAL>"/"            return DIV;
 53: <INITIAL>"%"            return MOD;
 54: <INITIAL>[A-Za-z_][A-Za-z_0-9]* {
 55:     yylval.identifier = crb_create_identifier(yytext);
 56:     return IDENTIFIER;
 57: }
 58: <INITIAL>[1-9][0-9]* {
 59:     Expression  *expression = crb_alloc_expression(INT_EXPRESSION);
 60:     sscanf(yytext, "%d", &expression->u.int_value);
 61:     yylval.expression = expression;
 62:     return INT_LITERAL;
 63: }
 64: <INITIAL>"0" {
 65:     Expression  *expression = crb_alloc_expression(INT_EXPRESSION);
 66:     expression->u.int_value = 0;
 67:     yylval.expression = expression;
 68:     return INT_LITERAL;
 69: }
 70: <INITIAL>[0-9]+\.[0-9]+ {
 71:     Expression  *expression = crb_alloc_expression(DOUBLE_EXPRESSION);
 72:     sscanf(yytext, "%lf", &expression->u.double_value);
 73:     yylval.expression = expression;
 74:     return DOUBLE_LITERAL;
 75: }
 76: <INITIAL>\" {
 77:     crb_open_string_literal();
 78:     BEGIN STRING_LITERAL_STATE;
 79: }
 80: <INITIAL>[ \t] ;
 81: <INITIAL>[ \t\n] {increment_line_number();}
 82: <INITIAL>#     BEGIN COMMENT;
 83: <INITIAL>.      {
 84:     crb_compile_error(CHARACTER_INVALID_ERR,
 85:                       CHARACTER_MESSAGE_ARGUMENT, "bad_char", yytext[0],
 86:                       MESSAGE_ARGUMENT_END);
 87: }
 88: <COMMENT>\n     {
 89:     increment_line_number();
 90:     BEGIN INITIAL;
 91: }
 92: <COMMENT>.      ;
 93: <STRING_LITERAL_STATE>\"        {
 94:     Expression *expression = crb_alloc_expression(STRING_EXPRESSION);
 95:     expression->u.string_value = crb_close_string_literal();
 96:     yylval.expression = expression;
 97:     BEGIN INITIAL;
 98:     return STRING_LITERAL;
 99: }
100: <STRING_LITERAL_STATE>\n        {
101:     crb_add_string_literal('\n');
102:     increment_line_number();
103: }
104: <STRING_LITERAL_STATE>\\\"      crb_add_string_literal('"');
105: <STRING_LITERAL_STATE>\\n       crb_add_string_literal('\n');
106: <STRING_LITERAL_STATE>\\t       crb_add_string_literal('\t');
107: <STRING_LITERAL_STATE>\\\\      crb_add_string_literal('\\');
108: <STRING_LITERAL_STATE>.         crb_add_string_literal(yytext[0]);
109: %%
戻る