diksam.l(Ver.0.1)

  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>"("            return LP;
124: <INITIAL>")"            return RP;
125: <INITIAL>"{"            return LC;
126: <INITIAL>"}"            return RC;
127: <INITIAL>"["            return LB;
128: <INITIAL>"]"            return RB;
129: <INITIAL>";"            return SEMICOLON;
130: <INITIAL>":"            return COLON;
131: <INITIAL>","            return COMMA;
132: <INITIAL>"&&"           return LOGICAL_AND;
133: <INITIAL>"||"           return LOGICAL_OR;
134: <INITIAL>"="            return ASSIGN_T;
135: <INITIAL>"=="           return EQ;
136: <INITIAL>"!="           return NE;
137: <INITIAL>">"            return GT;
138: <INITIAL>">="           return GE;
139: <INITIAL>"<"            return LT;
140: <INITIAL>"<="           return LE;
141: <INITIAL>"+"            return ADD;
142: <INITIAL>"-"            return SUB;
143: <INITIAL>"*"            return MUL;
144: <INITIAL>"/"            return DIV;
145: <INITIAL>"%"            return MOD;
146: <INITIAL>"+="           return ADD_ASSIGN_T;
147: <INITIAL>"-="           return SUB_ASSIGN_T;
148: <INITIAL>"*="           return MUL_ASSIGN_T;
149: <INITIAL>"/="           return DIV_ASSIGN_T;
150: <INITIAL>"%="           return MOD_ASSIGN_T;
151: <INITIAL>"++"           return INCREMENT;
152: <INITIAL>"--"           return DECREMENT;
153: <INITIAL>"!"            return EXCLAMATION;
154: <INITIAL>"."            return DOT;
155: <INITIAL>[A-Za-z_][A-Za-z_0-9]* {
156:     yylval.identifier = dkc_create_identifier(yytext);
157:     return IDENTIFIER;
158: }
159: <INITIAL>[1-9][0-9]* {
160:     Expression  *expression = dkc_alloc_expression(INT_EXPRESSION);
161:     sscanf(yytext, "%d", &expression->u.int_value);
162:     yylval.expression = expression;
163:     return INT_LITERAL;
164: }
165: <INITIAL>"0" {
166:     Expression  *expression = dkc_alloc_expression(INT_EXPRESSION);
167:     expression->u.int_value = 0;
168:     yylval.expression = expression;
169:     return INT_LITERAL;
170: }
171: <INITIAL>[0-9]+\.[0-9]+ {
172:     Expression  *expression = dkc_alloc_expression(DOUBLE_EXPRESSION);
173:     sscanf(yytext, "%lf", &expression->u.double_value);
174:     yylval.expression = expression;
175:     return DOUBLE_LITERAL;
176: }
177: <INITIAL>\" {
178:     dkc_open_string_literal();
179:     BEGIN STRING_LITERAL_STATE;
180: }
181: <INITIAL>[ \t] ;
182: <INITIAL>[ \t\r\n] {increment_line_number();}
183: <INITIAL>"/*"     BEGIN C_COMMENT;
184: <INITIAL>"//"     BEGIN CC_COMMENT;
185: <INITIAL>.      {
186:     dkc_compile_error(dkc_get_current_compiler()->current_line_number,
187:                       CHARACTER_INVALID_ERR,
188:                       CHARACTER_MESSAGE_ARGUMENT, "bad_char", yytext[0],
189:                       MESSAGE_ARGUMENT_END);
190: }
191: <C_COMMENT>\n     increment_line_number();
192: <C_COMMENT>"*/"     {
193:     BEGIN INITIAL;
194: }
195: <C_COMMENT>.      ;
196: <CC_COMMENT>\n  {
197:     increment_line_number();
198:     BEGIN INITIAL;
199: }
200: <CC_COMMENT>.   ;
201: <STRING_LITERAL_STATE>\"        {
202:     Expression *expression = dkc_alloc_expression(STRING_EXPRESSION);
203:     expression->u.string_value = dkc_close_string_literal();
204:     yylval.expression = expression;
205:     BEGIN INITIAL;
206:     return STRING_LITERAL;
207: }
208: <STRING_LITERAL_STATE>\n        {
209:     dkc_add_string_literal('\n');
210:     increment_line_number();
211: }
212: <STRING_LITERAL_STATE>\\\"      dkc_add_string_literal('"');
213: <STRING_LITERAL_STATE>\\n       dkc_add_string_literal('\n');
214: <STRING_LITERAL_STATE>\\t       dkc_add_string_literal('\t');
215: <STRING_LITERAL_STATE>\\\\      dkc_add_string_literal('\\');
216: <STRING_LITERAL_STATE>[\x81-\x9f\xe0-\xef][\x40-\x7e\x80-\xfc] {
217:   dkc_add_string_literal(yytext[0]);
218:   dkc_add_string_literal(yytext[1]);
219: }
220: <STRING_LITERAL_STATE>.         dkc_add_string_literal(yytext[0]);
221: %%
戻る