]> www.wagner.pp.ru Git - oss/fgis.git/blob - lib/reclass.y
8bf9b941d1bc6aa1242bdd8c722e71b199bdfd59
[oss/fgis.git] / lib / reclass.y
1 %{
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include "reclass.h"
5 #include "epp.h"
6 #define YYSTYPE int
7 int yylex();
8 int yyerror();
9  RECLASS table;
10  int curval,startval,endval,loop_var;
11  int maxclass;
12  int interactive_parser=0;
13 %}
14 %expect 2
15 %token NUMBER
16 %%
17 list: 
18      statement 
19     | list eol statement 
20     ;
21
22 statement: /* empty */
23          | dest range 
24          | map_statement 
25          | error { if (interactive_parser) 
26                      yyerrok;
27                    else
28                      YYABORT;
29                   }
30
31 dest: value '=' { curval=$1; }
32      | '(' value ':' value ')' '=' { curval=-1; startval=$2 ; endval = $4; 
33                                loop_var=startval;}
34 eol: '\n' 
35    | '\r' '\n'
36 range:subrange
37      |range subrange
38 subrange:value { 
39                  if (curval>=0) table[$1]=curval;
40                     else {table[$1]=loop_var++;
41                           if (loop_var>endval) 
42                             loop_var=startval;
43                          }
44                  }
45      
46      | value ':' value { int i; for(i=$1;i<=$3;i++) 
47                          if (curval>=0) table[i]=curval;
48                          else {table[i]=loop_var++;
49                                if (loop_var>endval) 
50                                 loop_var=startval;
51                               }
52                         }     
53      ;
54 value: NUMBER {if ($1>65535) 
55                    { yyerror("Class value out of range\n");
56                      return 1;YYERROR;
57                    }
58  }
59 map_statement: value ':' value '=' value ':' value {
60                int i,start,stop,startv,stopv;
61                if ($5>$7) {start=$7;startv=$3;stop=$5;stopv=$1;}
62                    else   {start=$5;startv=$1;stop=$7;stopv=$3;}
63                for (i=start;i<=stop;i++)
64                 table[i]=(i-start)*(stopv-startv)/(start-stop)+startv;
65                }
66 ;
67
68 %%
69 int (*my_getc)();
70 int yylex()
71 { int c,numb=0,isnumb=0; 
72   static char unget_buf=0;
73  while ((c=unget_buf?unget_buf:(*my_getc)())!=EOF)
74  { unget_buf=0;
75    switch (c)
76    {
77     case '0':
78     case '1':
79     case '2': 
80     case '3':
81     case '4':
82     case '5':
83     case '6':
84     case '7':
85     case '8':
86     case '9': {isnumb=1; numb=numb*10+c-'0'; break;}
87     case ' ':
88     case '\t': {if (isnumb) { yylval=numb; return NUMBER; } break;}
89     default: if (isnumb) { yylval=numb; unget_buf=c; return NUMBER;}
90              else return c;
91  }
92  }
93  return 0;
94 }
95
96 int yyerror(char *s)
97 { fprintf(stderr,"%s\n",s);
98  return 0;
99 }
100 RECLASS make_reclass_table(EPP *infile,int (*recl_getc)())
101 { int size=epp_table_size(infile); 
102   
103   return parse_statements(size,create_reclass_table(size),recl_getc);
104 }
105 RECLASS parse_statements(int size,RECLASS src,int (*recl_getc)())
106 { my_getc=recl_getc;
107   table=src;
108   if (size>65535)size=65535;
109   maxclass=size;
110   if (yyparse()) { free(table);return NULL;}
111     else return table;
112 }
113 RECLASS create_reclass_table(size)
114 { RECLASS table;
115   int i;
116   if (size<=0) return NULL;
117   else if (size>65535) size=65535;
118   table=malloc((size+1)*sizeof(short int));
119   for(i=0;i<=size;i++) table[i]=i;
120   return table;
121 }
122 RECLASS wrapped_reclass(EPP *infile,int white)
123 { int i,maxclass;
124   RECLASS table;
125   maxclass=epp_table_size(infile);
126   table=malloc(maxclass*sizeof(short int));
127   table[0]=0;
128   for(i=1;i<=maxclass;i++) table[i]=(i-1)%(white-1)+1;
129   table[infile->offsite]=white;
130   return table;  
131 }