title: Sql to Pymongo generation using Pyparsing
body: <p>I am using the "awesomest" parsing library in the world existing right now. Pyparsing. The problem at hand is to generate a PyMongo dictionary from a given SQL string (For select statements). The grammar def I am using is following :</p>
  
  <pre><code>sql_stmt = (select_key_word + ('*' | column_list).setResultsName
                   ("columns") + form_key_word + table_name_list.setResultsName
                   ("collections") + 
                   Optional(where_condition, "").setResultsName("where"))
  </code></pre>
  
  <p>Here the select_key_word, column_list etc. constructs are valid grammar defs. and using this i can parse a string like "Select * from collection_1 where (Sal = 1000 or Sal=5000) AND Car>2"
  The problem i have is that, the where part is being parsed is like this :</p>
  
  <pre><code>[[u'where', [u'(', [u'Sal', '=', u'1000'], 'or', [u'Sal', '=', u'5000'], u')'], 'and', [u'Car', '&gt;', u'2']]] 
  </code></pre>
  
  <p>Which is fine if i want it translated into something sqlish. But a valid representation of that same in pymongo would be something like this :</p>
  
  <pre><code>{u'$or': [{u'$and': [{u'Sal': u'1000'}, {u'Sal': u'5000'}]}, {u'Car': {u'$gte': u'2'}}]}
  </code></pre>
  
  <p>That is where I am stuck. Can anybody give me a direction? it seems to me that setParseAction will be a way to go, but just can't figure that out</p>
  
  <p>the code for the where_contidion is :</p>
  
  <pre><code>where_expr = Forward()
  and_keyword = get_conjunction_as_grammar("and")
  or_keyword = get_conjunction_as_grammar("or")
  in_operation = get_operation_as_grammar("in")
  
  column_value = get_real_number_as_grammar() | get_int_as_grammar() | \
                  quotedString
  binary_operator = get_bin_op_as_grammar()
  col_name = get_column_name_as_grammar()
  
  where_condn = Group(
  (col_name + binary_operator + column_value) |
  (col_name + in_operation + "(" + delimitedList(column_value) + ")" ) |
  ("(" + where_expr + ")")
  )
  where_expr &lt;&lt; where_condn + ZeroOrMore((and_keyword | or_keyword) 
                                          + where_expr)
  
  where_condition = Group(CaselessLiteral("where") + where_expr)
  </code></pre>
  
  <p>Thanks in advance. Please let me know if you need any other information.</p>