File:C grammar stmt.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, " if ( x > 9 ) { x = 0 ; y = y + 1 ; } ") from the nonterminal symbol . In the derivation, each line is obtained from the previous one by application of a grammar rule. For the original C grammar, see Appendix A of the book: Brian W. Kernighan and Dennis M. Ritchie (Apr 1988) The C Programming Language, Prentice Hall Software Series (2nd ed.), Englewood Cliffs/NJ: Prentice Hall ISBN: 0131103628.
See File:C grammar stmt2.pdf for a less simplified grammar excerpt, accounting also for 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 |
---|
\documentclass[12pt]{article}
\setlength{\unitlength}{1mm}
\usepackage[pdftex]{color}
\usepackage{amssymb}
\usepackage[paperwidth=240mm,paperheight=125mm]{geometry}
\setlength{\topmargin}{-36mm}
\setlength{\textwidth}{240mm}
\setlength{\textheight}{125mm}
\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}{1mm}
%\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{68.5mm}{76.7mm}}}}%
\end{picture}%
\begin{tabular}[b]{|rcl|}
\hline
\NT{Stmt} & $\rightarrow$ & \NT{Id} \TM{=} \NT{Expr} \TM{;} \\
\NT{Stmt} & $\rightarrow$ & \TM{\{} \NT{StmtList} \TM{\}} \\
\NT{Stmt} & $\rightarrow$ & \TM{if} \TM{(} \NT{Expr} \TM{)} \NT{Stmt} \\
%\hline
\NT{StmtList} & $\rightarrow$ & \NT{Stmt} \\
\NT{StmtList} & $\rightarrow$ & \NT{StmtList} \NT{Stmt} \\
%\hline
\NT{Expr} & $\rightarrow$ & \NT{Id} \\
\NT{Expr} & $\rightarrow$ & \NT{Num} \\
\NT{Expr} & $\rightarrow$ & \NT{Expr} \NT{Optr} \NT{Expr} \\
%\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
\NT{Optr}& $\rightarrow$ & \TM{>} \\
\NT{Optr}& $\rightarrow$ & \TM{+} \\
\hline
\end{tabular}
$\;\;\;\;\;\;\;$
\begin{tabular}[b]{\1c*{17}{c\1}}
\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{Expr}} & \TM{)} & \multicolumn{12}{c\1}{\NT{Stmt}} \\
\2{3-5}
\tm{if} & \tm{(} & \NT{Expr} & \NT{Optr} & \NT{Expr} & \tm{)} & \multicolumn{12}{c\1}{\nt{Stmt}} \\
\2{3-3}
\tm{if} & \tm{(} & \NT{Id} & \nt{Optr} & \nt{Expr} & \tm{)} & \multicolumn{12}{c\1}{\nt{Stmt}} \\
\2{3-3}
\tm{if} & \tm{(} & \TM{x} & \NT{Optr} & \nt{Expr} & \tm{)} & \multicolumn{12}{c\1}{\nt{Stmt}} \\
\2{4-4}
\tm{if} & \tm{(} & \tm{x} & \TM{>} & \NT{Expr} & \tm{)} & \multicolumn{12}{c\1}{\nt{Stmt}} \\
\2{5-5}
\tm{if} & \tm{(} & \tm{x} & \tm{>} & \NT{Num} & \tm{)} & \multicolumn{12}{c\1}{\nt{Stmt}} \\
\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{Expr} & \TM{;} & \multicolumn{6}{c\1}{\nt{Stmt}} & \tm{\}} \\
\2{8-8}
\tm{if} & \tm{(} & \tm{x} & \tm{>} & \tm{9} & \tm{)} & \tm{\{} & \TM{x} & \tm{=} & \NT{Expr} & \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{Expr}} & \TM{;} & \tm{\}} \\
\2{12-12}
\tm{if} & \tm{(} & \tm{x} & \tm{>} & \tm{9} & \tm{)} & \tm{\{} & \tm{x} & \tm{=} & \tm{0} & \tm{;} & \TM{y} & \tm{=} & \multicolumn{3}{c\1}{\NT{Expr}} & \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{Expr} & \NT{Optr} & \NT{Expr} & \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{Id} & \nt{Optr} & \nt{Expr} & \tm{;} & \tm{\}} \\
\2{14-14}
\tm{if} & \tm{(} & \tm{x} & \tm{>} & \tm{9} & \tm{)} & \tm{\{} & \tm{x} & \tm{=} & \tm{0} & \tm{;} & \tm{y} & \tm{=} & \TM{y} & \NT{Optr} & \nt{Expr} & \tm{;} & \tm{\}} \\
\2{15-15}
\tm{if} & \tm{(} & \tm{x} & \tm{>} & \tm{9} & \tm{)} & \tm{\{} & \tm{x} & \tm{=} & \tm{0} & \tm{;} & \tm{y} & \tm{=} & \tm{y} & \TM{+} & \NT{Expr} & \tm{;} & \tm{\}} \\
\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{+} & \NT{Num} & \tm{;} & \tm{\}} \\
\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{\}} \\
%\hline
\end{tabular}
\end{document}
|
Licensing
I, the copyright holder of this work, hereby publish it under the following license:
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.