Example: Python if-then-else syntax diagram

Published 2009-02-17 | Author: Ludger Humbert

A syntax diagram (or railroad diagram) for a if-then-elif-else expression in Python.

Author:Dr. Ludger Humbert
Source:https://haspe.homeip.net/projekte/ddi/browser/tex/pgf2

Download as: [PDF] [TEX]  •  [Open in Overleaf]

Python if-then-else syntax diagram

Do you have a question regarding this example, TikZ or LaTeX in general? Just ask in the LaTeX Forum.
Oder frag auf Deutsch auf TeXwelt.de. En français: TeXnique.fr.

% Author: Dr. Ludger Humbert
% Source: https://haspe.homeip.net/projekte/ddi/browser/tex/pgf2                                                                             
% https://haspe.homeip.net/projekte/ddi/browser/tex/pgf2/Python_if-then-elif-else.tex $:
%

\documentclass{article}
\usepackage[ngerman]{babel}

\usepackage{tikz}
\usepackage{xxcolor}


\usetikzlibrary{chains,matrix,scopes,decorations.shapes,arrows,shapes}


% fuer Railroad-Diagramme
\tikzset{
  nonterminal/.style={
    % The shape:
    rectangle,
    % The size:
    minimum size=6mm,
    % The border:
    very thick,
    draw=red!50!black!50,         % 50% red and 50% black,
                                  % and that mixed with 50% white
    % The filling:
    top color=white,              % a shading that is white at the top...
    bottom color=red!50!black!20, % and something else at the bottom
    % Font
    font=\itshape
  },
  terminal/.style={
    % The shape:
    rounded rectangle,
    minimum size=6mm,
    % The rest
    very thick,draw=black!50,
    top color=white,bottom color=black!20,
    font=\ttfamily},
  skip loop/.style={to path={-- ++(0,#1) -| (\tikztotarget)}}
}
{
  \tikzset{terminal/.append style={text height=1.5ex,text depth=.25ex}}
  \tikzset{nonterminal/.append style={text height=1.5ex,text depth=.25ex}}
}


\renewcommand\familydefault{\sfdefault}

\listfiles

\begin{document}


%% if_stmt ::= "if" expression ":" suite
%%                ( "elif" expression ":" suite )*
%%                ["else" ":" suite]
%%
\hspace{-35ex}
\begin{tikzpicture}[point/.style={coordinate},>=stealth',thick,draw=black!50,
                    tip/.style={->,shorten >=0.007pt},every join/.style={rounded corners},
                    hv path/.style={to path={-| (\tikztotarget)}},
                    vh path/.style={to path={|- (\tikztotarget)}},
                    text height=1.5ex,text depth=.25ex]     % um die Hoehe des Punktes festzuzurren
  \matrix[ampersand replacement=\&,column sep=4mm] {
    \node (p1)  [point]  {}; \&    \node (ifs)    [terminal]     {if};             \&
    \node (p2)  [point]  {}; \&    \node (expr1)  [nonterminal]  {expr};           \&
    \node (p3)  [point]  {}; \&    \node (colon1) [terminal]     {:};              \&
    \node (p4)  [point]  {}; \&    \node (suite1) [nonterminal]  {suite};          \&
    \node (p5)  [point]  {}; \&    \node (p6)     [point]  {};                     \&
    \node (p6a) [point]  {}; \&    \node (p6b)    [point]  {};                     \&
    \node (p7)  [point]  {}; \&    \node (elif)   [terminal]     {elif};           \&
    \node (p8)  [point]  {}; \&    \node (expr2)  [nonterminal]  {expr};           \&
    \node (p9)  [point]  {}; \&    \node (colon2) [terminal]     {:};              \&
    \node (p10) [point]  {}; \&    \node (suite2) [nonterminal]  {suite};          \&
    \node (p11) [point]  {}; \&    \node (p12)    [point]  {};                     \&
    \node (p12a)[point]  {}; \&    \node (p12b)   [point]  {};                     \&
    \node (p13) [point]  {}; \&    \node (else)   [terminal]     {else};           \&
    \node (p14) [point]  {}; \&    \node (colon3) [terminal]     {:};              \&
    \node (p15) [point]  {}; \&    \node (suite3) [nonterminal]  {suite};          \&
    \node (p16) [point]  {}; \&    \node (p17)    [point]       {};\\
  };

  { [start chain]
    \chainin (p1);
    \chainin (ifs)    [join=by tip];
    \chainin (p2)     [join];
    \chainin (expr1)  [join=by tip];
    \chainin (p3)     [join];
    \chainin (colon1) [join=by tip];
    \chainin (p4)     [join];
    \chainin (suite1) [join=by tip];
    \chainin (p7)     [join];
    \chainin (elif)   [join=by tip];
    \chainin (p8)     [join];
    \chainin (expr2)  [join=by tip];
    \chainin (p9)     [join];
    \chainin (colon2) [join=by tip];
    \chainin (p10)    [join];
    \chainin (suite2) [join=by tip];
    \chainin (p12a)   [join,join=with p6 by {skip loop=-11mm,tip}];
    \chainin (p13)    [join];
    \chainin (else)   [join=by tip];
    \chainin (p14)    [join];
    \chainin (colon3) [join=by tip];
    \chainin (p15)    [join];
    \chainin (suite3) [join=by tip];
    \chainin (p16)    [join];
    \chainin (p17)    [join=by tip];
    \chainin (p16)    [join,join=with p12b by {skip loop=-11mm,tip}];
    \chainin (p6b)    [join=with p11 by {skip loop=11mm,tip}];
  }
\end{tikzpicture}



\end{document}

Comments

  • #1 luoyi, February 21, 2009 at 3:55 p.m.

    I can't get the correct pdf from the src. it looks like there are some page setting doesn't included in the src file. my result is: http://www.luoyilinux.cn/my-pysyntax.pdf . it's not a complete figure.

  • #2 Kjell Magne Fauske, February 22, 2009 at 6:05 p.m.

    @luoyi The problem is that the diagram is too wide to fit on a normal page. When preparing the examples for the gallery I use the preview.sty package for extracting only the graphics. The code that is used typically looks like this:

    \usepackage{tikz}
    \usepackage[active,tightpage]{preview}
    \PreviewEnvironment{tikzpicture}
    \setlength\PreviewBorder{5pt}%
    

    The preview package will then extract every tikzpicture in the document as a separate page in the resulting PDF. It also removes white space around the figure. The preview code is then removed from the tex document before it is uploaded to the gallery.

    In this case the preview code should have been kept in the document. I'll consider doing this for future examples.

  • #3 Andrew, February 22, 2009 at 11:25 p.m.

    Yep, it would be very nice to see this recipe, maybe in the "About the gallery" section (to keep examples clean). Thanks again!

  • #4 luoyi, February 23, 2009 at 4:33 a.m.

    currently, preview.sty just support the pdftex and dvips, can't work well with xelatex. it's a pity.

  • #5 Radek Pysny, March 23, 2009 at 2:21 p.m.

    Hi,

    I found a problem with compiling this example code.

    On line number 103 \chainin (ifs) [join=by tip];

    The error message is ! Package PGF Math Error: Unknown function `of'.

    Could somebody help me, please?

    I'm using PGF 2.00 with LaTeX or pdfLaTeX.

    Thanks.

    Radek

Adding comments is currently not enabled.

Features
Tags
Scientific and technical areas

Cookbook

LaTeX Beginners Guide

Limited discount 50%
coupon code tDRet6Y

Creative Commons License