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