Stand alone SPARQL Parsing

semsol edited this page Mar 14, 2011 · 2 revisions

ARC's SPARQL parser covers the complete SPARQL grammar and can be used as a stand-alone component:

/* ARC2 static class inclusion */ 
include_once('path/to/arc/ARC2.php');

/* parser instantiation */
$parser = ARC2::getSPARQLParser();

/* parse a query */
$q = 'PREFIX ... SELECT ...'
$parser->parse($q);
if (!$parser->getErrors()) {
  $q_infos = $parser->getQueryInfos();
  print_r($q_infos);
}
else {
  echo "invalid query: " . print_r($parser->getErrors());
}

The $q_infos array contains structured information about the parsed query, e.g.:

PREFIX foaf: <http://xmlns.com/foaf/0.1/>

SELECT * WHERE {
  ?x a foaf:Person ;
     foaf:name ?name ;
     foaf:knows ?y .
  FILTER regex(?name, "^A") 
}
ORDER BY ASC(?name)
LIMIT 10
Array

 [base] => http://localhost/arc2/tests/sparql_parser.php
 [vars] => Array
   [0] => x
   [1] => name
   [2] => y

 [prefixes] => Array
   [rdf:] => http://www.w3.org/1999/02/22-rdf-syntax-ns#
   [rdfs:] => http://www.w3.org/2000/01/rdf-schema#
   [owl:] => http://www.w3.org/2002/07/owl#
   [xsd:] => http://www.w3.org/2001/XMLSchema#
   [foaf:] => http://xmlns.com/foaf/0.1/

 [query] => Array
   [type] => select
   [result_vars] => Array
     [0] => Array
       [var] => x
       [aggregate] => 0
       [alias] => 

     [1] => Array
       [var] => name
       [aggregate] => 0
       [alias] => 

     [2] => Array
       [var] => y
       [aggregate] => 0
       [alias] => 


   [dataset] => Array

   [pattern] => Array
     [type] => group
     [patterns] => Array
       [0] => Array
         [type] => triples
         [patterns] => Array
           [0] => Array
             [type] => triple
             [s] => x
             [p] => http://www.w3.org/1999/02/22-rdf-syntax-ns#type
             [o] => http://xmlns.com/foaf/0.1/Person
             [s_type] => var
             [p_type] => iri
             [o_type] => iri
             [o_datatype] => 
             [o_lang] => 

           [1] => Array
             [type] => triple
             [s] => x
             [p] => http://xmlns.com/foaf/0.1/name
             [o] => name
             [s_type] => var
             [p_type] => iri
             [o_type] => var
             [o_datatype] => 
             [o_lang] => 

           [2] => Array
             [type] => triple
             [s] => x
             [p] => http://xmlns.com/foaf/0.1/knows
             [o] => y
             [s_type] => var
             [p_type] => iri
             [o_type] => var
             [o_datatype] => 
             [o_lang] => 

       [1] => Array
         [type] => filter
         [constraint] => Array
           [type] => built_in_call
           [call] => regex
           [args] => Array
             [0] => Array
               [value] => name
               [type] => var
               [operator] => 

             [1] => Array
               [value] => ^A
               [type] => literal2
               [operator] => 



   [order_infos] => Array
     [0] => Array
       [value] => name
       [type] => var
       [operator] => 
       [direction] => asc


   [limit] => 10