# Using angular brackets in a grammar environment

How can I use angular <> brackets in a grammar environment?

I want to describe that my programming language supports the following constructs:

List<int> myList = new List<int>();
List<List<int>> myList2 = new List<List<int>>();
Set<int> mySet = new Set<int>();
Bag<int> myBag = new MyBag<int>();
Array<int> myArray = new Array<int>();


For that reason, I am using the grammar environment, to describe the possible constructs:

\begin{grammar}
<collection_parameter_decl> ::= <ident>
\alt 'List' '<' <collection_parameter_decl> '>'
\alt 'Bag' '<' <collection_parameter_decl> '>'
\alt 'Set' '<' <collection_parameter_decl> '>'
\alt 'Array' '<' <collection_parameter_decl> '>'
\end{grammar}


Unfortunately, angular brackets are used in my programming language and at the same time the grammar environment uses them to figure out what a token is. Because of this, the whole thing is formatted in an ugly way:

• I'm not sure what it is supposed to look like, but \char60 instead of < might be a help. – Steven B. Segletes Feb 7 '18 at 15:32
• Good hint - works... maybe you make an answer out of it ;) – Vertexwahn Feb 7 '18 at 15:38
• shouldn't the shape of the angle brackets (at least the unquoted ones) be the same in both line 1 and the additional lines of the grammar? – barbara beeton Feb 7 '18 at 17:55
• @barbarabeeton Yes. That is exactly the problem. – Vertexwahn Feb 8 '18 at 9:05

I really don't have a clue what the answer is supposed to look like, but I did note that \char60 can be used in lieu of < to get a non-interpreted version of the character. I codify it here in a macro named \?.

\documentclass[12pt]{article}
\usepackage[T1]{fontenc}
\usepackage{syntax}
\newcommand\?{\char60}
\begin{document}
\begin{grammar}
<collection_parameter_decl> ::= \?ident>
\alt 'List' '\?' <collection_parameter_decl> '>'
\alt 'Bag' '\?' <collection_parameter_decl> '>'
\alt 'Set' '\?' <collection_parameter_decl> '>'
\alt 'Array' '\?' <collection_parameter_decl> '>'
\end{grammar}
\end{document}


• in the original question, "collection_parameter_decl" is italic on all lines. also, see my comment to the original question regarding the shape of the angle brackets. (that's awaiting a response from the op.) – barbara beeton Feb 7 '18 at 17:57
• i have taken the liberty of correcting the lines after the first so that the grammar description is in italic as in the example shown in the original question. the use of \char60 is correct; the original formulation of the answer just used it in too many places. – barbara beeton Feb 8 '18 at 17:41

Assuming your quotes are always balanced and don't contain metavariables in angle brackets, you can tweak grammar.

\documentclass[12pt]{article}
\usepackage[T1]{fontenc}
\usepackage{syntax}
\usepackage{etoolbox}

\AtBeginEnvironment{grammar}{%
\begingroup\lccode~='\lowercase{\endgroup\let~}\grammarquote
\catcode'=\active
}
\let\normallt=<
\begingroup
\catcode'=\active
\catcode<=\active
\gdef\grammarquote#1'{\begingroup\let<\normallt\rq#1\rq\endgroup}
\endgroup

\begin{document}

\begin{grammar}
<collection_parameter_decl> ::= <ident>
\alt 'List' '<' <collection_parameter_decl> '>'
\alt 'Bag' '<' <collection_parameter_decl> '>'
\alt 'Set' '<' <collection_parameter_decl> '>'
\alt 'Array' '<' <collection_parameter_decl> '>'
\end{grammar}

\end{document}


The idea is to make ' into an active character that absorbs everything up to the next ' and typesets it after defining < (which is active inside grammar) to produce a normal <` symbol.