Math & Sciencecontext free grammar

Information and discussion about LaTeX's math and science related features (e.g. formulas, graphs).
Post Reply
krislodz
Posts: 42
Joined: Sun Nov 08, 2009 1:13 pm

context free grammar

Post by krislodz »

Hello,
I need to do type setting of simple context-free grammar. i could not find any useful packages dedicated for that. Are there any such packages ?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Re: context free grammar

Post by gmedina »

Hi,

can you please attach (or provide a link to) an image showing the kind of things that you want to do?
1,1,2,3,5,8,13,21,34,55,89,144,233,...
krislodz
Posts: 42
Joined: Sun Nov 08, 2009 1:13 pm

context free grammar

Post by krislodz »

Grammar printout as in following image
img23.gif
img23.gif (1020 Bytes) Viewed 9602 times
Designed for simple subset of C language.
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

context free grammar

Post by gmedina »

You can use something like the following:

Code: Select all

\documentclass{article}
\usepackage{amsmath}
\usepackage{array}
\usepackage{txfonts}

\begin{document}

{\setlength\tabcolsep{4pt}
\begin{tabular}{>{$}l<{$}>{$}r<{$}>{$}l<{$}}
  R &\Coloneqq & R\\
  &| &R_\ast\\
  &| &(R)\\
  &| &a\\
  &| &b
\end{tabular}}

\end{document}
1,1,2,3,5,8,13,21,34,55,89,144,233,...
krislodz
Posts: 42
Joined: Sun Nov 08, 2009 1:13 pm

context free grammar

Post by krislodz »

Thanks gmedina, I managed to typeset the grammar that i use. It does not look as good as it could though, the nonterminals like Ss and Fs are separated by unwanted spaces. Any idea how to improve it ?

Code: Select all

\documentclass{article}
\usepackage{amsmath}
\usepackage{array}
\usepackage{txfonts}

\begin{document}

{\setlength\tabcolsep{4pt}
\begin{tabular}{>{$}l<{$}>{$}r<{$}>{$}l<{$}}
  e &\Coloneqq &n\\
  &| &x\\
  &| &e_1 \; op_b \; e_2\\
  &| &op_m \; e\\
  &| &(e)\\
  &| &f(es)\\
  es &\Coloneqq &e\\
  &| &e, \; es\\
  S &\Coloneqq &x := e;\\
  &| &skip;\\
  &| &read \; x;\\
  &| &write \; e;\\
  &| &return \; e;\\
  &| &if(e) \; S_1 \; else \; S_2\\
  &| &while(e) \; S\\
  &| &\{Ds \; Ss\}\\
  &| &\{Ss\}\\
  Ss &\Coloneqq &S\\
  &| &S \; Ss\\
  Ds &\Coloneqq &T \; x;\\
  &| &T \; x; Ds\\
  Fs &\Coloneqq &T \; f(Ds)\{Ss\}\\
  &| &T \; f(Ds)\{Ss\} \; Fs\\
  T &\Coloneqq &int\\
  &| &float\\
  P &\Coloneqq &program \; Ds \; Fs \; Ss \; end
\end{tabular}}

$op_b \in \{\&,|,<,>,+,-,*,/\}, op_m \in \{!,-\}$

\end{document}
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

context free grammar

Post by gmedina »

Hi,

certain combinations of characters will produce that unwanted space. In the code below I defined the new command \nonter that corrects the problem:

Code: Select all

\documentclass{article}
\usepackage{amsmath}
\usepackage{array}
\usepackage{txfonts}

\newcommand\nonter[1]{\ensuremath{#1\negthinspace s}}

\begin{document}

{\setlength\tabcolsep{4pt}
\begin{tabular}{>{$}l<{$}>{$}r<{$}>{$}l<{$}}
  &| &\{Ds \; Ss\}\\% bad spacing
  &| &\{Ds \; \nonter{S} \}\\% corrected spacing
  &| &\{Ds \; Fs\}\\% bad spacing
  &| &\{Ds \; \nonter{F} \}% corrected spacing
\end{tabular}}

\end{document}
Of course, instead of \negthinspace you can use \negthickspace or even \hspace*{<value>}, where <value> can be any valid length (in your case, negative).
1,1,2,3,5,8,13,21,34,55,89,144,233,...
krislodz
Posts: 42
Joined: Sun Nov 08, 2009 1:13 pm

Re: context free grammar

Post by krislodz »

Thanks a lot gmedina for help again. Kind of strange that there is no package for printing grammars built-in.
Post Reply