diksam.l(Ver.0.2)
1: %{
2: #undef YY_INPUT
3: #define YY_INPUT(buf, result, max_size) (result = my_yyinput(buf, max_size))
4: #include <stdio.h>
5: #include <string.h>
6: #include "DBG.h"
7: #include "diksamc.h"
8: #include "y.tab.h"
9:
10: static int
11: file_input(char *buf, int max_size)
12: {
13: int ch;
14: int len;
15:
16: if (feof(yyin))
17: return 0;
18:
19: for (len = 0; len < max_size; len++) {
20: ch = getc(yyin);
21: if (ch == EOF)
22: break;
23: buf[len] = ch;
24: }
25: return len;
26: }
27:
28: static char **st_source_string;
29: static int st_current_source_line;
30: static int st_current_char_index;
31:
32: void
33: dkc_set_source_string(char **source)
34: {
35: st_source_string = source;
36: st_current_source_line = 0;
37: st_current_char_index = 0;
38: }
39:
40: static int
41: string_input(char *buf, int max_size)
42: {
43: int len;
44:
45: if (st_source_string[st_current_source_line] == NULL)
46: return 0;
47:
48: if (st_source_string[st_current_source_line][st_current_char_index]
49: == '\0') {
50: st_current_source_line++;
51: st_current_char_index = 0;
52: }
53:
54: if (st_source_string[st_current_source_line] == NULL)
55: return 0;
56:
57: len = smaller(strlen(st_source_string[st_current_source_line])
58: - st_current_char_index,
59: max_size);
60: strncpy(buf,
61: &st_source_string[st_current_source_line][st_current_char_index],
62: len);
63: st_current_char_index += len;
64:
65: return len;
66: }
67:
68: static int
69: my_yyinput(char *buf, int max_size)
70: {
71: int result;
72:
73: switch (dkc_get_current_compiler()->input_mode) {
74: case DKC_FILE_INPUT_MODE:
75: result = file_input(buf, max_size);
76: break;
77: case DKC_STRING_INPUT_MODE:
78: result = string_input(buf, max_size);
79: break;
80: default:
81: DBG_panic(("bad default. input_mode..%d\n",
82: dkc_get_current_compiler()->input_mode));
83: }
84:
85: return result;
86: }
87:
88:
89: int
90: yywrap(void)
91: {
92: return 1;
93: }
94:
95: static void
96: increment_line_number(void)
97: {
98: dkc_get_current_compiler()->current_line_number++;
99: }
100: %}
101: %start C_COMMENT CC_COMMENT STRING_LITERAL_STATE
102: %%
103: <INITIAL>"if" return IF;
104: <INITIAL>"else" return ELSE;
105: <INITIAL>"elsif" return ELSIF;
106: <INITIAL>"while" return WHILE;
107: <INITIAL>"for" return FOR;
108: <INITIAL>"foreach" return FOREACH;
109: <INITIAL>"return" return RETURN_T;
110: <INITIAL>"break" return BREAK;
111: <INITIAL>"continue" return CONTINUE;
112: <INITIAL>"null" return NULL_T;
113: <INITIAL>"true" return TRUE_T;
114: <INITIAL>"false" return FALSE_T;
115: <INITIAL>"try" return TRY;
116: <INITIAL>"catch" return CATCH;
117: <INITIAL>"finally" return FINALLY;
118: <INITIAL>"throw" return THROW;
119: <INITIAL>"boolean" return BOOLEAN_T;
120: <INITIAL>"int" return INT_T;
121: <INITIAL>"double" return DOUBLE_T;
122: <INITIAL>"string" return STRING_T;
123: <INITIAL>"new" return NEW;
124: <INITIAL>"(" return LP;
125: <INITIAL>")" return RP;
126: <INITIAL>"{" return LC;
127: <INITIAL>"}" return RC;
128: <INITIAL>"[" return LB;
129: <INITIAL>"]" return RB;
130: <INITIAL>";" return SEMICOLON;
131: <INITIAL>":" return COLON;
132: <INITIAL>"," return COMMA;
133: <INITIAL>"&&" return LOGICAL_AND;
134: <INITIAL>"||" return LOGICAL_OR;
135: <INITIAL>"=" return ASSIGN_T;
136: <INITIAL>"==" return EQ;
137: <INITIAL>"!=" return NE;
138: <INITIAL>">" return GT;
139: <INITIAL>">=" return GE;
140: <INITIAL>"<" return LT;
141: <INITIAL>"<=" return LE;
142: <INITIAL>"+" return ADD;
143: <INITIAL>"-" return SUB;
144: <INITIAL>"*" return MUL;
145: <INITIAL>"/" return DIV;
146: <INITIAL>"%" return MOD;
147: <INITIAL>"+=" return ADD_ASSIGN_T;
148: <INITIAL>"-=" return SUB_ASSIGN_T;
149: <INITIAL>"*=" return MUL_ASSIGN_T;
150: <INITIAL>"/=" return DIV_ASSIGN_T;
151: <INITIAL>"%=" return MOD_ASSIGN_T;
152: <INITIAL>"++" return INCREMENT;
153: <INITIAL>"--" return DECREMENT;
154: <INITIAL>"!" return EXCLAMATION;
155: <INITIAL>"." return DOT;
156: <INITIAL>[A-Za-z_][A-Za-z_0-9]* {
157: yylval.identifier = dkc_create_identifier(yytext);
158: return IDENTIFIER;
159: }
160: <INITIAL>[1-9][0-9]* {
161: Expression *expression = dkc_alloc_expression(INT_EXPRESSION);
162: sscanf(yytext, "%d", &expression->u.int_value);
163: yylval.expression = expression;
164: return INT_LITERAL;
165: }
166: <INITIAL>"0" {
167: Expression *expression = dkc_alloc_expression(INT_EXPRESSION);
168: expression->u.int_value = 0;
169: yylval.expression = expression;
170: return INT_LITERAL;
171: }
172: <INITIAL>[0-9]+\.[0-9]+ {
173: Expression *expression = dkc_alloc_expression(DOUBLE_EXPRESSION);
174: sscanf(yytext, "%lf", &expression->u.double_value);
175: yylval.expression = expression;
176: return DOUBLE_LITERAL;
177: }
178: <INITIAL>\" {
179: dkc_open_string_literal();
180: BEGIN STRING_LITERAL_STATE;
181: }
182: <INITIAL>[ \t] ;
183: <INITIAL>[ \t\r\n] {increment_line_number();}
184: <INITIAL>"/*" BEGIN C_COMMENT;
185: <INITIAL>"//" BEGIN CC_COMMENT;
186: <INITIAL>. {
187: dkc_compile_error(dkc_get_current_compiler()->current_line_number,
188: CHARACTER_INVALID_ERR,
189: CHARACTER_MESSAGE_ARGUMENT, "bad_char", yytext[0],
190: MESSAGE_ARGUMENT_END);
191: }
192: <C_COMMENT>\n increment_line_number();
193: <C_COMMENT>"*/" {
194: BEGIN INITIAL;
195: }
196: <C_COMMENT>. ;
197: <CC_COMMENT>\n {
198: increment_line_number();
199: BEGIN INITIAL;
200: }
201: <CC_COMMENT>. ;
202: <STRING_LITERAL_STATE>\" {
203: Expression *expression = dkc_alloc_expression(STRING_EXPRESSION);
204: expression->u.string_value = dkc_close_string_literal();
205: yylval.expression = expression;
206: BEGIN INITIAL;
207: return STRING_LITERAL;
208: }
209: <STRING_LITERAL_STATE>\n {
210: dkc_add_string_literal('\n');
211: increment_line_number();
212: }
213: <STRING_LITERAL_STATE>\\\" dkc_add_string_literal('"');
214: <STRING_LITERAL_STATE>\\n dkc_add_string_literal('\n');
215: <STRING_LITERAL_STATE>\\t dkc_add_string_literal('\t');
216: <STRING_LITERAL_STATE>\\\\ dkc_add_string_literal('\\');
217: <STRING_LITERAL_STATE>[\x81-\x9f\xe0-\xef][\x40-\x7e\x80-\xfc] {
218: dkc_add_string_literal(yytext[0]);
219: dkc_add_string_literal(yytext[1]);
220: }
221: <STRING_LITERAL_STATE>. dkc_add_string_literal(yytext[0]);
222: %%
戻る