Another way to do this kind of thing is to use a \lccode
trick:
\begingroup\lccode`!=`\\\lowercase{\endgroup\def\@backslashchar{!}}
This avoids any need to change catcodes thanks to the special properties of \lowercase
. Compared to the \expandafter\@gobble\string
method, it's a bit more flexible as you can choose the catcode of the resulting character inside your macro. Compared to a change of catcodes, you don't have to use \global
because of how \lowercase
interacts with \endgroup
.
Here's the detail of the code. The \begingroup
is here to keep the lccode changes local. The
\lccode`!=`\\
means that when lowercasing !
you will get the backslash \
(the choice of !
is arbitrary, you just need a normal
character which will not appear elsewhere inside the \lowercase
). The important thing is that the lowercase version of !
wille be a normal character since \lowercase
doesn't change catcodes.
The code \lowercase{\endgroup\def\@backslashchar{!}}
is thus equivalent to \endgroup\def\@backslashchar{\}
but with \
not being special (so \
followed by }
won't be interpreted as \}
but as two separate entities).
At the end, the result is that you have defined \@backslashchar
to be a \
with normal catcode. It can thus be used inside a \write
command without causing the same problems as \
does.
This lccode trick works with all other special characters. For example if you want a space character (of course, for spaces, the macro \space
works fine):
\begingroup\lccode`!=`\ \lowercase{\endgroup\def\@spacechar{!}}
If ever you break the code on two lines, just be careful with the end of lines and put a %
after the \
:
\begingroup\lccode`!=`\ %
\lowercase{\endgroup\def\@spacechar{!}}
Updated Version:
Here is an updated version which addresses your requirements:
- Double slash are comments are in gray and italics
- Strings in single quote are green
- Literals (written in all caps) are dark blue (but need to be specified)
options{}
is in black until a trailing }
is encountered.
- Everything else is magenta
- Line breaking is enabled (see lines 5-7)

Code:
\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}
\lstset{% This applies to ALL lstlisting
backgroundcolor=\color{yellow!10},%
numbers=left, numberstyle=\tiny, stepnumber=2, numbersep=5pt,%
}%
% Applies only when you use it
\lstdefinestyle{MyLang}{
basicstyle=\small\ttfamily\color{magenta},%
breaklines=true,% allow line breaks
moredelim=[s][\color{green!50!black}\ttfamily]{'}{'},% single quotes in green
moredelim=*[s][\color{black}\ttfamily]{options}{\}},% options in black (until trailing })
commentstyle={\color{gray}\itshape},% gray italics for comments
morecomment=[l]{//},% define // comment
emph={%
STRING% literal strings listed here
},emphstyle={\color{blue}\ttfamily},% and formatted in blue
alsoletter={:,|,;},%
morekeywords={:,|,;},% define the special characters
keywordstyle={\color{black}},% and format them in black
}
\begin{document}
\begin{lstlisting}[style=MyLang]
options {
language=Java;
//backtrack=true;
// please comment it in for correct lexer generation!
// the definitions of many operators require backtracking!
}
// program blocks
program
: (declaration)+
;
declaration
: metaDeclaration
| actionDeclaration
| globalDeclaration
| eventDeclaration
;
metaDeclaration
: 'meta' metaParam STRING? ';'
;
metaParam
: 'version'
| 'name'
| 'icon'
| 'color'
| 'private'
;
globalDeclaration
: 'var' varIdentifier ':' typeIdentifier
codeblock
;\end{lstlisting}
\end{document}
Initial Version
Adapting the solution from Extend a language with additional keywords? should get you started:

\documentclass{article}
\usepackage{listings}
\usepackage{xcolor}
\lstset{%
backgroundcolor=\color{yellow!20},%
basicstyle=\small\ttfamily\color{blue},%
numbers=left, numberstyle=\tiny, stepnumber=2, numbersep=5pt,%
}%
% Add your keywords here, and have this in a separate file
% and include it in your preamble
\lstset{emph={%
color, icon, meta, name, private, var%
},emphstyle={\color{green}}%
}%
\begin{document}
\begin{lstlisting}
metaParam
: 'version'
| 'name'
| 'icon'
| 'color'
| 'private'
;
globalDeclaration
: 'var' varIdentifier ':' typeIdentifier
codeblock
;
\end{lstlisting}
\end{document}
Best Answer
I have no problem with
and running again
bibtex
andpdflatex