book/atccl.y
author Eugen Sawin <sawine@me73.com>
Wed, 30 Mar 2011 18:10:56 +0200
changeset 19 a8c8ad6045d6
permissions -rw-r--r--
Final.
     1 %{
     2 #include <stdlib.h>
     3 #include <stdio.h>
     4 #include <string.h>
     5 #include "semantic_helper.hh"
     6 
     7 #define MAX_ID_TABLE_NO 1024 
     8 char* id_table[MAX_ID_TABLE_NO];
     9 int id_table_no;
    10 
    11 ErrorType* errors;
    12 int maxErrorNo;
    13 int errorNo;
    14 
    15 int yylex();
    16 int yywrap();
    17 int yyerror(const char *str);
    18 void yyrestart(FILE* file);
    19 
    20 %}
    21 
    22 %token
    23 CONSTRNT PATTERN FLOWP COM
    24 QUOTE THAN FROM UNTIL ON
    25 
    26 %union
    27 {
    28 	int integer;
    29 	double real;
    30 	const char* string;
    31 }
    32 
    33 %token <integer> INTEGER
    34 %token <integer> TIME
    35 %token <real> 	REAL
    36 %token <string> STRING
    37 
    38 
    39 %token <integer> IS 
    40 %token <integer> IN 
    41 %token <integer> GREATER 
    42 %token <integer> LESS 
    43 %token <integer> NOT 
    44 %token <integer> AND 
    45 %token <integer> OR 
    46 %token <integer> AT
    47 %token <integer> COPX
    48 %token <integer> ADEP
    49 %token <integer> ADES
    50 %token <integer> RWY
    51 %token <integer> ROUTE
    52 %token <integer> ATYP
    53 %token <integer> FLVL
    54 %token <integer> TAS
    55 %token <integer> FRUL
    56 %token <integer> FTYP
    57 %token <integer> EQUIP
    58 %token <integer> RFL
    59 %token <integer> TRAVEL_TYPE
    60 %token <integer> TIMESEP 
    61 %token <integer> FL
    62 
    63 %type <string> string
    64 %type <string> pttrn
    65 %type <string> flowp
    66 %type <string> constrnt
    67 %type <integer> int
    68 %type <integer> string_array
    69 %type <integer> int_array
    70 %type <integer> int_pt
    71 %type <integer> char_pt
    72 %type <integer> string_pt
    73 %type <integer> string_array_pt
    74 %type <integer> char_array_pt
    75 %type <integer> int_op
    76 %type <integer> string_op
    77 %type <integer> string_array_op
    78 %type <integer> string_string_array_op
    79 %type <integer> si_unit
    80 
    81 %left OR
    82 %left AND
    83 %right NOT
    84 
    85 %%
    86 prog:			| prog rule {  }
    87 				| error ')' { yyerrok; yyclearin;}				
    88 ;
    89 
    90 rule:			pttrn 		{ if (!pattern($1)) printf("Error: %s\n", last_error()); }
    91 				| constrnt 	{ if (!constraint($1)) printf("Error: %s\n", last_error()); }
    92 				| flowp 	{ if (!flowpoint($1)) printf("Error: %s\n", last_error()); }
    93 				| COM		{ comment(); }
    94 ;
    95 
    96 flowp:			FLOWP STRING '(' flowp_entry ')'	{ $$ = $2; }
    97 ;
    98 
    99 flowp_entry:	pttrn_lbl ':' constrnt_lbl					
   100 ;
   101 
   102 pttrn_lbl:		STRING						{ if (!pattern_id($1)) printf("Error: %s\n", last_error()); }						
   103 ;
   104 
   105 constrnt_lbl:	STRING	{ if (!constraint_id($1)) printf("Error: %s\n", last_error()); }
   106 ;
   107 
   108 constrnt:		CONSTRNT STRING '(' cterm ')' 	{ $$ = $2; }
   109 ;
   110 
   111 cterm:			TIMESEP AT string IS INTEGER si_unit		
   112 				{ intpt_string_int_cterm(PT_TIMESEP, $3, $5 * $6, 0, 0, 0); }				
   113 				| TIMESEP AT string_array IS INTEGER si_unit
   114 				{ intpt_stringarray_int_cterm(PT_TIMESEP, $3, $5 * $6, 0, 0, 0); }		
   115 				| TIMESEP AT string IS INTEGER si_unit FROM INTEGER UNTIL INTEGER	
   116 				{ intpt_string_int_cterm(PT_TIMESEP, $3, $5 * $6, $8, $10, 0); }				
   117 				| TIMESEP AT string_array IS INTEGER si_unit FROM INTEGER UNTIL INTEGER
   118 				{ intpt_stringarray_int_cterm(PT_TIMESEP, $3, $5 * $6, $8, $10, 0); }		
   119 				
   120 				| TIMESEP AT string IS INTEGER si_unit AT FL int_array
   121 				{ intpt_string_int_cterm(PT_TIMESEP, $3, $5 * $6, 0, 0, $9); }				
   122 				| TIMESEP AT string_array IS INTEGER si_unit AT FL int_array
   123 				{ intpt_stringarray_int_cterm(PT_TIMESEP, $3, $5 * $6, 0, 0, $9); }		
   124 				| TIMESEP AT string IS INTEGER si_unit FROM INTEGER UNTIL INTEGER AT FL int_array	
   125 				{ intpt_string_int_cterm(PT_TIMESEP, $3, $5 * $6, $8, $10, $13); }				
   126 				| TIMESEP AT string_array IS INTEGER si_unit FROM INTEGER UNTIL INTEGER AT FL int_array
   127 				{ intpt_stringarray_int_cterm(PT_TIMESEP, $3, $5 * $6, $8, $10, $13); }	
   128 									
   129 				| cterm AND cterm							
   130 				{ and_term(); }
   131 ;
   132 
   133 si_unit:		STRING	
   134 				{ 
   135 					if (!strcmp($1, "s")) { $$ = 1; }
   136 					else if (!strcmp($1, "min")) { $$ = 60; }
   137 				}
   138 ;
   139 
   140 pttrn: 			PATTERN STRING '(' term ')'	{ $$ = $2; }
   141 				| PATTERN '(' term ')'		{ $$ = get_pattern_id(); }
   142 ;
   143 
   144 term:			int_pt int_op INTEGER	
   145 				{ intpt_int_term($2, $1, $3); }	
   146 					
   147 				| char_pt string_op string
   148 				{ charpt_char_term($2, $1, $3); }
   149 						
   150 				| string_pt string_op string
   151 				{ stringpt_string_term($2, $1, $3); }
   152 				
   153 				| string_array_pt string_array_op string_array
   154 				{ stringarraypt_stringarray_term($2, $1, $3); }	
   155 							
   156 				| string_array string_array_op string_array_pt
   157 				{ 
   158 					if ($2 == OT_IN_C)
   159 					{
   160 						$2 = OT_IN_P;
   161 					}
   162 					else if ($2 == OT_NOTIN_C)
   163 					{
   164 						$2 = OT_NOTIN_P;
   165 					}
   166 					stringarraypt_stringarray_term($2, $3, $1); 
   167 				}			
   168 					
   169 				| string string_string_array_op string_array_pt
   170 				{ 
   171 					if ($2 == OT_IN_C)
   172 					{
   173 						$2 = OT_IN_P;
   174 					}
   175 					else if ($2 == OT_NOTIN_C)
   176 					{
   177 						$2 = OT_NOTIN_P;
   178 					} 
   179 					stringarraypt_string_term($2, $3, $1); 
   180 				}		
   181 				
   182 				| string string_string_array_op char_array_pt
   183 				{ 
   184 					if ($2 == OT_IN_C)
   185 					{
   186 						$2 = OT_IN_P;
   187 					}
   188 					else if ($2 == OT_NOTIN_C)
   189 					{
   190 						$2 = OT_NOTIN_P;
   191 					} 
   192 					chararraypt_char_term($2, $3, $1); 
   193 				}	
   194 							
   195 				| term OR term 		
   196 				{ or_term(); }
   197 				
   198 				| term AND term
   199 				{ and_term(); } 	
   200 				
   201 				| NOT term						
   202 				| '(' term ')'				
   203 ;
   204 
   205 int_op:			IS 				{ $$ = OT_IS; }	
   206 				| IS NOT		{ $$ = OT_ISNOT; } 
   207 				| GREATER THAN	{ $$ = OT_GREATER; }
   208 				| LESS THAN		{ $$ = OT_LESS; }
   209 ;
   210 
   211 string_op:		IS			{ $$ = OT_IS; }	
   212 				| IS NOT	{ $$ = OT_ISNOT; } 
   213 ;
   214 
   215 string_array_op:IS			{ $$ = OT_IS; }
   216 				| IS NOT 	{ $$ = OT_ISNOT; } 
   217 				| IN 		{ $$ = OT_IN_P; }
   218 				| NOT IN	{ $$ = OT_NOTIN_P; }
   219 ;
   220 
   221 string_string_array_op: IN 			{ $$ = OT_IN_P; }
   222 						| NOT IN	{ $$ = OT_NOTIN_P; }
   223 ;
   224 
   225 int_array:		'[' ints ']' { $$ = integer_array(); }
   226 ;
   227 
   228 ints:			| ints ',' int
   229 				| ints int
   230 ;
   231 
   232 int:		INTEGER { integer($1); $$ = $1; }
   233 ; 
   234 
   235 string_array:	'[' strings ']'	{ $$ = string_array(); }
   236 ;
   237 
   238 strings:		| strings ',' string
   239 				| strings string
   240 ;
   241 
   242 int_pt:			TAS		{ $$ = PT_TAS; }			
   243 ;
   244 
   245 char_pt:		FTYP		{ $$ = PT_FTYP; }
   246 
   247 string_pt:		ADEP	{ $$ = PT_ADEP; }
   248 				| ADES	{ $$ = PT_ADES; }
   249 				| RWY 	{ $$ = PT_RWY; }
   250 				| ATYP 	{ $$ = PT_ATYP; }
   251 				| COPX	{ $$ = PT_COPX; }				
   252 				| FRUL 	{ $$ = PT_FRUL; }				
   253 				| RFL	{ $$ = PT_RFL; }
   254 				| TRAVEL_TYPE { $$ = PT_TRAVEL_TYPE; }
   255 ;
   256 
   257 string_array_pt:ROUTE	{ $$ = PT_ROUTE; }				
   258 ;
   259 
   260 char_array_pt:	EQUIP	{ $$ = PT_EQUIP; }
   261 ;
   262 
   263 string:			QUOTE STRING QUOTE	{ string($2); $$ = $2; }
   264 ;
   265 %%
   266 
   267 #include "atccl_parser.c"
   268 
   269 void clear_id_table()
   270 {
   271 	int i;
   272 	for (i = 0; i < id_table_no; ++i)
   273 	{
   274 		free(id_table[i]);
   275 		id_table[i] = 0;
   276 	}
   277 	id_table_no = 0;
   278 }
   279 
   280 int yywrap()
   281 {
   282 	clear_id_table();
   283 	return 1;
   284 }
   285 
   286 int semantic_error(const char* str)
   287 {
   288 	if (errorNo < maxErrorNo)
   289 	{
   290 		errors[errorNo].line = -1;
   291 		strcpy(errors[errorNo].text, "");
   292 		strcpy(errors[errorNo].error, str);		
   293 		errorNo++;
   294 	}		
   295 	return 1;
   296 }
   297 
   298 int yyerror(const char *str)
   299 {
   300 	if (errorNo < maxErrorNo)
   301 	{
   302 		errors[errorNo].line = line_no;
   303 		strcpy(errors[errorNo].text, yytext);
   304 		strcpy(errors[errorNo].error, str);		
   305 		errorNo++;
   306 	}		
   307 	return 1;
   308 }
   309 
   310 int parse_syntax(FILE* file, ErrorType* errors_, int maxErrorNo_, int passes)
   311 {
   312 	line_no = 1;
   313 	errors = errors_;
   314 	maxErrorNo = maxErrorNo_;
   315 	errorNo = 0;
   316 	
   317 	clear_all();
   318 	clear_id_table();		
   319 	yyrestart(file);
   320 	yyparse();
   321 	
   322 	if (passes > 1)
   323 	{	
   324 		if (!complete_semantics())
   325 		{
   326 			semantic_error(last_error());
   327 		}	
   328 	}	
   329 	return errorNo;
   330 }