This is the Python version of sparql-grammar library which was originally written in Ruby.
>>> from pysparql import Parser
>>> query = "SELECT ?name WHERE { ?x rdf:type foaf:Person . ?x foaf:name ?name }"
>>> p = Parser(query)
>>> p.parse()
>>> print p.result
['project', [Variable(?name)], Query(None)[['triple', Variable(?x), URI(type), URI(Person)], ['triple', Variable(?x), URI(name), Variable(?name)]]]
PySPARQL is freely available for all uses under a BSD license.