title: (SOLVED) rdflib : ParseException: Expected {SelectQuery | ConstructQuery | DescribeQuery | AskQuery}, found 's' (at char 17), (line:2, col:17)
body: <p>Working on several SPARQL queries that works, we are facing an issue with the rdflib library, with queries containing projection with <em>str</em> function.</p>
  <p>Here is an example of code on which the parser fail :</p>
  <pre><code>query = &quot;&quot;&quot;
  SELECT DISTINCT str(?s) 
  WHERE { ?s ?p ?o. }
  &quot;&quot;&quot;
  parsedQuery = rdflib.plugins.sparql.parser.parseQuery(query)
  </code></pre>
  <p>the error is :</p>
  <pre><code>---------------------------------------------------------------------------
  ParseException                            Traceback (most recent call last)
  &lt;ipython-input-47-30dec35045be&gt; in &lt;module&gt;
        3 WHERE { ?s ?p ?o. }
        4 &quot;&quot;&quot;
  ----&gt; 5 parsedQuery = rdflib.plugins.sparql.parser.parseQuery(query)
  
  ~/anaconda3/lib/python3.7/site-packages/rdflib/plugins/sparql/parser.py in parseQuery(q)
     1058 
     1059     q = expandUnicodeEscapes(q)
  -&gt; 1060     return Query.parseString(q, parseAll=True)
     1061 
     1062 
  
  ~/anaconda3/lib/python3.7/site-packages/pyparsing.py in parseString(self, instring, parseAll)
     1945             else:
     1946                 # catch and re-raise exception from here, clears out pyparsing internal stack trace
  -&gt; 1947                 raise exc
     1948         else:
     1949             return tokens
  
  ~/anaconda3/lib/python3.7/site-packages/pyparsing.py in parseString(self, instring, parseAll)
     1935             instring = instring.expandtabs()
     1936         try:
  -&gt; 1937             loc, tokens = self._parse(instring, 0)
     1938             if parseAll:
     1939                 loc = self.preParse(instring, loc)
  
  ~/anaconda3/lib/python3.7/site-packages/pyparsing.py in _parseCache(self, instring, loc, doActions, callPreParse)
     1836                 ParserElement.packrat_cache_stats[MISS] += 1
     1837                 try:
  -&gt; 1838                     value = self._parseNoCache(instring, loc, doActions, callPreParse)
     1839                 except ParseBaseException as pe:
     1840                     # cache a copy of the exception, without the traceback
  
  ~/anaconda3/lib/python3.7/site-packages/pyparsing.py in _parseNoCache(self, instring, loc, doActions, callPreParse)
     1675             if self.mayIndexError or preloc &gt;= len(instring):
     1676                 try:
  -&gt; 1677                     loc, tokens = self.parseImpl(instring, preloc, doActions)
     1678                 except IndexError:
     1679                     raise ParseException(instring, len(instring), self.errmsg, self)
  
  ~/anaconda3/lib/python3.7/site-packages/pyparsing.py in parseImpl(self, instring, loc, doActions)
     4050                     raise ParseSyntaxException(instring, len(instring), self.errmsg, self)
     4051             else:
  -&gt; 4052                 loc, exprtokens = e._parse(instring, loc, doActions)
     4053             if exprtokens or exprtokens.haskeys():
     4054                 resultlist += exprtokens
  
  ~/anaconda3/lib/python3.7/site-packages/pyparsing.py in _parseCache(self, instring, loc, doActions, callPreParse)
     1836                 ParserElement.packrat_cache_stats[MISS] += 1
     1837                 try:
  -&gt; 1838                     value = self._parseNoCache(instring, loc, doActions, callPreParse)
     1839                 except ParseBaseException as pe:
     1840                     # cache a copy of the exception, without the traceback
  
  ~/anaconda3/lib/python3.7/site-packages/pyparsing.py in _parseNoCache(self, instring, loc, doActions, callPreParse)
     1675             if self.mayIndexError or preloc &gt;= len(instring):
     1676                 try:
  -&gt; 1677                     loc, tokens = self.parseImpl(instring, preloc, doActions)
     1678                 except IndexError:
     1679                     raise ParseException(instring, len(instring), self.errmsg, self)
  
  ~/anaconda3/lib/python3.7/site-packages/pyparsing.py in parseImpl(self, instring, loc, doActions)
     4250             if maxException is not None:
     4251                 maxException.msg = self.errmsg
  -&gt; 4252                 raise maxException
     4253             else:
     4254                 raise ParseException(instring, loc, &quot;no defined alternatives to match&quot;, self)
  
  ~/anaconda3/lib/python3.7/site-packages/pyparsing.py in parseImpl(self, instring, loc, doActions)
     4235         for e in self.exprs:
     4236             try:
  -&gt; 4237                 ret = e._parse(instring, loc, doActions)
     4238                 return ret
     4239             except ParseException as err:
  
  ~/anaconda3/lib/python3.7/site-packages/pyparsing.py in _parseCache(self, instring, loc, doActions, callPreParse)
     1836                 ParserElement.packrat_cache_stats[MISS] += 1
     1837                 try:
  -&gt; 1838                     value = self._parseNoCache(instring, loc, doActions, callPreParse)
     1839                 except ParseBaseException as pe:
     1840                     # cache a copy of the exception, without the traceback
  
  ~/anaconda3/lib/python3.7/site-packages/pyparsing.py in _parseNoCache(self, instring, loc, doActions, callPreParse)
     1675             if self.mayIndexError or preloc &gt;= len(instring):
     1676                 try:
  -&gt; 1677                     loc, tokens = self.parseImpl(instring, preloc, doActions)
     1678                 except IndexError:
     1679                     raise ParseException(instring, len(instring), self.errmsg, self)
  
  ~/anaconda3/lib/python3.7/site-packages/pyparsing.py in parseImpl(self, instring, loc, doActions)
     4443     def parseImpl(self, instring, loc, doActions=True):
     4444         if self.expr is not None:
  -&gt; 4445             return self.expr._parse(instring, loc, doActions, callPreParse=False)
     4446         else:
     4447             raise ParseException(&quot;&quot;, loc, self.errmsg, self)
  
  ~/anaconda3/lib/python3.7/site-packages/pyparsing.py in _parseCache(self, instring, loc, doActions, callPreParse)
     1836                 ParserElement.packrat_cache_stats[MISS] += 1
     1837                 try:
  -&gt; 1838                     value = self._parseNoCache(instring, loc, doActions, callPreParse)
     1839                 except ParseBaseException as pe:
     1840                     # cache a copy of the exception, without the traceback
  
  ~/anaconda3/lib/python3.7/site-packages/pyparsing.py in _parseNoCache(self, instring, loc, doActions, callPreParse)
     1675             if self.mayIndexError or preloc &gt;= len(instring):
     1676                 try:
  -&gt; 1677                     loc, tokens = self.parseImpl(instring, preloc, doActions)
     1678                 except IndexError:
     1679                     raise ParseException(instring, len(instring), self.errmsg, self)
  
  ~/anaconda3/lib/python3.7/site-packages/pyparsing.py in parseImpl(self, instring, loc, doActions)
     4050                     raise ParseSyntaxException(instring, len(instring), self.errmsg, self)
     4051             else:
  -&gt; 4052                 loc, exprtokens = e._parse(instring, loc, doActions)
     4053             if exprtokens or exprtokens.haskeys():
     4054                 resultlist += exprtokens
  
  ~/anaconda3/lib/python3.7/site-packages/pyparsing.py in _parseCache(self, instring, loc, doActions, callPreParse)
     1836                 ParserElement.packrat_cache_stats[MISS] += 1
     1837                 try:
  -&gt; 1838                     value = self._parseNoCache(instring, loc, doActions, callPreParse)
     1839                 except ParseBaseException as pe:
     1840                     # cache a copy of the exception, without the traceback
  
  ~/anaconda3/lib/python3.7/site-packages/pyparsing.py in _parseNoCache(self, instring, loc, doActions, callPreParse)
     1675             if self.mayIndexError or preloc &gt;= len(instring):
     1676                 try:
  -&gt; 1677                     loc, tokens = self.parseImpl(instring, preloc, doActions)
     1678                 except IndexError:
     1679                     raise ParseException(instring, len(instring), self.errmsg, self)
  
  ~/anaconda3/lib/python3.7/site-packages/pyparsing.py in parseImpl(self, instring, loc, doActions)
     4250             if maxException is not None:
     4251                 maxException.msg = self.errmsg
  -&gt; 4252                 raise maxException
     4253             else:
     4254                 raise ParseException(instring, loc, &quot;no defined alternatives to match&quot;, self)
  
  ~/anaconda3/lib/python3.7/site-packages/pyparsing.py in parseImpl(self, instring, loc, doActions)
     4235         for e in self.exprs:
     4236             try:
  -&gt; 4237                 ret = e._parse(instring, loc, doActions)
     4238                 return ret
     4239             except ParseException as err:
  
  ~/anaconda3/lib/python3.7/site-packages/pyparsing.py in _parseCache(self, instring, loc, doActions, callPreParse)
     1836                 ParserElement.packrat_cache_stats[MISS] += 1
     1837                 try:
  -&gt; 1838                     value = self._parseNoCache(instring, loc, doActions, callPreParse)
     1839                 except ParseBaseException as pe:
     1840                     # cache a copy of the exception, without the traceback
  
  ~/anaconda3/lib/python3.7/site-packages/pyparsing.py in _parseNoCache(self, instring, loc, doActions, callPreParse)
     1675             if self.mayIndexError or preloc &gt;= len(instring):
     1676                 try:
  -&gt; 1677                     loc, tokens = self.parseImpl(instring, preloc, doActions)
     1678                 except IndexError:
     1679                     raise ParseException(instring, len(instring), self.errmsg, self)
  
  ~/anaconda3/lib/python3.7/site-packages/pyparsing.py in parseImpl(self, instring, loc, doActions)
     4678         if check_ender:
     4679             try_not_ender(instring, loc)
  -&gt; 4680         loc, tokens = self_expr_parse(instring, loc, doActions, callPreParse=False)
     4681         try:
     4682             hasIgnoreExprs = (not not self.ignoreExprs)
  
  ~/anaconda3/lib/python3.7/site-packages/pyparsing.py in _parseCache(self, instring, loc, doActions, callPreParse)
     1836                 ParserElement.packrat_cache_stats[MISS] += 1
     1837                 try:
  -&gt; 1838                     value = self._parseNoCache(instring, loc, doActions, callPreParse)
     1839                 except ParseBaseException as pe:
     1840                     # cache a copy of the exception, without the traceback
  
  ~/anaconda3/lib/python3.7/site-packages/pyparsing.py in _parseNoCache(self, instring, loc, doActions, callPreParse)
     1675             if self.mayIndexError or preloc &gt;= len(instring):
     1676                 try:
  -&gt; 1677                     loc, tokens = self.parseImpl(instring, preloc, doActions)
     1678                 except IndexError:
     1679                     raise ParseException(instring, len(instring), self.errmsg, self)
  
  ~/anaconda3/lib/python3.7/site-packages/pyparsing.py in parseImpl(self, instring, loc, doActions)
     4443     def parseImpl(self, instring, loc, doActions=True):
     4444         if self.expr is not None:
  -&gt; 4445             return self.expr._parse(instring, loc, doActions, callPreParse=False)
     4446         else:
     4447             raise ParseException(&quot;&quot;, loc, self.errmsg, self)
  
  ~/anaconda3/lib/python3.7/site-packages/pyparsing.py in _parseCache(self, instring, loc, doActions, callPreParse)
     1836                 ParserElement.packrat_cache_stats[MISS] += 1
     1837                 try:
  -&gt; 1838                     value = self._parseNoCache(instring, loc, doActions, callPreParse)
     1839                 except ParseBaseException as pe:
     1840                     # cache a copy of the exception, without the traceback
  
  ~/anaconda3/lib/python3.7/site-packages/pyparsing.py in _parseNoCache(self, instring, loc, doActions, callPreParse)
     1675             if self.mayIndexError or preloc &gt;= len(instring):
     1676                 try:
  -&gt; 1677                     loc, tokens = self.parseImpl(instring, preloc, doActions)
     1678                 except IndexError:
     1679                     raise ParseException(instring, len(instring), self.errmsg, self)
  
  ~/anaconda3/lib/python3.7/site-packages/pyparsing.py in parseImpl(self, instring, loc, doActions)
     4443     def parseImpl(self, instring, loc, doActions=True):
     4444         if self.expr is not None:
  -&gt; 4445             return self.expr._parse(instring, loc, doActions, callPreParse=False)
     4446         else:
     4447             raise ParseException(&quot;&quot;, loc, self.errmsg, self)
  
  ~/anaconda3/lib/python3.7/site-packages/pyparsing.py in _parseCache(self, instring, loc, doActions, callPreParse)
     1836                 ParserElement.packrat_cache_stats[MISS] += 1
     1837                 try:
  -&gt; 1838                     value = self._parseNoCache(instring, loc, doActions, callPreParse)
     1839                 except ParseBaseException as pe:
     1840                     # cache a copy of the exception, without the traceback
  
  ~/anaconda3/lib/python3.7/site-packages/pyparsing.py in _parseNoCache(self, instring, loc, doActions, callPreParse)
     1675             if self.mayIndexError or preloc &gt;= len(instring):
     1676                 try:
  -&gt; 1677                     loc, tokens = self.parseImpl(instring, preloc, doActions)
     1678                 except IndexError:
     1679                     raise ParseException(instring, len(instring), self.errmsg, self)
  
  ~/anaconda3/lib/python3.7/site-packages/pyparsing.py in parseImpl(self, instring, loc, doActions)
     4250             if maxException is not None:
     4251                 maxException.msg = self.errmsg
  -&gt; 4252                 raise maxException
     4253             else:
     4254                 raise ParseException(instring, loc, &quot;no defined alternatives to match&quot;, self)
  
  ~/anaconda3/lib/python3.7/site-packages/pyparsing.py in parseImpl(self, instring, loc, doActions)
     4235         for e in self.exprs:
     4236             try:
  -&gt; 4237                 ret = e._parse(instring, loc, doActions)
     4238                 return ret
     4239             except ParseException as err:
  
  ~/anaconda3/lib/python3.7/site-packages/pyparsing.py in _parseCache(self, instring, loc, doActions, callPreParse)
     1836                 ParserElement.packrat_cache_stats[MISS] += 1
     1837                 try:
  -&gt; 1838                     value = self._parseNoCache(instring, loc, doActions, callPreParse)
     1839                 except ParseBaseException as pe:
     1840                     # cache a copy of the exception, without the traceback
  
  ~/anaconda3/lib/python3.7/site-packages/pyparsing.py in _parseNoCache(self, instring, loc, doActions, callPreParse)
     1675             if self.mayIndexError or preloc &gt;= len(instring):
     1676                 try:
  -&gt; 1677                     loc, tokens = self.parseImpl(instring, preloc, doActions)
     1678                 except IndexError:
     1679                     raise ParseException(instring, len(instring), self.errmsg, self)
  
  ~/anaconda3/lib/python3.7/site-packages/pyparsing.py in parseImpl(self, instring, loc, doActions)
     4443     def parseImpl(self, instring, loc, doActions=True):
     4444         if self.expr is not None:
  -&gt; 4445             return self.expr._parse(instring, loc, doActions, callPreParse=False)
     4446         else:
     4447             raise ParseException(&quot;&quot;, loc, self.errmsg, self)
  
  ~/anaconda3/lib/python3.7/site-packages/pyparsing.py in _parseCache(self, instring, loc, doActions, callPreParse)
     1836                 ParserElement.packrat_cache_stats[MISS] += 1
     1837                 try:
  -&gt; 1838                     value = self._parseNoCache(instring, loc, doActions, callPreParse)
     1839                 except ParseBaseException as pe:
     1840                     # cache a copy of the exception, without the traceback
  
  ~/anaconda3/lib/python3.7/site-packages/pyparsing.py in _parseNoCache(self, instring, loc, doActions, callPreParse)
     1675             if self.mayIndexError or preloc &gt;= len(instring):
     1676                 try:
  -&gt; 1677                     loc, tokens = self.parseImpl(instring, preloc, doActions)
     1678                 except IndexError:
     1679                     raise ParseException(instring, len(instring), self.errmsg, self)
  
  ~/anaconda3/lib/python3.7/site-packages/pyparsing.py in parseImpl(self, instring, loc, doActions)
     4250             if maxException is not None:
     4251                 maxException.msg = self.errmsg
  -&gt; 4252                 raise maxException
     4253             else:
     4254                 raise ParseException(instring, loc, &quot;no defined alternatives to match&quot;, self)
  
  ~/anaconda3/lib/python3.7/site-packages/pyparsing.py in parseImpl(self, instring, loc, doActions)
     4235         for e in self.exprs:
     4236             try:
  -&gt; 4237                 ret = e._parse(instring, loc, doActions)
     4238                 return ret
     4239             except ParseException as err:
  
  ~/anaconda3/lib/python3.7/site-packages/pyparsing.py in _parseCache(self, instring, loc, doActions, callPreParse)
     1836                 ParserElement.packrat_cache_stats[MISS] += 1
     1837                 try:
  -&gt; 1838                     value = self._parseNoCache(instring, loc, doActions, callPreParse)
     1839                 except ParseBaseException as pe:
     1840                     # cache a copy of the exception, without the traceback
  
  ~/anaconda3/lib/python3.7/site-packages/pyparsing.py in _parseNoCache(self, instring, loc, doActions, callPreParse)
     1675             if self.mayIndexError or preloc &gt;= len(instring):
     1676                 try:
  -&gt; 1677                     loc, tokens = self.parseImpl(instring, preloc, doActions)
     1678                 except IndexError:
     1679                     raise ParseException(instring, len(instring), self.errmsg, self)
  
  ~/anaconda3/lib/python3.7/site-packages/pyparsing.py in parseImpl(self, instring, loc, doActions)
     4443     def parseImpl(self, instring, loc, doActions=True):
     4444         if self.expr is not None:
  -&gt; 4445             return self.expr._parse(instring, loc, doActions, callPreParse=False)
     4446         else:
     4447             raise ParseException(&quot;&quot;, loc, self.errmsg, self)
  
  ~/anaconda3/lib/python3.7/site-packages/pyparsing.py in _parseCache(self, instring, loc, doActions, callPreParse)
     1836                 ParserElement.packrat_cache_stats[MISS] += 1
     1837                 try:
  -&gt; 1838                     value = self._parseNoCache(instring, loc, doActions, callPreParse)
     1839                 except ParseBaseException as pe:
     1840                     # cache a copy of the exception, without the traceback
  
  ~/anaconda3/lib/python3.7/site-packages/pyparsing.py in _parseNoCache(self, instring, loc, doActions, callPreParse)
     1675             if self.mayIndexError or preloc &gt;= len(instring):
     1676                 try:
  -&gt; 1677                     loc, tokens = self.parseImpl(instring, preloc, doActions)
     1678                 except IndexError:
     1679                     raise ParseException(instring, len(instring), self.errmsg, self)
  
  ~/anaconda3/lib/python3.7/site-packages/pyparsing.py in parseImpl(self, instring, loc, doActions)
     4033         # pass False as last arg to _parse for first element, since we already
     4034         # pre-parsed the string as part of our And pre-parsing
  -&gt; 4035         loc, resultlist = self.exprs[0]._parse(instring, loc, doActions, callPreParse=False)
     4036         errorStop = False
     4037         for e in self.exprs[1:]:
  
  ~/anaconda3/lib/python3.7/site-packages/pyparsing.py in _parseCache(self, instring, loc, doActions, callPreParse)
     1836                 ParserElement.packrat_cache_stats[MISS] += 1
     1837                 try:
  -&gt; 1838                     value = self._parseNoCache(instring, loc, doActions, callPreParse)
     1839                 except ParseBaseException as pe:
     1840                     # cache a copy of the exception, without the traceback
  
  ~/anaconda3/lib/python3.7/site-packages/pyparsing.py in _parseNoCache(self, instring, loc, doActions, callPreParse)
     1679                     raise ParseException(instring, len(instring), self.errmsg, self)
     1680             else:
  -&gt; 1681                 loc, tokens = self.parseImpl(instring, preloc, doActions)
     1682 
     1683         tokens = self.postParse(instring, loc, tokens)
  
  ~/anaconda3/lib/python3.7/site-packages/pyparsing.py in parseImpl(self, instring, loc, doActions)
     4443     def parseImpl(self, instring, loc, doActions=True):
     4444         if self.expr is not None:
  -&gt; 4445             return self.expr._parse(instring, loc, doActions, callPreParse=False)
     4446         else:
     4447             raise ParseException(&quot;&quot;, loc, self.errmsg, self)
  
  ~/anaconda3/lib/python3.7/site-packages/pyparsing.py in _parseCache(self, instring, loc, doActions, callPreParse)
     1836                 ParserElement.packrat_cache_stats[MISS] += 1
     1837                 try:
  -&gt; 1838                     value = self._parseNoCache(instring, loc, doActions, callPreParse)
     1839                 except ParseBaseException as pe:
     1840                     # cache a copy of the exception, without the traceback
  
  ~/anaconda3/lib/python3.7/site-packages/pyparsing.py in _parseNoCache(self, instring, loc, doActions, callPreParse)
     1679                     raise ParseException(instring, len(instring), self.errmsg, self)
     1680             else:
  -&gt; 1681                 loc, tokens = self.parseImpl(instring, preloc, doActions)
     1682 
     1683         tokens = self.postParse(instring, loc, tokens)
  
  ~/anaconda3/lib/python3.7/site-packages/pyparsing.py in parseImpl(self, instring, loc, doActions)
     2881         if instring[loc] == self.firstMatchChar:
     2882             return loc + 1, self.match
  -&gt; 2883         raise ParseException(instring, loc, self.errmsg, self)
     2884 
     2885 _L = Literal
  
  ParseException: Expected {SelectQuery | ConstructQuery | DescribeQuery | AskQuery}, found 's'  (at char 17), (line:2, col:17)
  </code></pre>
  <p>It looks like rdflib.plugins.sparql.parser.parseQuery(...) cannot parse it. Or maybe there is something that I missed writing code. If someone can help that would be great.</p>