Math & Science ⇒ context free grammar
context free grammar
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 ?
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 ?
NEW: TikZ book now 40% off at Amazon.com for a short time.

Re: context free grammar
Hi,
can you please attach (or provide a link to) an image showing the kind of things that you want to do?
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,...
context free grammar
Grammar printout as in following image
Designed for simple subset of C language.context free grammar
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,...
context free grammar
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}
context free grammar
Hi,
certain combinations of characters will produce that unwanted space. In the code below I defined the new command \nonter that corrects the problem:
Of course, instead of \negthinspace you can use \negthickspace or even \hspace*{<value>}, where <value> can be any valid length (in your case, negative).
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}
1,1,2,3,5,8,13,21,34,55,89,144,233,...
Re: context free grammar
Thanks a lot gmedina for help again. Kind of strange that there is no package for printing grammars built-in.