I'm new to StackExchange, and don't know well the forum ; but I think this page should be updated.
I first followed the SQL
solution, then the \lstdefinelanguage
: both of these solutions fail with comments.
However, I found that there is a SPARQL definition in /usr/share/texlive/texmf-dist/tex/latex/listings/lstlang3.sty
(install on Ubuntu 18.04) :
.../usr/share/texlive/texmf-dist/tex/latex/listings$ grep -i sparql *
lstlang3.sty:%% SPARQL definition (c) 2006 Christoph Kiefer
lstlang3.sty:\lst@definelanguage{SPARQL}%
As you can see, it was written in 2006 (but maybe not in listings package in 2011 or 2013, when the answers on this page were posted?).
Here's a sample code to compare the solutions :
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{listings}
\usepackage{color}
\lstset{
morecomment=[l]{\#},
frame = single,
basicstyle = \ttfamily\footnotesize,
commentstyle=\color{blue},
}
\lstdefinelanguage{sparqlB}{
morecomment=[l]{\#},
language=SQL,
morekeywords={SELECT},
commentstyle=\itshape\color{blue},
sensitive=true,
}
\begin{document}
-- SPARQL in lstlang3.sty :
\begin{lstlisting}[language=sparql]
# comment with 'single quotes' = OK
SELECT * WHERE { ?s ?p ?o }
\end{lstlisting}
-- SQL solution :
\begin{lstlisting}[language=SQL]
# comment with 'single quotes' = NOT RECOGNIZED
SELECT * WHERE { ?s ?p ?o }
\end{lstlisting}
-- \\lstdefinelanguage solution
\begin{lstlisting}[language=sparqlB]
# comment with 'single quotes' = NOT RECOGNIZED
SELECT * WHERE { ?s ?p ?o }
\end{lstlisting}
\end{document}
EDIT :
Actually the \lstdefinelanguage
works for comments if I change the order of options
\lstdefinelanguage{
language=SQL, % <-- has to be before morecomment
morecomment=[l]{\#},
[...]
or remove language=SQL
But the definition in .sty file is more complete, of course.