1

I am trying to learn Latex, I have to solve an assignment that involves BNF grammar rules.

Here is my code:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{syntax}

\title{Hello world }
\author{Buk Lau }
\date{October, 28 2016}

\usepackage{natbib}
\usepackage{graphicx}

\begin{document}

\maketitle

\section{Second Problem}
We have the following BNF grammar rule:

\begin{grammar}
<pop> ::= [<bop> , <pop>] | <bop>\\
<bop> ::= <boop> | (<pop> )\\
<boop> ::= x|y|z
\end{grammar}

\section{Conclusion}
``I always thought something was fundamentally wrong with the universe'' \citep{adams1995hitchhiker}

\bibliographystyle{plain}
\bibliography{references}
\end{document}

Here is my output:

enter image description here

No errors are given on compilation. However, there is an extra tab added after the break (on the next two statements):

enter image description here

Why is it happening? What can I do to fix that? Furthermore, how can centralize the three statements (Not really part of this question, but I'd appreciate if you can tell me how)?

2 Answers 2

2

The package manual texdoc syntax states that separate production rules should be separated by blank lines and that \\ is for continuing long rules over a line (hence the indentation)

enter image description here

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{syntax}

\title{Hello world }
\author{Buk Lau }
\date{October, 28 2016}

\usepackage{natbib}
\usepackage{graphicx}

\begin{document}

\maketitle

\section{Second Problem}
We have the following BNF grammar rule:

\begin{grammar}
<pop> ::= [<bop> , <pop>] | <bop>

<bop> ::= <boop> | (<pop> )

<boop> ::= x|y|z
\end{grammar}

\section{Conclusion}
``I always thought something was fundamentally wrong with the universe'' \citep{adams1995hitchhiker}

\bibliographystyle{plain}
\bibliography{references}
\end{document}
2
  • This will add two line breaks. I don't want this to happen. I only want it to return line. Commented Oct 19, 2016 at 21:10
  • @BukLau As noted further down the page that describes the enviornment use \setlength\grammarparsep{0pt} to customise the length between rules Commented Oct 19, 2016 at 21:17
1

Instead of using syntax, you can define something similar via listings.

There is some literate changes to improve the layout, including using \vcentcolon for a better preview of ::=:

enter image description here

\documentclass{article}

\usepackage{listings,mathtools}

\lstnewenvironment{grammar}[1][]
  {\lstset{
    %basicstyle = \ttfamily,
    numbers = none,
    fontadjust = true,
    flexiblecolumns = true,
    keepspaces = false,
    literate = {<}{$\langle$}1
               {>}{$\rangle$}1
               {::=}{$\vcentcolon\vcentcolon=$}3
   }}
  {}

\begin{document}

\noindent
We have the following BNF grammar rule:
\begin{grammar}
<pop> ::= [<bop> , <pop>] | <bop>
<bop> ::= <boop> | (<pop>)
<boop> ::= x|y|z
\end{grammar}

\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .