]> ruin.nu Git - popboot.git/blobdiff - parser.y
Spelling fix in inittab
[popboot.git] / parser.y
index e286054cb2fd1319ac0657f541f8d8f2fe21f95a..ad68377a79770ad4c4403dd80e29bffc68d682d6 100644 (file)
--- a/parser.y
+++ b/parser.y
@@ -6,17 +6,21 @@
     #include <sstream>
     #include <fstream>
     #include <vector>
+       #include <cstdlib>
 
     #include "action.h"
        #include "planner.h"
 
     using namespace std;
 
+    int yyparse(void);
     int yylex (void);
     void yyerror (char const *);
 
     ifstream* input;
+    int line = 1;
     vector<Action>* actions;
+    EffectsMap*            runlevels;
 %}
 
 
 
 %%
 
-input  :  /* empty */
-       | input action  
-       | input '\n'
-;
+input  : runlvls '\n' actions 
+       ;
+
+
+runlvls        : '\n' runlvls
+       | effects                   { runlevels = $1; } 
+       ;
+
+actions        :  /* empty */
+       | actions action                 
+       | actions '\n'
+       ;
+
 
 action : id preconds '\n' exec effects '\n' {  
     
-                               // Detta vill inte funka för mig!  
                                actions->push_back( Action(*$1,*$2,*$4,*$5) ); 
 
                                //---------------------------------------------
                                // Debug test print :   
-                               cout << "id:  " << *$1 << endl;
+/*                             cerr << "id:  " << *$1 << endl;
 
                                // Precondition flags:
-                               cout << "a: " << (*$2)["NET"] << endl;
-                               cout << "b: " << (*$2)["b"] << endl;
-                               cout << "c: " << (*$2)["c"] << endl;
-                               cout << "d: " << (*$2)["d"] << endl;
-                               cout << "e: " << (*$2)["e"] << endl;
+                               cerr << "a: " << (*$2)["a"] << endl;
+                               cerr << "b: " << (*$2)["b"] << endl;
+                               cerr << "c: " << (*$2)["c"] << endl;
+                               cerr << "d: " << (*$2)["d"] << endl;
+                               cerr << "e: " << (*$2)["e"] << endl;
 
-                               cout << "exe: " << *$4 << endl;
+                               cerr << "exe: " << *$4 << endl;
 
                                // Print number of effects
-                               cout << "1: " << (*$5)[0].size() << endl;
-                               cout << "99: " << (*$5)[99].size() << endl;
-                               cout << "88: " << (*$5)[88].size() << endl;
+                               cerr << "0: " << (*$5)[0].size() << endl;
+                               cerr << "99: " << (*$5)[99].size() << endl;
+                               cerr << "88: " << (*$5)[88].size() << endl;
 
-                               cout << endl;
-                               //----------------------------------------------
+                               cerr << endl;
+*/                             //----------------------------------------------
                                            }
        ;
 
@@ -81,7 +93,8 @@ id    : STR '\n'                  { $$ = $1; }
        ;
 
 /* Preconditions (literals with flag) */
-preconds: preconds ','         preconds    {  
+preconds: /* empty */              {   $$ = new Preconditions(); }
+       | preconds ','  preconds    {  
                                        $$ = $1;
                                        // $3 will only have on element
                                        (*$$)[$3->begin()->first] =
@@ -186,14 +199,17 @@ yylex (void)
     }
 
     /* Return single char */
-    return input->get();          
+    int c = input->get();         
+    if ( c=='\n' )
+       ++line;
+    return c;
 }
 
 
 void 
 yyerror (char const *s)
 {
-    cerr << "Parse error : " << s;
+    cerr << "Line " << line << ": " << s << endl;
 }
 
 vector<string> stringToVector(string str){
@@ -201,7 +217,6 @@ vector<string> stringToVector(string str){
 
        istringstream ist(str);
        while (ist >> str){
-               cout << str << endl;
                strings.push_back(str);
        }
 
@@ -212,13 +227,14 @@ int
 main (int argc, char** argv)
 {
 
-    if (argc!=4)
+    if (argc < 3)
     {
-       cout << "Syntax: " << argv[0] << 
-               " <file> \"init state\" \"goal state\"" << endl;
+       cerr << "Syntax: " << argv[0] << 
+               " <file> <runlevel> \"init state\"" << endl;
        return 1;
     }
 
+       Literals init = argc >= 4 ? stringToVector(argv[3]) : Literals();
     ifstream file(argv[1]);
     if (!file)
     {
@@ -229,9 +245,18 @@ main (int argc, char** argv)
     // Set global variables
     input = &file;
     actions = new vector<Action>();
+    runlevels = new EffectsMap();
 
     yyparse();
 
-       Planner p(*actions, stringToVector(argv[2]), stringToVector(argv[3]));
-       p.execute();
+/*    cerr << (*runlevels)[2].size() << endl;
+    cerr << (*runlevels)[3].size() << endl;
+    cerr << (*runlevels)[4].size() << endl;
+    cerr << (*runlevels)[5].size() << endl;
+*/
+    Planner p(*actions, init, (*runlevels)[atoi(argv[2])]);
+    p.execute();
+
+    delete actions;
+       delete runlevels;
 }