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