Parsing SPARQL Queries

I need to test a specific structural property from several million SPARQL queries, and for this I need a WHERE statement structure. I'm currently trying to use fyzz for this, but unfortunately its documentation is not very useful. Parsing queries is easy, the problem is that I could not restore the structure of the operator. For instance:

 >>> from fyzz import parse >>> a=parse("SELECT * WHERE {?xa ?y . {?xa ?z}}") >>> b=parse("SELECT * WHERE {?xa ?y OPTIONAL {?xa ?z}}") >>> a.where==b.where True >>> a.where [(SparqlVar('x'), ('', 'a'), SparqlVar('y')), (SparqlVar('x'), ('', 'a'), SparqlVar('y'))] 

Is there a way to restore the actual syntax tree in fyzz, not just triples, or some other tool that would allow me to do this? RDFLib used to have a SPARQL parser, but I can't find it in rdflib or rdfextras.sparql .

thanks

+4
source share
2 answers

Another tool is the roqet tool, which is packaged in rasqal . This is a command line tool that returns a processed tree. For instance:

roqet -i laqrs -d structure -n -e "SELECT * WHERE {?xa ?y OPTIONAL {?xa ?z}}"

will be displayed ..

 Query: query verb: SELECT query bound variables (3): x, y, z query Group graph pattern[0] { sub-graph patterns (2) { Basic graph pattern[1] #0 { triples { triple #0 { triple(variable(x), uri<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>, variable(y)) } } } Optional graph pattern[2] #1 { sub-graph patterns (1) { Basic graph pattern[3] #0 { triples { triple #0 { triple(variable(x), uri<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>, variable(z)) } } } } } } } 

Looking at your comment in another answer, I don't think this is what you need. And I don’t think you will find an answer looking at the SPARQL parser. The evaluation of an object (or triple pattern) in a query takes place inside Query Engines , which in well-designed systems is isolated from query parsing.

For example, in 4store, you can see the 4s-query command with the -vvv parameter (very verbose), where you will see the output of how the query was executed and how the substitutions were performed for each evaluation of the three templates.

+5
source

ANTLR has a SPARQL grammar: http://www.antlr.org/grammar/1200929755392/index.html

ANTLR can generate parsing code to run Python.

+3
source

All Articles