]> ruin.nu Git - popboot.git/blob - parser.y
more sane test
[popboot.git] / parser.y
1
2 %{
3     #include <ctype.h>
4     #include <string>
5     #include <iostream>
6     #include <sstream>
7     #include <fstream>
8     #include <vector>
9
10     #include "action.h"
11         #include "planner.h"
12
13     using namespace std;
14
15     int yylex (void);
16     void yyerror (char const *);
17
18     ifstream* input;
19     vector<Action>* actions;
20 %}
21
22
23
24 %union {
25     Preconditions* preconds;
26     Literals* literals;
27     EffectsMap* effects;
28     string* str;   /* STR tokens */
29     int num;       /* NUM tokena */
30 }
31
32 %type <str> id
33 %type <preconds> preconds
34 %type <str> exec
35 %type <effects> effects
36 %type <literals> literals 
37 %token <str> STR 
38 %token <num> NUM
39
40 %left ','
41
42 %%
43
44 input   :  /* empty */
45         | input action  
46         | input '\n'
47 ;
48
49 action  : id preconds '\n' exec effects '\n' {  
50     
51                                 // Detta vill inte funka för mig!  
52                                 actions->push_back( Action(*$1,*$2,*$4,*$5) ); 
53
54                                 //---------------------------------------------
55                                 // Debug test print :   
56                                 cout << "id:  " << *$1 << endl;
57
58                                 // Precondition flags:
59                                 cout << "a: " << (*$2)["NET"] << endl;
60                                 cout << "b: " << (*$2)["b"] << endl;
61                                 cout << "c: " << (*$2)["c"] << endl;
62                                 cout << "d: " << (*$2)["d"] << endl;
63                                 cout << "e: " << (*$2)["e"] << endl;
64
65                                 cout << "exe: " << *$4 << endl;
66
67                                 // Print number of effects
68                                 cout << "1: " << (*$5)[0].size() << endl;
69                                 cout << "99: " << (*$5)[99].size() << endl;
70                                 cout << "88: " << (*$5)[88].size() << endl;
71
72                                 cout << endl;
73                                 //----------------------------------------------
74                                             }
75         ;
76
77
78
79
80 id      : STR '\n'                  { $$ = $1; }
81         ;
82
83 /* Preconditions (literals with flag) */
84 preconds: preconds ','  preconds    {  
85                                         $$ = $1;
86                                         // $3 will only have on element
87                                         (*$$)[$3->begin()->first] =
88                                             $3->begin()->second;
89                                     }
90         | '?' STR                   {   
91                                         $$ = new Preconditions();
92                                         (*$$)[*$2] = false ;
93                                     } 
94         | STR                       {   
95                                         $$ = new Preconditions();
96                                         (*$$)[*$1] = true ;
97                                     }
98         ;
99
100 /* Executable */
101 exec    : STR '\n'                  { $$ = $1; }
102         ;
103
104 /* EffectMap */
105 effects : effects NUM ':' literals '\n' {
106                                         $$ = $1;
107                                         (*$$)[$2] = *$4;
108                                         }
109         | NUM ':' literals '\n'         {
110                                         $$ = new EffectsMap();
111                                         (*$$)[$1] = *$3 ;
112                                         }
113         ;
114
115
116 /* List of literals (effects) */
117 literals: literals ',' literals     { 
118                                         $$ = $1;
119                                         $$->push_back( *($3->begin()) ); 
120                                     }
121         | STR                       { 
122                                         $$ = new Literals();
123                                         $$->push_back(*$1); 
124                                     }
125         ;
126
127
128 %%
129
130 int 
131 yylex (void)
132 {
133     string str;
134
135     // Check for end of file
136     if ( input->eof() )
137         return 0;
138
139     // Ignore spaces
140     while ( input->peek()==' ' || input->peek()=='\t')
141         input->get();
142
143     // Ignore comments
144     while ( input->peek()=='#')
145         while ( input->peek()!='\n' )
146             input->get();
147
148
149
150
151     // Numbers
152     if ( isdigit(input->peek()) )
153     {
154         *input >> yylval.num;
155         return NUM;
156     }
157
158     // Alpha strings
159     if ( isalpha(input->peek()) )
160     {  
161         string* str = new string();
162         
163         int c = input->peek();
164         while (isalpha(c) || isdigit(c) || c=='_' || c=='.' )   
165         {
166             *str += input->get();
167             c = input->peek();
168         }
169         yylval.str = str;
170         return STR;
171     }
172
173     // String with spaces and other special characters
174     if ( input->peek()=='"' )
175     {
176         input->get();               // Consume "-character
177         string* str = new string();
178         while (input->peek()!='"')
179         {
180             *str += input->get();
181         }
182         input->get();               // Consume "-character
183
184         yylval.str = str;
185         return STR;
186     }
187
188     /* Return single char */
189     return input->get();           
190 }
191
192
193 void 
194 yyerror (char const *s)
195 {
196     cerr << "Parse error : " << s;
197 }
198
199 vector<string> stringToVector(string str){
200         vector<string> strings;
201
202         istringstream ist(str);
203         while (ist >> str){
204                 strings.push_back(str);
205         }
206
207         return strings;
208 }
209
210 int 
211 main (int argc, char** argv)
212 {
213
214     if (argc!=4)
215     {
216         cout << "Syntax: " << argv[0] << 
217                 " <file> \"init state\" \"goal state\"" << endl;
218         return 1;
219     }
220
221     ifstream file(argv[1]);
222     if (!file)
223     {
224         cerr << "Unable to open file: " << argv[1] << endl;
225         exit(2);
226     }
227
228     // Set global variables
229     input = &file;
230     actions = new vector<Action>();
231
232     yyparse();
233
234         Planner p(*actions, stringToVector(argv[2]), stringToVector(argv[3]));
235         p.execute();
236 }