Skip to content
Hirokazu Chiba edited this page Dec 8, 2023 · 14 revisions

An excerpt from the EBNF of SPARQL and the corresponding PEG rules

SPARQL = QueryUnit / UpdateUnit
// [1]
QueryUnit = Query
// [2]
Query = p:Prologue WS* q:( SelectQuery / ConstructQuery / DescribeQuery / AskQuery ) v:ValuesClause
{
  let ast = { type: 'Query' };
  if (p.length) {
    ast.prologue = p;
  }
  ast.queryBody = q;
  if (v) {
    ast.values = v;
  }
  return ast;
}
// [3]
UpdateUnit = Update
// [4]
Prologue = ( BaseDecl / PrefixDecl )*

Input SPARQL

SELECT * WHERE { ?s ?p ?o } LIMIT 10

Formatted SPARQL

SELECT *
WHERE {
  ?s ?p ?o .
}
LIMIT 10

JSON-LD context

{
  "@context": {
    "@vocab": "https://purl.org/sparql-formatter/ontology#",
    "type": "@type",
    "dataType": {
      "@type": "@id"
    }
  }
}

JSON

{
  "type": "Query",
  "queryBody": {
    "select": [
      "*"
    ],
    "where": [
      {
        "type": "TriplesBlock",
        "triplePattern": [
          {
            "subject": {
              "variable": "s"
            },
            "properties": [
              {
                "predicate": {
                  "variable": "p"
                },
                "objects": [
                  {
                    "variable": "o"
                  }
                ]
              }
            ]
          }
        ]
      }
    ],
    "limitOffset": [
      {
        "limit": 10
      }
    ]
  }
}

Turtle

@prefix : <https://purl.org/sparql-formatter/ontology#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

[] a :Query ;
   :queryBody [
       :select "*" ;
       :where [
           a :TriplesBlock ;
           :triplePattern [
               :subject [
                   :variable "s"
               ] ;
               :properties [
                   :predicate [
                       :variable "p"
                   ] ;
                   :objects [
                       :variable "o"
                   ]
               ]
           ]
       ] ;
       :limitOffset [
           :limit 10
       ]
   ] .

Clone this wiki locally