File:C grammar stmt2.pdf

Summary

Description
English: Shows a simplified excerpt of the formal grammar for the C programming language (left), and a derivation of a piece of C code (right).
For more details, see File:C grammar stmt.pdf (derivation of same code fragment from more simplified grammar, completely ignoring operator priorities).
Date
Source Own work
Author Jochen Burghardt
Other versions File:Algol grammar expr.pdf * File:Algol grammar expr svg.svg * File:C grammar stmt.pdf * File:C grammar stmt svg.svg * File:C grammar stmt2.pdf * File:C grammar stmt2 svg.svg
LaTeX source code
\documentclass[12pt]{article}
\usepackage[pdftex]{color}
\usepackage{amssymb}
\usepackage[paperwidth=233mm,paperheight=112mm,textwidth=235mm,textheight=110mm]{geometry}
\setlength{\unitlength}{1mm}
\setlength{\topmargin}{-36mm}
\setlength{\oddsidemargin}{-23mm}
\setlength{\parindent}{0cm}
\pagestyle{empty}
% colors
\definecolor{cNontermNew}       {rgb}{0.00,0.00,0.75}
\definecolor{cNontermRep}       {rgb}{0.75,0.75,0.99}
\definecolor{cTermNew}          {rgb}{0.75,0.00,0.00}
\definecolor{cTermRep}          {rgb}{0.99,0.75,0.75}
\definecolor{cGrmBg}            {rgb}{0.95,0.95,0.95}
\newcommand{\NT}[1]{\textcolor{cNontermNew}{$\langle$#1$\rangle$}}
\newcommand{\nt}[1]{\textcolor{cNontermRep}{$\langle$#1$\rangle$}}
\newcommand{\TM}[1]{\textcolor{cTermNew}{\texttt{#1}}}
\newcommand{\tm}[1]{\textcolor{cTermRep}{\texttt{#1}}}

\begin{document}
\setlength{\tabcolsep}{0.8mm}
%\newcommand{\1}{|}     \newcommand{\2}[1]{}
\newcommand{\1}{}       \newcommand{\2}[1]{\cline{#1}}

\begin{picture}(0,0)%
\textcolor{cGrmBg}{\put(0,0){\makebox(0,0)[bl]{\rule{69.6mm}{102.1mm}}}}%
\end{picture}%
\begin{tabular}[b]{|rcl|}
\hline
\NT{Stmt} & $\rightarrow$ & \NT{Id} \TM{=} \NT{RExpr} \TM{;}    \\
\NT{Stmt} & $\rightarrow$ & \TM{\{} \NT{StmtList} \TM{\}}       \\
\NT{Stmt} & $\rightarrow$ & \TM{if} \TM{(} \NT{RExpr} \TM{)} \NT{Stmt}  \\
%\NT{Stmt} & $\rightarrow$ & \TM{while} \TM{(} \NT{Expr} \TM{)} \NT{Stmt}       \\
%\NT{Stmt} & $\rightarrow$ & \TM{switch}\TM{(}\NT{Expr} \TM{)} \NT{Stmt}        \\
%\NT{Stmt} & $\rightarrow$ & \TM{case} \NT{Num} : \NT{Stmt}     \\
%\NT{Stmt} & $\rightarrow$ & \TM{break} \TM{;}  \\
%\NT{Stmt} & $\rightarrow$ & \TM{continue} \TM{;}       \\
%\NT{Stmt} & $\rightarrow$ & \NT{Id} \TM{:} \NT{Stmt}   \\
%\NT{Stmt} & $\rightarrow$ & \TM{goto} \NT{Id} \TM{;}   \\
%\hline
\NT{StmtList} & $\rightarrow$ & \NT{Stmt}       \\
\NT{StmtList} & $\rightarrow$ & \NT{StmtList} \NT{Stmt} \\
%\hline
\NT{RExpr} & $\rightarrow$ & \NT{RExpr} \TM{>~} \NT{AExpr}      \\
\NT{RExpr} & $\rightarrow$ & \NT{RExpr} \TM{<~} \NT{AExpr}      \\
\NT{RExpr} & $\rightarrow$ & \NT{RExpr} \TM{>=} \NT{AExpr}      \\
\NT{RExpr} & $\rightarrow$ & \NT{RExpr} \TM{<=} \NT{AExpr}      \\
\NT{RExpr} & $\rightarrow$ & \NT{AExpr} \\
\NT{AExpr} & $\rightarrow$ & \NT{AExpr} \TM{+} \NT{PExpr}       \\
\NT{AExpr} & $\rightarrow$ & \NT{AExpr} \TM{-} \NT{PExpr}       \\
\NT{AExpr} & $\rightarrow$ & \NT{PExpr} \\
%\NT{PExpr} & $\rightarrow$ & \TM{(} \NT{RExpr} \TM{)}  \\
\NT{PExpr} & $\rightarrow$ & \NT{Id}    \\
\NT{PExpr} & $\rightarrow$ & \NT{Num}   \\
%\hline
\NT{Id} & $\rightarrow$ & \TM{x}        \\
\NT{Id} & $\rightarrow$ & \TM{y}        \\
%\hline
\NT{Num} & $\rightarrow$ & \TM{0}       \\
\NT{Num} & $\rightarrow$ & \TM{1}       \\
\NT{Num} & $\rightarrow$ & \TM{9}       \\
\hline
\end{tabular}
$\;\;\;\;\;\;\;\;\;$
\begin{tabular}[b]{\1c*{17}{c\1}}
%\hline
\multicolumn{18}{\1c\1}{\NT{Stmt}}      \\
\2{1-18}
\TM{if} & \TM{(} & \multicolumn{3}{c\1}{\NT{RExpr}} & \TM{)} & \multicolumn{12}{c\1}{\NT{Stmt}} \\
\2{3-5}
\tm{if} & \tm{(} & \NT{RExpr} & \TM{>} & \NT{AExpr} & \tm{)} & \multicolumn{12}{c\1}{\nt{Stmt}} \\
\2{3-3}
\tm{if} & \tm{(} & \NT{AExpr} & \tm{>} & \NT{AExpr} & \tm{)} & \multicolumn{12}{c\1}{\nt{Stmt}} \\
\2{3-3}
\2{5-5}
\tm{if} & \tm{(} & \NT{PExpr} & \tm{>} & \NT{PExpr} & \tm{)} & \multicolumn{12}{c\1}{\nt{Stmt}} \\
\2{3-3}
\2{5-5}
\tm{if} & \tm{(} & \NT{Id}    & \tm{>} & \NT{Num}   & \tm{)} & \multicolumn{12}{c\1}{\nt{Stmt}} \\
\2{3-3}
\2{5-5}
\tm{if} & \tm{(} & \TM{x}     & \tm{>} & \TM{9}     & \tm{)} & \multicolumn{12}{c\1}{\NT{Stmt}} \\
\2{7-18}
\tm{if} & \tm{(} & \tm{x}     & \tm{>} & \tm{9}     & \tm{)} & \TM{\{} & \multicolumn{10}{c\1}{\NT{StmtList}}                                                                  & \TM{\}}        \\
\2{8-17}
\tm{if} & \tm{(} & \tm{x}     & \tm{>} & \tm{9}     & \tm{)} & \tm{\{} & \multicolumn{4}{c\1}{\NT{StmtList}}    & \multicolumn{6}{c\1}{\NT{Stmt}}                              & \tm{\}}        \\
\2{8-11}
\tm{if} & \tm{(} & \tm{x}     & \tm{>} & \tm{9}     & \tm{)} & \tm{\{} & \multicolumn{4}{c\1}{\NT{Stmt}}        & \multicolumn{6}{c\1}{\nt{Stmt}}                              & \tm{\}}        \\
\2{8-11}
\tm{if} & \tm{(} & \tm{x}     & \tm{>} & \tm{9}     & \tm{)} & \tm{\{} & \NT{Id} & \TM{=} & \NT{RExpr} & \TM{;} & \multicolumn{6}{c\1}{\nt{Stmt}}                              & \tm{\}}        \\
\2{8-8}
\2{10-10}
\tm{if} & \tm{(} & \tm{x}     & \tm{>} & \tm{9}     & \tm{)} & \tm{\{} & \TM{x}  & \tm{=} & \NT{AExpr} & \tm{;} & \multicolumn{6}{c\1}{\nt{Stmt}}                              & \tm{\}}        \\
\2{10-10}
\tm{if} & \tm{(} & \tm{x}     & \tm{>} & \tm{9}     & \tm{)} & \tm{\{} & \tm{x}  & \tm{=} & \NT{PExpr} & \tm{;} & \multicolumn{6}{c\1}{\nt{Stmt}}                              & \tm{\}}        \\
\2{10-10}
\tm{if} & \tm{(} & \tm{x}     & \tm{>} & \tm{9}     & \tm{)} & \tm{\{} & \tm{x}  & \tm{=} & \NT{Num}   & \tm{;} & \multicolumn{6}{c\1}{\nt{Stmt}}                              & \tm{\}}        \\
\2{10-10}
\tm{if} & \tm{(} & \tm{x}     & \tm{>} & \tm{9}     & \tm{)} & \tm{\{} & \tm{x}  & \tm{=} & \TM{0}     & \tm{;} & \multicolumn{6}{c\1}{\NT{Stmt}}                              & \tm{\}}        \\
\2{12-17}
\tm{if} & \tm{(} & \tm{x}     & \tm{>} & \tm{9}     & \tm{)} & \tm{\{} & \tm{x}  & \tm{=} & \tm{0}     & \tm{;} & \NT{Id} & \TM{=} & \multicolumn{3}{c\1}{\NT{RExpr}} & \TM{;} & \tm{\}}        \\
\2{12-12}
\2{14-16}
\tm{if} & \tm{(} & \tm{x}     & \tm{>} & \tm{9}     & \tm{)} & \tm{\{} & \tm{x}  & \tm{=} & \tm{0}     & \tm{;} & \TM{y}  & \tm{=} & \multicolumn{3}{c\1}{\NT{AExpr}} & \tm{;} & \tm{\}}        \\
\2{14-16}
\tm{if} & \tm{(} & \tm{x}     & \tm{>} & \tm{9}     & \tm{)} & \tm{\{} & \tm{x}  & \tm{=} & \tm{0}     & \tm{;} & \tm{y}  & \tm{=} & \NT{AExpr} & \TM{+} & \NT{PExpr} & \tm{;} & \tm{\}}        \\
\2{14-14}
\tm{if} & \tm{(} & \tm{x}     & \tm{>} & \tm{9}     & \tm{)} & \tm{\{} & \tm{x}  & \tm{=} & \tm{0}     & \tm{;} & \tm{y}  & \tm{=} & \NT{PExpr} & \tm{+} & \NT{PExpr} & \tm{;} & \tm{\}}        \\
\2{14-14}
\2{16-16}
\tm{if} & \tm{(} & \tm{x}     & \tm{>} & \tm{9}     & \tm{)} & \tm{\{} & \tm{x}  & \tm{=} & \tm{0}     & \tm{;} & \tm{y}  & \tm{=} & \NT{Id}    & \tm{+} & \NT{Num}   & \tm{;} & \tm{\}}        \\
\2{14-14}
\2{16-16}
\TM{if} & \TM{(} & \TM{x}     & \TM{>} & \TM{9}     & \TM{)} & \TM{\{} & \TM{x}  & \TM{=} & \TM{0}     & \TM{;} & \TM{y}  & \TM{=} & \TM{y}     & \TM{+} & \TM{1}     & \TM{;} & \TM{\}}        \\
\end{tabular}
\end{document}

Licensing

I, the copyright holder of this work, hereby publish it under the following license:
w:en:Creative Commons
attribution share alike
This file is licensed under the Creative Commons Attribution-Share Alike 4.0 International license.
You are free:
  • to share – to copy, distribute and transmit the work
  • to remix – to adapt the work
Under the following conditions:
  • attribution – You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.
  • share alike – If you remix, transform, or build upon the material, you must distribute your contributions under the same or compatible license as the original.
Category:CC-BY-SA-4.0#C%20grammar%20stmt2.pdf
Category:Self-published work Category:Syntax trees Category:C (programming language) Category:Files by User:Jochen Burghardt Category:Images with LaTeX source code
Category:CC-BY-SA-4.0 Category:C (programming language) Category:Files by User:Jochen Burghardt Category:Images with LaTeX source code Category:Self-published work Category:Syntax trees