I use XeLaTeX and have TeXLive 2015 installed.
I want to use both the listings package for printing code and the syntax package for printing grammars. For code, I also want to expand certain symbols als literate mathematical strings, for example I want N0 to yield ‘N₀’, consuming two characters. To this end, one may use the literate facility of the listings package, see its documentation.
Now, here’s my problem: Running xelatex on
\documentclass{article}
\usepackage{
listings,
% syntax,
}
\begin{document}
\lstset{
literate={N0}{{$N$}{$_0$}} 2
}
\begin{lstlisting}[mathescape]
N0 $N_0$
\end{lstlisting}
\end{document}
yields a result as expected, but uncommenting the line % syntax, to load the syntax package, will give a semantic nesting error on compiling:
! TeX capacity exceeded, sorry [semantic nest size=500].
<recently read> {
l.16 ^^IN0
$N_0$
No pages of output.
I found out that the culprit is the mathematical underscore in literate={N0}{{$N$}{$_0$}} 2, and only in the literate part. Replacing this line by literate={N0}{{$N$}{$0$}} 2 (i.e. removing the underscore) will let xelatex compile again. Note that there still is a mathematical underscore in $N_0$ inside of the listing environment.
What is going on? Can I prevent this from happening?

syntaxpackage. I’m kinda curious myself, though I’m not sure I’ll understand it. But as said, I’m perfectly content either way. – k.stm Jun 04 '16 at 10:40syntaxpackage makes_into an active character at begin document, but for its purposeslistingsconsiders it with its standard category code when evaluating options. Chaos ensues: groups are opened in series and the semantic nest size is exhausted, but it's essentially an infinite loop. – egreg Jun 04 '16 at 10:54