1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/paper/src/atccl.y Fri Jun 04 13:48:28 2010 +0200
1.3 @@ -0,0 +1,285 @@
1.4 +%{
1.5 +#include <stdlib.h>
1.6 +#include <stdio.h>
1.7 +#include <string.h>
1.8 +#include "semantic_helper.hh"
1.9 +
1.10 +#define MAX_ID_TABLE_NO 1024
1.11 +char* id_table[MAX_ID_TABLE_NO];
1.12 +int id_table_no;
1.13 +ErrorType* errors;
1.14 +int maxErrorNo;
1.15 +int errorNo;
1.16 +int yylex();
1.17 +int yywrap();
1.18 +int yyerror(const char *str);
1.19 +void yyrestart(FILE* file);
1.20 +%}
1.21 +%token
1.22 +CONSTRNT PATTERN FLOWP COM
1.23 +QUOTE THAN FROM UNTIL ON
1.24 +
1.25 +%union
1.26 +{
1.27 + int integer;
1.28 + double real;
1.29 + const char* string;
1.30 +}
1.31 +
1.32 +%token <integer> INTEGER
1.33 +%token <integer> TIME
1.34 +%token <real> REAL
1.35 +%token <string> STRING
1.36 +%token <integer> IS
1.37 +%token <integer> IN
1.38 +%token <integer> GREATER
1.39 +%token <integer> LESS
1.40 +%token <integer> NOT
1.41 +%token <integer> AND
1.42 +%token <integer> OR
1.43 +%token <integer> AT
1.44 +%token <integer> COPX
1.45 +%token <integer> GATE
1.46 +%token <integer> INTERNAL
1.47 +%token <integer> GLOBAL
1.48 +%token <integer> ADEP
1.49 +%token <integer> ADES
1.50 +%token <integer> RWY
1.51 +%token <integer> ROUTE
1.52 +%token <integer> ATYP
1.53 +%token <integer> FLVL
1.54 +%token <integer> TAS
1.55 +%token <integer> FRUL
1.56 +%token <integer> FTYP
1.57 +%token <integer> EQUIP
1.58 +%token <integer> RFL
1.59 +%token <integer> TRAVEL_TYPE
1.60 +%token <integer> TIMESEP
1.61 +%token <integer> DEPTIME
1.62 +%token <integer> ARRTIME
1.63 +%token <integer> GATETIME
1.64 +%token <integer> FL
1.65 +%type <string> string
1.66 +%type <string> pttrn
1.67 +%type <string> flowp
1.68 +%type <string> constrnt
1.69 +%type <integer> int
1.70 +%type <integer> string_array
1.71 +%type <integer> int_array
1.72 +%type <integer> int_pt
1.73 +%type <integer> time_pt
1.74 +%type <integer> char_pt
1.75 +%type <integer> string_pt
1.76 +%type <integer> string_array_pt
1.77 +%type <integer> char_array_pt
1.78 +%type <integer> int_op
1.79 +%type <integer> string_op
1.80 +%type <integer> string_array_op
1.81 +%type <integer> string_string_array_op
1.82 +%type <integer> si_unit
1.83 +
1.84 +%left OR
1.85 +%left AND
1.86 +%right NOT
1.87 +%%
1.88 +prog: | prog rule { }
1.89 + | error ')' { yyerrok; yyclearin;}
1.90 +;
1.91 +rule: pttrn { if (!pattern($1))
1.92 + printf("Error: %s\n", last_error()); }
1.93 + | constrnt { if (!constraint($1))
1.94 + printf("Error: %s\n", last_error()); }
1.95 + | flowp { if (!flowpoint($1))
1.96 + printf("Error: %s\n", last_error()); }
1.97 + | COM { comment(); }
1.98 +;
1.99 +flowp: FLOWP STRING '(' flowp_entry ')' { $$ = $2; }
1.100 +;
1.101 +flowp_entry: pttrn_lbl ':' constrnt_lbl
1.102 +;
1.103 +pttrn_lbl: STRING { if (!pattern_id($1))
1.104 + printf("Error: %s\n", last_error()); }
1.105 +;
1.106 +constrnt_lbl: STRING { if (!constraint_id($1))
1.107 + printf("Error: %s\n", last_error()); }
1.108 +;
1.109 +constrnt: CONSTRNT STRING '(' cterm ')' { $$ = $2; }
1.110 +;
1.111 +cterm: TIMESEP AT string IS INTEGER si_unit
1.112 + { intpt_string_int_cterm(PT_TIMESEP, $3, $5 * $6,
1.113 + 0, 0, 0); }
1.114 + | TIMESEP AT string_array IS INTEGER si_unit
1.115 + { intpt_stringarray_int_cterm(PT_TIMESEP, $3, $5 * $6,
1.116 + 0, 0, 0); }
1.117 + | TIMESEP AT string IS INTEGER si_unit
1.118 + FROM INTEGER UNTIL INTEGER
1.119 + { intpt_string_int_cterm(PT_TIMESEP, $3, $5 * $6, $8,
1.120 + $10, 0); }
1.121 + | TIMESEP AT string_array IS INTEGER si_unit
1.122 + FROM INTEGER UNTIL INTEGER
1.123 + { intpt_stringarray_int_cterm(PT_TIMESEP, $3, $5 * $6,
1.124 + $8, $10, 0); }
1.125 + | TIMESEP AT string IS INTEGER si_unit AT FL int_array
1.126 + { intpt_string_int_cterm(PT_TIMESEP, $3, $5 * $6,
1.127 + 0, 0, $9); }
1.128 + | TIMESEP AT string_array IS INTEGER si_unit
1.129 + AT FL int_array
1.130 + { intpt_stringarray_int_cterm(PT_TIMESEP, $3, $5 * $6,
1.131 + 0, 0, $9); }
1.132 + | TIMESEP AT string IS INTEGER si_unit
1.133 + FROM INTEGER UNTIL INTEGER AT FL int_array
1.134 + { intpt_string_int_cterm(PT_TIMESEP, $3, $5 * $6, $8,
1.135 + $10, $13); }
1.136 + | TIMESEP AT string_array IS INTEGER si_unit
1.137 + FROM INTEGER UNTIL INTEGER AT FL int_array
1.138 + { intpt_stringarray_int_cterm(PT_TIMESEP, $3, $5 * $6,
1.139 + $8, $10, $13); }
1.140 + | cterm AND cterm { and_term(); }
1.141 +;
1.142 +si_unit: STRING { if (!strcmp($1, "s")) { $$ = 1; }
1.143 + else if (!strcmp($1, "min")) { $$ = 60; } }
1.144 +;
1.145 +pttrn: PATTERN STRING '(' term ')' { $$ = $2; }
1.146 + | PATTERN '(' term ')' { $$ = get_pattern_id(); }
1.147 +;
1.148 +term: int_pt int_op INTEGER
1.149 + { intpt_int_term($2, $1, $3); }
1.150 + | time_pt int_op INTEGER
1.151 + { time_pt_int_term($2, $1, $3); }
1.152 + | char_pt string_op string
1.153 + { charpt_char_term($2, $1, $3); }
1.154 + | string_pt string_op string
1.155 + { stringpt_string_term($2, $1, $3); }
1.156 + | string_array_pt string_array_op string_array
1.157 + { stringarraypt_stringarray_term($2, $1, $3); }
1.158 + | string_array string_array_op string_array_pt
1.159 + { if ($2 == OT_IN_C) { $2 = OT_IN_P; }
1.160 + else if ($2 == OT_NOTIN_C) { $2 = OT_NOTIN_P; }
1.161 + stringarraypt_stringarray_term($2, $3, $1); }
1.162 + | string string_string_array_op string_array_pt
1.163 + { if ($2 == OT_IN_C) { $2 = OT_IN_P; }
1.164 + else if ($2 == OT_NOTIN_C) { $2 = OT_NOTIN_P; }
1.165 + stringarraypt_string_term($2, $3, $1); }
1.166 + | string string_string_array_op char_array_pt
1.167 + { if ($2 == OT_IN_C) { $2 = OT_IN_P; }
1.168 + else if ($2 == OT_NOTIN_C) { $2 = OT_NOTIN_P; }
1.169 + chararraypt_char_term($2, $3, $1); }
1.170 + | term OR term { or_term(); }
1.171 + | term AND term { and_term(); }
1.172 + | NOT term
1.173 + | '(' term ')'
1.174 +;
1.175 +int_op: IS { $$ = OT_IS; }
1.176 + | IS NOT { $$ = OT_ISNOT; }
1.177 + | GREATER THAN { $$ = OT_GREATER; }
1.178 + | LESS THAN { $$ = OT_LESS; }
1.179 +;
1.180 +string_op: IS { $$ = OT_IS; }
1.181 + | IS NOT { $$ = OT_ISNOT; }
1.182 +;
1.183 +string_array_op: IS { $$ = OT_IS; }
1.184 + | IS NOT { $$ = OT_ISNOT; }
1.185 + | IN { $$ = OT_IN_P; }
1.186 + | NOT IN { $$ = OT_NOTIN_P; }
1.187 +;
1.188 +string_string_array_op: IN { $$ = OT_IN_P; }
1.189 + | NOT IN { $$ = OT_NOTIN_P; }
1.190 +;
1.191 +int_array: '[' ints ']' { $$ = integer_array(); }
1.192 +;
1.193 +ints: | ints ',' int
1.194 + | ints int
1.195 +;
1.196 +int: INTEGER { integer($1); $$ = $1; }
1.197 +;
1.198 +string_array: '[' strings ']' { $$ = string_array(); }
1.199 +;
1.200 +strings: | strings ',' string
1.201 + | strings string
1.202 +;
1.203 +int_pt: TAS { $$ = PT_TAS; }
1.204 + | INTERNAL { $$ = PT_INTERNAL; }
1.205 + | GLOBAL { $$ = PT_GLOBAL; }
1.206 +;
1.207 +time_pt: DEPTIME { $$ = PT_DEPTIME; }
1.208 + | ARRTIME { $$ = PT_ARRTIME; }
1.209 + | GATETIME { $$ = PT_GATETIME; }
1.210 +;
1.211 +char_pt: FTYP { $$ = PT_FTYP; }
1.212 +;
1.213 +string_pt: ADEP { $$ = PT_ADEP; }
1.214 + | ADES { $$ = PT_ADES; }
1.215 + | RWY { $$ = PT_RWY; }
1.216 + | ATYP { $$ = PT_ATYP; }
1.217 + | COPX { $$ = PT_COPX; }
1.218 + | GATE { $$ = PT_GATE; }
1.219 + | FRUL { $$ = PT_FRUL; }
1.220 + | RFL { $$ = PT_RFL; }
1.221 + | TRAVEL_TYPE { $$ = PT_TRAVEL_TYPE; }
1.222 +;
1.223 +string_array_pt: ROUTE { $$ = PT_ROUTE; }
1.224 +;
1.225 +char_array_pt: EQUIP { $$ = PT_EQUIP; }
1.226 +;
1.227 +string: QUOTE STRING QUOTE { string($2); $$ = $2; }
1.228 +;
1.229 +%%
1.230 +#include "atccl_parser.c"
1.231 +void clear_id_table()
1.232 +{
1.233 + int i;
1.234 + for (i = 0; i < id_table_no; ++i)
1.235 + {
1.236 + free(id_table[i]);
1.237 + id_table[i] = 0;
1.238 + }
1.239 + id_table_no = 0;
1.240 +}
1.241 +int yywrap()
1.242 +{
1.243 + clear_id_table();
1.244 + return 1;
1.245 +}
1.246 +int semantic_error(const char* str)
1.247 +{
1.248 + if (errorNo < maxErrorNo)
1.249 + {
1.250 + errors[errorNo].line = -1;
1.251 + strcpy(errors[errorNo].text, "");
1.252 + strcpy(errors[errorNo].error, str);
1.253 + errorNo++;
1.254 + }
1.255 + return 1;
1.256 +}
1.257 +int yyerror(const char *str)
1.258 +{
1.259 + if (errorNo < maxErrorNo)
1.260 + {
1.261 + errors[errorNo].line = line_no;
1.262 + strcpy(errors[errorNo].text, yytext);
1.263 + strcpy(errors[errorNo].error, str);
1.264 + errorNo++;
1.265 + }
1.266 + return 1;
1.267 +}
1.268 +int parse_syntax(FILE* file, ErrorType* errors_,
1.269 + int maxErrorNo_, int passes)
1.270 +{
1.271 + line_no = 1;
1.272 + errors = errors_;
1.273 + maxErrorNo = maxErrorNo_;
1.274 + errorNo = 0;
1.275 + clear_all();
1.276 + clear_id_table();
1.277 + yyrestart(file);
1.278 + yyparse();
1.279 +
1.280 + if (passes > 1)
1.281 + {
1.282 + if (!complete_semantics())
1.283 + {
1.284 + semantic_error(last_error());
1.285 + }
1.286 + }
1.287 + return errorNo;
1.288 +}