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