Release coccinelle-0.1.4
[bpt/coccinelle.git] / docs / grammar / cocci_syntax.tex
CommitLineData
faf9a90c
C
1\documentclass{article}
2%\usepackage[latin9]{inputenc}
3\usepackage{hevea}
4\usepackage{fullpage}
5\usepackage{alltt}
6\usepackage{xspace}
7\usepackage[pdfborder={0 0 0}]{hyperref}
8\usepackage{listings}
9\usepackage[usenames,dvipsnames]{color}
10\usepackage[T1]{fontenc}
11\usepackage{multirow}
12
13\lstset{basicstyle=\ttfamily,numbers=left, numberstyle=\tiny, stepnumber=1, numbersep=5pt,language=C,commentstyle=\color{OliveGreen},keywordstyle=\color{blue},stringstyle=\color{BrickRed}}
14
15%
16% You must prefix the +/- lines of
17% cocci files with @+/@- respectively.
18% This will enable the automatic coloration.
19%
20% Note: You need at least the following version of hevea
21% http://hevea.inria.fr/distri/unstable/hevea-2008-12-17.tar.gz
22%
23\ifhevea % For HTML generation
24\lstdefinelanguage{Cocci}{
25morekeywords={idexpression,expression,statement,identifier,
26parameter,list,when,strict,any,forall,local,position,typedef},
27keywordstyle=\color{OliveGreen}\bfseries,
28sensitive=false,
29moredelim=[is][\color{blue}]{@M}{@M},
30moredelim=[il][\color{OliveGreen}]{@+},
31moredelim=[il][\color{BrickRed}]{@-}}
32
33\lstdefinelanguage{PatchC}[ANSI]{C}{
34stringstyle=\color{black},
35moredelim=[il][\color{OliveGreen}]{@+},
36moredelim=[il][\color{BrickRed}]{@-},
37moredelim=[il][\color{Plum}]{@M}}
38
39\else % For DVI/PS/PDF generation
40\lstdefinelanguage{Cocci}{
41morekeywords={idexpression,expression,statement,identifier,
42parameter,list,when,strict,any,forall,local,position,typedef},
43keywordstyle=\color{OliveGreen}\bfseries,
44sensitive=false,
45moredelim=*[is][\color{blue}]{@M}{@M},
46moredelim=[il][\color{OliveGreen}]{@+},
47moredelim=[il][\color{BrickRed}]{@-}}
48
49\lstdefinelanguage{PatchC}[ANSI]{C}{
50stringstyle=\color{black},
51moredelim=[il][\color{OliveGreen}]{@+},
52moredelim=[il][\color{BrickRed}]{@-},
53moredelim=[il][\color{Plum}]{@M}}
54\fi
55
56\newif\iflanguagestyle
57\languagestylefalse
58\input{grammar}
59
60\newcommand{\sizecodebis}[0]{\scriptsize}
61
62\newcommand{\mita}[1]{\mbox{\it{{#1}}}}
63\newcommand{\mtt}[1]{\mbox{\tt{{#1}}}}
64\newcommand{\msf}[1]{\mbox{\sf{{#1}}}}
65\newcommand{\stt}[1]{\mbox{\scriptsize\tt{{#1}}}}
66\newcommand{\ssf}[1]{\mbox{\scriptsize\sf{{#1}}}}
67\newcommand{\sita}[1]{\mbox{\scriptsize\it{{#1}}}}
68\newcommand{\mrm}[1]{\mbox{\rm{{#1}}}}
69\newcommand{\mth}[1]{\({#1}\)}
70\newcommand{\entails}[2]{\begin{array}{@{}c@{}}{#1}\\\hline{#2}\end{array}}
71\newcommand{\ttlb}{\mbox{\tt \char'173}}
72\newcommand{\ttrb}{\mbox{\tt \char'175}}
73\newcommand{\ttmid}{\mbox{\tt \char'174}}
74\newcommand{\tttld}{\mbox{\tt \char'176}}
75
76\newcommand{\fixme}[1]{{\color{red} #1}}
77
78\ifhevea
79\newcommand{\phantom}{}
80\newcommand{\air}{ }
81\else
82\newcommand{\air}{\phantom{xxx}}
83\fi
84
85\title{The SmPL Grammar (version 0.1.4)}
86\author{Research group on Coccinelle}
87\date{\today}
88
89\begin{document}
90\maketitle
91
92%\section{The SmPL Grammar}
93
94% This section presents the SmPL grammar. This definition follows closely
95% our implementation using the Menhir parser generator \cite{menhir}.
96
97This document presents the grammar of the SmPL language used by the
98\href{http://www.emn.fr/x-info/coccinelle}{Coccinelle tool}. For the most
99part, the grammar is written using standard notation. In some rules,
100however, the left-hand side is in all uppercase letters. These are
101macros, which take one or more grammar rule right-hand-sides as
102arguments. The grammar also uses some unspecified nonterminals, such
103as {\sf id}, {\sf const}, etc. These refer to the sets suggested by
104the name, {\em i.e.}, {\sf id} refers to the set of possible
105C-language identifiers, while {\sf const} refers to the set of
106possible C-language constants. \ifhevea A PDF version of this
107documentation is available at
108\url{http://www.emn.fr/x-info/coccinelle/docs/cocci_syntax.pdf}.\else
109A HTML version of this documentation is available online at
110\url{http://www.emn.fr/x-info/coccinelle/docs/cocci_syntax.html}. \fi
111
112%% \ifhevea A PDF
113%% version of this documentation is available at
114%% \url{http://localhost:8080/coccinelle/cocci_syntax.pdf}.\else A HTML
115%% version of this documentation is available online at
116%% \url{http://localhost:8080/coccinelle/cocci_syntax.html}. \fi
117
118\section{Program}
119
120\begin{grammar}
121 \RULE{\rt{program}}
122 \CASE{\any{\NT{include\_cocci}} \some{\NT{changeset}}}
123
124 \RULE{\rt{include\_cocci}}
125 \CASE{using \NT{string}}
126 \CASE{using \NT{pathToIsoFile}}
127
128 \RULE{\rt{changeset}}
129 \CASE{\NT{metavariables} \NT{transformation}}
130% \CASE{\NT{metavariables} \ANY{--- filename +++ filename} \NT{transformation}}
131
132\end{grammar}
133
134% Between the metavariables and the transformation rule, there can be a
135% specification of constraints on the names of the old and new files,
136% analogous to the filename specifications in the standard patch syntax.
137% (see Figure \ref{scsiglue_patch}).
138
139\section{Metavariables}
140
141The \NT{rulename} portion of the metavariable declaration can specify
142properties of a rule such as its name, the names of the rules that it
143depends on, the isomorphisms to be used in processing the rule, and whether
144quantification over paths should be universal or existential. The optional
145annotation {\tt expression} indicates that the pattern is to be considered
146as matching an expression, and thus can be used to avoid some parsing
147problems.
148
149The \NT{metadecl} portion of the metavariable declaration defines various
150types of metavariables that will be used for matching in the transformation
151section.
152
153\begin{grammar}
154 \RULE{\rt{metavariables}}
155 \CASE{@@ \any{\NT{metadecl}} @@}
156 \CASE{@ \NT{rulename} @ \any{\NT{metadecl}} @@}
157
158 \RULE{\rt{rulename}}
159 \CASE{\T{id} \OPT{extends \T{id}} \OPT{depends on \NT{dep}} \opt{\NT{iso}}
160 \opt{\NT{disable-iso}} \opt{\NT{exists}} \opt{expression}}
161 \CASE{script:\T{language} \OPT{depends on \NT{dep}}}
162
163 \RULE{\rt{dep}}
164 \CASE{\NT{pnrule}}
165 \CASE{\NT{dep} \&\& \NT{dep}}
166 \CASE{\NT{dep} || \NT{dep}}
167
168 \RULE{\rt{pnrule}}
169 \CASE{\T{id}}
170 \CASE{!\T{id}}
171 \CASE{ever \T{id}}
172 \CASE{never \T{id}}
173 \CASE{(\NT{dep})}
174
175 \RULE{\rt{iso}}
176 \CASE{using \NT{string} \ANY{, \NT{string}}}
177
178 \RULE{\rt{disable-iso}}
179 \CASE{disable \NT{COMMA\_LIST}\mth{(}\T{id}\mth{)}}
180
181 \RULE{\rt{exists}}
182 \CASE{exists}
183 \CASE{forall}
184% \CASE{\opt{reverse} forall}
185
186 \RULE{\rt{COMMA\_LIST}\mth{(}\rt{elem}\mth{)}}
187 \CASE{\NT{elem} \ANY{, \NT{elem}}}
188\end{grammar}
189
190The keyword \NT{disable-iso} is normally used with the names of
191isomorphisms defined in standard.iso or whatever isomorphism file has been
192included. There are, however, some other isomorphisms that are built into
193the implementation of Coccinelle and that can be disabled as well. Their
194names are given below. In each case, the text descibes the standard
195behavior. Using \NT{disable-iso} with the given name disables this behavior.
196
197\begin{itemize}
198\item \KW{optional\_storage}: A SmPL function definition that does not
199 specify any visibility (i.e., static or extern), or a SmPL variable
200 declaration that does not specify any storage (i.e., auto, static,
201 register, or extern), matches a function declaration or variable
202 declaration with any visibility or storage, respectively.
203\item \KW{optional\_qualifier}: This is similar to \KW{optional\_storage},
204 except that here is it the qualifier (i.e., const or volatile) that does
205 not have to be specified in the SmPL code, but may be present in the C code.
206\item \KW{value\_format}: Integers in various formats, e.g., 1 and 0x1, are
207 considered to be equivalent in the matching process.
208\item \KW{comm\_assoc}: An expression of the form \NT{exp} \NT{bin\_op}
209 \KW{...}, where \NT{bin\_op} is commutative and associative, is
210 considered to match any top-level sequence of \NT{bin\_op} operators
211 containing \NT{exp} as the top-level argument.
212\end{itemize}
213
214The possible types of metavariable declarations are defined by the grammar
215rule below. Metavariables should occur at least once in the transformation
216immediately following their declaration. Fresh metavariables must only be
217used in {\tt +} code. These properties are not expressed in the grammar,
218but are checked by a subsequent analysis. The metavariables are designated
219according to the kind of terms they can match, such as a statement, an
220identifier, or an expression. An expression metavariable can be further
221constrained by its type.
222
223\begin{grammar}
224 \RULE{\rt{metadecl}}
225 \CASE{fresh identifier \NT{ids} ;}
226 \CASE{identifier \NT{COMMA\_LIST}\mth{(}\NT{pmid\_with\_not\_eq}\mth{)} ;}
227 \CASE{parameter \opt{list} \NT{ids} ;}
228 \CASE{parameter list [ \NT{id} ] \NT{ids} ;}
229 \CASE{type \NT{ids} ;}
230 \CASE{statement \opt{list} \NT{ids} ;}
231 \CASE{typedef \NT{ids} ;}
232 \CASE{declarer name \NT{ids} ;}
233% \CASE{\opt{local} function \NT{pmid\_with\_not\_eq\_list} ;}
234 \CASE{declarer \NT{COMMA\_LIST}\mth{(}\NT{pmid\_with\_not\_eq}\mth{)} ;}
235 \CASE{iterator name \NT{ids} ;}
236 \CASE{iterator \NT{COMMA\_LIST}\mth{(}\NT{pmid\_with\_not\_eq}\mth{)} ;}
237% \CASE{error \NT{pmid\_with\_not\_eq\_list} ; }
238 \CASE{\opt{local} idexpression \opt{\NT{ctype}} \NT{COMMA\_LIST}\mth{(}\NT{pmid\_with\_not\_eq}\mth{)} ;}
239 \CASE{\opt{local} idexpression \OPT{\ttlb \NT{ctypes}\ttrb~\any{*}} \NT{COMMA\_LIST}\mth{(}\NT{pmid\_with\_not\_eq}\mth{)} ;}
240 \CASE{\opt{local} idexpression \some{*} \NT{COMMA\_LIST}\mth{(}\NT{pmid\_with\_not\_eq}\mth{)} ;}
241 \CASE{expression list \NT{ids} ;}
242 \CASE{expression \some{*} \NT{COMMA\_LIST}\mth{(}\NT{pmid\_with\_not\_eq}\mth{)} ;}
243 \CASE{expression \NT{COMMA\_LIST}\mth{(}\NT{pmid\_with\_not\_ceq}\mth{)} ;}
244 \CASE{expression list [ ident ] \NT{ids} ;}
245 \CASE{\NT{ctype} [ ] \NT{COMMA\_LIST}\mth{(}\NT{pmid\_with\_not\_eq}\mth{)} ;}
246 \CASE{\NT{ctype} \NT{COMMA\_LIST}\mth{(}\NT{pmid\_with\_not\_ceq}\mth{)} ;}
247 \CASE{\ttlb \NT{ctypes}\ttrb~\any{*} \NT{COMMA\_LIST}\mth{(}\NT{pmid\_with\_not\_ceq}\mth{)} ;}
248 \CASE{\ttlb \NT{ctypes}\ttrb~\any{*} [ ] \NT{COMMA\_LIST}\mth{(}\NT{pmid\_with\_not\_eq}\mth{)} ;}
249 \CASE{constant \opt{\NT{ctype}} \NT{COMMA\_LIST}\mth{(}\NT{pmid\_with\_not\_eq}\mth{)} ;}
250 \CASE{constant \OPT{\ttlb \NT{ctypes}\ttrb~\any{*}} \NT{COMMA\_LIST}\mth{(}\NT{pmid\_with\_not\_eq}\mth{)} ;}
251 \CASE{position \opt{any} \NT{COMMA\_LIST}\mth{(}\NT{pmid\_with\_not\_eq\_mid}\mth{)} ;}
252\end{grammar}
253
254\begin{grammar}
255 \RULE{\rt{ids}}
256 \CASE{\NT{COMMA\_LIST}\mth{(}\NT{pmid}\mth{)}}
257
258 \RULE{\rt{pmid}}
259 \CASE{\T{id}}
260 \CASE{\NT{mid}}
261% \CASE{list}
262% \CASE{error}
263% \CASE{type}
264
265 \RULE{\rt{mid}} \CASE{\T{rulename\_id}.\T{id}}
266
267 \RULE{\rt{pmid\_with\_not\_eq}}
268 \CASE{\NT{pmid} \OPT{!= \T{id}}}
269 \CASE{\NT{pmid} \OPT{!= \ttlb~\NT{COMMA\_LIST}\mth{(}\T{id}\mth{)} \ttrb}}
270
271 \RULE{\rt{pmid\_with\_not\_ceq}}
272 \CASE{\NT{pmid} \OPT{!= \NT{id\_or\_cst}}}
273 \CASE{\NT{pmid} \OPT{!= \ttlb~\NT{COMMA\_LIST}\mth{(}\NT{id\_or\_cst}\mth{)} \ttrb}}
274
275 \RULE{\rt{id\_or\_cst}}
276 \CASE{\T{id}}
277 \CASE{\T{integer}}
278
279 \RULE{\rt{pmid\_with\_not\_eq\_mid}}
280 \CASE{\NT{pmid} \OPT{!= \NT{mid}}}
281 \CASE{\NT{pmid} \OPT{!= \ttlb~\NT{COMMA\_LIST}\mth{(}\NT{mid}\mth{)} \ttrb}}
282\end{grammar}
283
284Subsequently, we refer to arbitrary metavariables as
285\mth{\msf{metaid}^{\mbox{\scriptsize{\it{ty}}}}}, where {\it{ty}}
286indicates the {\it metakind} used in the declaration of the variable.
287For example, \mth{\msf{metaid}^{\ssf{Type}}} refers to a metavariable
288that was declared using \texttt{type} and stands for any type.
289
290The \NT{ctype} and \NT{ctypes} nonterminals are used by both the grammar of
291metavariable declarations and the grammar of transformations, and are
292defined on page~\pageref{types}.
293
294\section{Transformation}
295
296The transformation specification essentially has the form of C code,
297except that lines to remove are annotated with \verb+-+ in the first
298column, and lines to add are annotated with \verb-+-. A
299transformation specification can also use {\em dots}, ``\verb-...-'',
300describing an arbitrary sequence of function arguments or instructions
301within a control-flow path. Dots may be modified with a {\tt when}
302clause, indicating a pattern that should not occur anywhere within the
303matched sequence. Finally, a transformation can specify a disjunction
304of patterns, of the form \mtt{( \mth{\mita{pat}_1} | \mita{\ldots} |
305 \mth{\mita{pat}_n} )} where each \texttt{(}, \texttt{|} or
306\texttt{)} is in column 0 or preceded by \texttt{\textbackslash}.
307
308The grammar that we present for the transformation is not actually the
309grammar of the SmPL code that can be written by the programmer, but is
310instead the grammar of the slice of this consisting of the {\tt -}
311annotated and the unannotated code (the context of the transformed lines),
312or the {\tt +} annotated code and the unannotated code. For example, for
313parsing purposes, the following transformation
314%presented in Section \ref{sec:seq2}
315is split into the two variants shown below and each is parsed
316separately.
317
318\begin{center}
319\begin{tabular}{c}
320\begin{lstlisting}[language=Cocci]
321 proc_info_func(...) {
322 <...
323@-- hostno
324@++ hostptr->host_no
325 ...>
326 }
327\end{lstlisting}\\
328\end{tabular}
329\end{center}
330
331{%\sizecodebis
332\begin{center}
333\begin{tabular}{p{5cm}p{3cm}p{5cm}}
334\begin{lstlisting}[language=Cocci]
335 proc_info_func(...) {
336 <...
337@-- hostno
338 ...>
339 }
340\end{lstlisting}
341&&
342\begin{lstlisting}[language=Cocci]
343 proc_info_func(...) {
344 <...
345@++ hostptr->host_no
346 ...>
347 }
348\end{lstlisting}
349\end{tabular}
350\end{center}
351}
352
353\noindent
354Requiring that both slices parse correctly ensures that the rule matches
355syntactically valid C code and that it produces syntactically valid C code.
356The generated parse trees are then merged for use in the subsequent
357matching and transformation process.
358
359The grammar for the minus or plus slice of a transformation is as follows:
360
361\begin{grammar}
362
363 \RULE{\rt{transformation}}
364 \CASE{\some{\NT{include}}}
365 \CASE{\NT{OPTDOTSEQ}\mth{(}\NT{expr}, \NT{when}\mth{)}}
366 \CASE{\NT{OPTDOTSEQ}\mth{(}\some{\NT{decl\_stmt}}, \NT{when}\mth{)}}
367 \CASE{\NT{OPTDOTSEQ}\mth{(}\NT{fundecl}, \NT{when}\mth{)}}
368
369 \RULE{\rt{include}}
370 \CASE{\#include \T{include\_string}}
371
372% \RULE{\rt{fun\_decl\_stmt}}
373% \CASE{\NT{decl\_stmt}}
374% \CASE{\NT{fundecl}}
375
376% \CASE{\NT{ctype}}
377% \CASE{\ttlb \NT{initialize\_list} \ttrb}
378% \CASE{\NT{toplevel\_seq\_start\_after\_dots\_init}}
379%
380% \RULE{\rt{toplevel\_seq\_start\_after\_dots\_init}}
381% \CASE{\NT{stmt\_dots} \NT{toplevel\_after\_dots}}
382% \CASE{\NT{expr} \opt{\NT{toplevel\_after\_exp}}}
383% \CASE{\NT{decl\_stmt\_expr} \opt{\NT{toplevel\_after\_stmt}}}
384%
385% \RULE{\rt{stmt\_dots}}
386% \CASE{... \any{\NT{when}}}
387% \CASE{<... \any{\NT{when}} \NT{nest\_after\_dots} ...>}
388% \CASE{<+... \any{\NT{when}} \NT{nest\_after\_dots} ...+>}
389
390 \RULE{\rt{when}}
391 \CASE{when != \NT{when\_code}}
392 \CASE{when = \NT{rule\_elem\_stmt}}
393 \CASE{when \NT{COMMA\_LIST}\mth{(}\NT{any\_strict}\mth{)}}
394 \CASE{when true != \NT{expr}}
395 \CASE{when false != \NT{expr}}
396
397 \RULE{\rt{when\_code}}
398 \CASE{\NT{OPTDOTSEQ}\mth{(}\some{\NT{decl\_stmt}}, \NT{when}\mth{)}}
399 \CASE{\NT{OPTDOTSEQ}\mth{(}\NT{expr}, \NT{when}\mth{)}}
400
401 \RULE{\rt{rule\_elem\_stmt}}
402 \CASE{\NT{one\_decl}}
403 \CASE{\NT{expr};}
404 \CASE{return \opt{\NT{expr}};}
405 \CASE{break;}
406 \CASE{continue;}
407 \CASE{\bs(\NT{rule\_elem\_stmt} \SOME{\bs| \NT{rule\_elem\_stmt}}\bs)}
408
409 \RULE{\rt{any\_strict}}
410 \CASE{any}
411 \CASE{strict}
412 \CASE{forall}
413 \CASE{exists}
414
415% \RULE{\rt{nest\_after\_dots}}
416% \CASE{\NT{decl\_stmt\_exp} \opt{\NT{nest\_after\_stmt}}}
417% \CASE{\opt{\NT{exp}} \opt{\NT{nest\_after\_exp}}}
418%
419% \RULE{\rt{nest\_after\_stmt}}
420% \CASE{\NT{stmt\_dots} \NT{nest\_after\_dots}}
421% \CASE{\NT{decl\_stmt} \opt{\NT{nest\_after\_stmt}}}
422%
423% \RULE{\rt{nest\_after\_exp}}
424% \CASE{\NT{stmt\_dots} \NT{nest\_after\_dots}}
425%
426% \RULE{\rt{toplevel\_after\_dots}}
427% \CASE{\opt{\NT{toplevel\_after\_exp}}}
428% \CASE{\NT{exp} \opt{\NT{toplevel\_after\_exp}}}
429% \CASE{\NT{decl\_stmt\_expr} \NT{toplevel\_after\_stmt}}
430%
431% \RULE{\rt{toplevel\_after\_exp}}
432% \CASE{\NT{stmt\_dots} \opt{\NT{toplevel\_after\_dots}}}
433%
434% \RULE{\rt{decl\_stmt\_expr}}
435% \CASE{TMetaStmList$^\ddag$}
436% \CASE{\NT{decl\_var}}
437% \CASE{\NT{stmt}}
438% \CASE{(\NT{stmt\_seq} \ANY{| \NT{stmt\_seq}})}
439%
440% \RULE{\rt{toplevel\_after\_stmt}}
441% \CASE{\NT{stmt\_dots} \opt{\NT{toplevel\_after\_dots}}}
442% \CASE{\NT{decl\_stmt} \NT{toplevel\_after\_stmt}}
443
444\end{grammar}
445
446\begin{grammar}
447 \RULE{\rt{OPTDOTSEQ}\mth{(}\rt{grammar\_ds}, \rt{when\_ds}\mth{)}}
448 \CASE{}\multicolumn{3}{r}{\hspace{1cm}
449 \KW{\opt{... \opt{\NT{when\_ds}}} \NT{grammar\_ds}
450 \ANY{... \opt{\NT{when\_ds}} \NT{grammar\_ds}}
451 \opt{... \opt{\NT{when\_ds}}}}
452 }
453
454% \CASE{\opt{... \opt{\NT{when\_ds}}} \NT{grammar}
455% \ANY{... \opt{\NT{when\_ds}} \NT{grammar}}
456% \opt{... \opt{\NT{when\_ds}}}}
457% \CASE{<... \any{\NT{when\_ds}} \NT{grammar} ...>}
458% \CASE{<+... \any{\NT{when\_ds}} \NT{grammar} ...+>}
459
460\end{grammar}
461
462\noindent
463Lines may be annotated with an element of the set $\{\mtt{-}, \mtt{+},
464\mtt{*}\}$ or the singleton $\mtt{?}$, or one of each set. \mtt{?}
465represents at most one match of the given pattern. \mtt{*} is used for
466semantic match, \emph{i.e.}, a pattern that highlights the fragments
467annotated with \mtt{*}, but does not perform any modification of the
468matched code. \mtt{*} cannot be mixed with \mtt{-} and \mtt{+}. There are
469some constraints on the use of these annotations:
470\begin{itemize}
471\item Dots, {\em i.e.} \texttt{...}, cannot occur on a line marked
472 \texttt{+}.
473\item Nested dots, {\em i.e.}, dots enclosed in {\tt <} and {\tt >}, cannot
474 occur on a line with any marking.
475\end{itemize}
476
477\section{Types}
478\label{types}
479
480\begin{grammar}
481
482 \RULE{\rt{ctypes}}
483 \CASE{\NT{COMMA\_LIST}\mth{(}\NT{ctype}\mth{)}}
484
485 \RULE{\rt{ctype}}
486 \CASE{\opt{\NT{const\_vol}} \NT{generic\_ctype} \any{*}}
487 \CASE{\opt{\NT{const\_vol}} void \some{*}}
488 \CASE{(\NT{ctype} \ANY{| \NT{ctype}})}
489
490 \RULE{\rt{const\_vol}}
491 \CASE{const}
492 \CASE{volatile}
493
494 \RULE{\rt{generic\_ctype}}
495 \CASE{\NT{ctype\_qualif}}
496 \CASE{\opt{\NT{ctype\_qualif}} char}
497 \CASE{\opt{\NT{ctype\_qualif}} short}
498 \CASE{\opt{\NT{ctype\_qualif}} int}
499 \CASE{\opt{\NT{ctype\_qualif}} long}
500 \CASE{\opt{\NT{ctype\_qualif}} long long}
501 \CASE{double}
502 \CASE{float}
503 \CASE{\OPT{struct\OR union} \T{id} \OPT{\{ \any{\NT{struct\_decl\_list}} \}}}
504
505 \RULE{\rt{ctype\_qualif}}
506 \CASE{unsigned}
507 \CASE{signed}
508
509 \RULE{\rt{struct\_decl\_list}}
510 \CASE{\NT{struct\_decl\_list\_start}}
511
512 \RULE{\rt{struct\_decl\_list\_start}}
513 \CASE{\NT{struct\_decl}}
514 \CASE{\NT{struct\_decl} \NT{struct\_decl\_list\_start}}
515 \CASE{... \opt{when != \NT{struct\_decl}}$^\dag$ \opt{\NT{continue\_struct\_decl\_list}}}
516
517 \RULE{\rt{continue\_struct\_decl\_list}}
518 \CASE{\NT{struct\_decl} \NT{struct\_decl\_list\_start}}
519 \CASE{\NT{struct\_decl}}
520
521 \RULE{\rt{struct\_decl}}
522 \CASE{\NT{ctype} \NT{d\_ident};}
523 \CASE{\NT{fn\_ctype} (* \NT{d\_ident}) (\NT{PARAMSEQ}\mth{(}\NT{name\_opt\_decl}, \mth{\varepsilon)});)}
524 \CASE{\opt{\NT{const\_vol}} \T{id} \NT{d\_ident};}
525
526 \RULE{\rt{d\_ident}}
527 \CASE{\NT{id} \any{[\opt{\NT{expr}}]}}
528
529 \RULE{\rt{fn\_ctype}}
530 \CASE{\NT{generic\_ctype} \any{*}}
531 \CASE{void \any{*}}
532
533 \RULE{\rt{name\_opt\_decl}}
534 \CASE{\NT{decl}}
535 \CASE{\NT{ctype}}
536 \CASE{\NT{fn\_ctype}}
537\end{grammar}
538
539$^\dag$ The optional \texttt{when} construct ends at the end of the line.
540
541\section{Function declarations}
542
543\begin{grammar}
544
545 \RULE{\rt{fundecl}}
546 \CASE{\opt{\NT{fn\_ctype}} \any{\NT{funinfo}} \NT{funid}
547 (\opt{\NT{PARAMSEQ}\mth{(}\NT{param}, \mth{\varepsilon)}})
548 \ttlb~\opt{\NT{stmt\_seq}} \ttrb}
549
550 \RULE{\rt{funproto}}
551 \CASE{\opt{\NT{fn\_ctype}} \any{\NT{funinfo}} \NT{funid}
552 (\opt{\NT{PARAMSEQ}\mth{(}\NT{param}, \mth{\varepsilon)}});}
553
554 \RULE{\rt{funinfo}}
555 \CASE{inline}
556 \CASE{\NT{storage}}
557% \CASE{\NT{attr}}
558
559 \RULE{\rt{storage}}
560 \CASE{static}
561 \CASE{auto}
562 \CASE{register}
563 \CASE{extern}
564
565 \RULE{\rt{funid}}
566 \CASE{\T{id}}
567 \CASE{\mth{\T{metaid}^{\ssf{Id}}}}
568% \CASE{\mth{\T{metaid}^{\ssf{Func}}}}
569% \CASE{\mth{\T{metaid}^{\ssf{LocalFunc}}}}
570
571 \RULE{\rt{param}}
572 \CASE{\NT{type} \T{id}}
573 \CASE{\mth{\T{metaid}^{\ssf{Param}}}}
574 \CASE{\mth{\T{metaid}^{\ssf{ParamList}}}}
575
576 \RULE{\rt{decl}}
577 \CASE{\NT{ctype} \NT{id}}
578 \CASE{\NT{fn\_ctype} (* \NT{id}) (\NT{PARAMSEQ}\mth{(}\NT{name\_opt\_decl}, \mth{\varepsilon)})}
579 \CASE{void}
580 \CASE{\mth{\T{metaid}^{\ssf{Param}}}}
581\end{grammar}
582
583\begin{grammar}
584 \RULE{\rt{PARAMSEQ}\mth{(}\rt{gram\_p}, \rt{when\_p}\mth{)}}
585 \CASE{\NT{COMMA\_LIST}\mth{(}\NT{gram\_p} \OR \ldots \opt{\NT{when\_p}}\mth{)}}
586\end{grammar}
587
588%\newpage
589
590\section{Declarations}
591
592\begin{grammar}
593 \RULE{\rt{decl\_var}}
594% \CASE{\NT{type} \opt{\NT{id} \opt{[\opt{\NT{dot\_expr}}]}
595% \ANY{, \NT{id} \opt{[ \opt{\NT{dot\_expr}}]}}};}
596 \CASE{\NT{common\_decl}}
597 \CASE{\opt{\NT{storage}} \NT{ctype} \NT{COMMA\_LIST}\mth{(}\NT{d\_ident}\mth{)} ;}
598 \CASE{\opt{\NT{storage}} \opt{\NT{const\_vol}} \T{id} \NT{COMMA\_LIST}\mth{(}\NT{d\_ident}\mth{)} ;}
599 \CASE{\opt{\NT{storage}} \NT{fn\_ctype} ( * \NT{d\_ident} ) ( \NT{PARAMSEQ}\mth{(}\NT{name\_opt\_decl}, \mth{\varepsilon)} ) = \NT{initialize} ;}
600 \CASE{typedef \NT{ctype} \NT{typedef\_ident} ;}
601
602 \RULE{\rt{one\_decl}}
603 \CASE{\NT{common\_decl}}
604 \CASE{\opt{\NT{storage}} \NT{ctype} \NT{id};}
605% \CASE{\NT{storage} \NT{ctype} \NT{id} \opt{[\opt{\NT{dot\\_expr}}]} = \NT{nest\\_expr};}
606 \CASE{\opt{\NT{storage}} \opt{\NT{const\_vol}} \T{id} \NT{d\_ident} ;}
607
608 \RULE{\rt{common\_decl}}
609 \CASE{\NT{ctype};}
610 \CASE{\NT{funproto}}
611 \CASE{\opt{\NT{storage}} \NT{ctype} \NT{d\_ident} = \NT{initialize} ;}
612 \CASE{\opt{\NT{storage}} \opt{\NT{const\_vol}} \T{id} \NT{d\_ident} = \NT{initialize} ;}
613 \CASE{\opt{\NT{storage}} \NT{fn\_ctype} ( * \NT{d\_ident} ) ( \NT{PARAMSEQ}\mth{(}\NT{name\_opt\_decl}, \mth{\varepsilon)} ) ;}
614 \CASE{\NT{decl\_ident} ( \OPT{\NT{COMMA\_LIST}\mth{(}\NT{expr}\mth{)}} ) ;}
615
616 \RULE{\rt{initialize}}
617 \CASE{\NT{dot\_expr}}
618 \CASE{\ttlb~\opt{\NT{COMMA\_LIST}\mth{(}\NT{dot\_expr}\mth{)}}~\ttrb}
619
620 \RULE{\rt{decl\_ident}}
621 \CASE{\T{DeclarerId}}
622 \CASE{\mth{\T{metaid}^{\ssf{Declarer}}}}
623\end{grammar}
624
625\section{Statements}
626
627The first rule {\em statement} describes the various forms of a statement.
628The remaining rules implement the constraints that are sensitive to the
629context in which the statement occurs: {\em single\_statement} for a
630context in which only one statement is allowed, and {\em decl\_statement}
631for a context in which a declaration, statement, or sequence thereof is
632allowed.
633
634\begin{grammar}
635 \RULE{\rt{stmt}}
636 \CASE{\NT{include}}
637 \CASE{\mth{\T{metaid}^{\ssf{Stmt}}}}
638 \CASE{\NT{expr};}
639 \CASE{if (\NT{dot\_expr}) \NT{single\_stmt} \opt{else \NT{single\_stmt}}}
640 \CASE{for (\opt{\NT{dot\_expr}}; \opt{\NT{dot\_expr}}; \opt{\NT{dot\_expr}})
641 \NT{single\_stmt}}
642 \CASE{while (\NT{dot\_expr}) \NT{single\_stmt}}
643 \CASE{do \NT{single\_stmt} while (\NT{dot\_expr});}
644 \CASE{\NT{iter\_ident} (\any{\NT{dot\_expr}}) \NT{single\_stmt}}
645 \CASE{switch (\opt{\NT{dot\_expr}}) \ttlb \any{\NT{case\_line}} \ttrb}
646 \CASE{return \opt{\NT{dot\_expr}};}
647 \CASE{\ttlb~\opt{\NT{stmt\_seq}} \ttrb}
648 \CASE{\NT{NEST}\mth{(}\some{\NT{decl\_stmt}}, \NT{when}\mth{)}}
649 \CASE{\NT{NEST}\mth{(}\NT{expr}, \NT{when}\mth{)}}
650 \CASE{break;}
651 \CASE{continue;}
652 \CASE{\NT{id}:}
653 \CASE{goto \NT{id};}
654 \CASE{\ttlb \NT{stmt\_seq} \ttrb}
655
656 \RULE{\rt{single\_stmt}}
657 \CASE{\NT{stmt}}
658 \CASE{\NT{OR}\mth{(}\NT{stmt}\mth{)}}
659
660 \RULE{\rt{decl\_stmt}}
661 \CASE{\mth{\T{metaid}^{\ssf{StmtList}}}}
662 \CASE{\NT{decl\_var}}
663 \CASE{\NT{stmt}}
664 \CASE{\NT{OR}\mth{(}\NT{stmt\_seq}\mth{)}}
665
666 \RULE{\rt{stmt\_seq}}
667 \CASE{\any{\NT{decl\_stmt}}
668 \opt{\NT{DOTSEQ}\mth{(}\some{\NT{decl\_stmt}},
669 \NT{when}\mth{)} \any{\NT{decl\_stmt}}}}
670 \CASE{\any{\NT{decl\_stmt}}
671 \opt{\NT{DOTSEQ}\mth{(}\NT{expr},
672 \NT{when}\mth{)} \any{\NT{decl\_stmt}}}}
673
674 \RULE{\rt{case\_line}}
675 \CASE{default :~\NT{stmt\_seq}}
676 \CASE{case \NT{dot\_expr} :~\NT{stmt\_seq}}
677
678 \RULE{\rt{iter\_ident}}
679 \CASE{\T{IteratorId}}
680 \CASE{\mth{\T{metaid}^{\ssf{Iterator}}}}
681\end{grammar}
682
683\begin{grammar}
684 \RULE{\rt{OR}\mth{(}\rt{gram\_o}\mth{)}}
685 \CASE{( \NT{gram\_o} \ANY{\ttmid \NT{gram\_o}})}
686
687 \RULE{\rt{DOTSEQ}\mth{(}\rt{gram\_d}, \rt{when\_d}\mth{)}}
688 \CASE{\ldots \opt{\NT{when\_d}} \ANY{\NT{gram\_d} \ldots \opt{\NT{when\_d}}}}
689
690 \RULE{\rt{NEST}\mth{(}\rt{gram\_n}, \rt{when\_n}\mth{)}}
691 \CASE{<\ldots \opt{\NT{when\_n}} \NT{gram\_n} \ANY{\ldots \opt{\NT{when\_n}} \NT{gram\_n}} \ldots>}
692 \CASE{<+\ldots \opt{\NT{when\_n}} \NT{gram\_n} \ANY{\ldots \opt{\NT{when\_n}} \NT{gram\_n}} \ldots+>}
693\end{grammar}
694
695\noindent
696OR is a macro that generates a disjunction of patterns. The three
697tokens \T{(}, \T{\ttmid}, and \T{)} must appear in the leftmost
698column, to differentiate them from the parentheses and bit-or tokens
699that can appear within expressions (and cannot appear in the leftmost
700column). These token may also be preceded by \texttt{\bs}
701when they are used in an other column. These tokens are furthermore
702different from (, \(\mid\), and ), which are part of the grammar
703metalanguage.
704
705\section{Expressions}
706
707A nest or a single ellipsis is allowed in some expression contexts, and
708causes ambiguity in others. For example, in a sequence \mtt{\ldots
709\mita{expr} \ldots}, the nonterminal \mita{expr} must be instantiated as an
710explicit C-language expression, while in an array reference,
711\mtt{\mth{\mita{expr}_1} \mtt{[} \mth{\mita{expr}_2} \mtt{]}}, the
712nonterminal \mth{\mita{expr}_2}, because it is delimited by brackets, can
713be also instantiated as \mtt{\ldots}, representing an arbitrary expression. To
714distinguish between the various possibilities, we define three nonterminals
715for expressions: {\em expr} does not allow either top-level nests or
716ellipses, {\em nest\_expr} allows a nest but not an ellipsis, and {\em
717dot\_expr} allows both. The EXPR macro is used to express these variants
718in a concise way.
719
720\begin{grammar}
721 \RULE{\rt{expr}}
722 \CASE{\NT{EXPR}\mth{(}\NT{expr}\mth{)}}
723
724 \RULE{\rt{nest\_expr}}
725 \CASE{\NT{EXPR}\mth{(}\NT{nest\_expr}\mth{)}}
726 \CASE{\NT{NEST}\mth{(}\NT{nest\_expr}, \NT{exp\_whencode}\mth{)}}
727
728 \RULE{\rt{dot\_expr}}
729 \CASE{\NT{EXPR}\mth{(}\NT{dot\_expr}\mth{)}}
730 \CASE{\NT{NEST}\mth{(}\NT{dot\_expr}, \NT{exp\_whencode}\mth{)}}
731 \CASE{...~\opt{\NT{exp\_whencode}}}
732
733 \RULE{\rt{EXPR}\mth{(}\rt{exp}\mth{)}}
734 \CASE{\NT{exp} \NT{assign\_op} \NT{exp}}
735 \CASE{\NT{exp}++}
736 \CASE{\NT{exp}--}
737 \CASE{\NT{unary\_op} \NT{exp}}
738 \CASE{\NT{exp} \NT{bin\_op} \NT{exp}}
739 \CASE{\NT{exp} ?~\NT{dot\_expr} :~\NT{exp}}
740 \CASE{(\NT{type}) \NT{exp}}
741 \CASE{\NT{exp} [\NT{dot\_expr}]}
742 \CASE{\NT{exp} .~\NT{id}}
743 \CASE{\NT{exp} -> \NT{id}}
744 \CASE{\NT{exp}(\opt{\NT{PARAMSEQ}\mth{(}\NT{arg}, \NT{exp\_whencode}\mth{)}})}
745 \CASE{\NT{id}}
746% \CASE{\mth{\T{metaid}^{\ssf{Func}}}}
747% \CASE{\mth{\T{metaid}^{\ssf{LocalFunc}}}}
748 \CASE{\mth{\T{metaid}^{\ssf{Exp}}}}
749% \CASE{\mth{\T{metaid}^{\ssf{Err}}}}
750 \CASE{\mth{\T{metaid}^{\ssf{Const}}}}
751 \CASE{\NT{const}}
752 \CASE{(\NT{dot\_expr})}
753 \CASE{\NT{OR}\mth{(}\NT{exp}\mth{)}}
754
755 \RULE{\rt{arg}}
756 \CASE{\NT{nest\_expr}}
757 \CASE{\mth{\T{metaid}^{\ssf{ExpList}}}}
758
759 \RULE{\rt{exp\_whencode}}
760 \CASE{when != \NT{expr}}
761
762 \RULE{\rt{assign\_op}}
763 \CASE{= \OR -= \OR += \OR *= \OR /= \OR \%=}
764 \CASE{\&= \OR |= \OR \caret= \OR \lt\lt= \OR \gt\gt=}
765
766 \RULE{\rt{bin\_op}}
767 \CASE{* \OR / \OR \% \OR + \OR -}
768 \CASE{\lt\lt \OR \gt\gt \OR \caret\xspace \OR \& \OR \ttmid}
769 \CASE{< \OR > \OR <= \OR >= \OR == \OR != \OR \&\& \OR \ttmid\ttmid}
770
771 \RULE{\rt{unary\_op}}
772 \CASE{++ \OR -- \OR \& \OR * \OR + \OR - \OR !}
773
774\end{grammar}
775
776\section{Constant, Identifiers and Types for Transformations}
777
778\begin{grammar}
779 \RULE{\rt{const}}
780 \CASE{\NT{string}}
781 \CASE{[0-9]+}
782 \CASE{\mth{\cdots}}
783
784 \RULE{\rt{string}}
785 \CASE{"\any{[\^{}"]}"}
786
787 \RULE{\rt{id}}
788 \CASE{\T{id} \OR \mth{\T{metaid}^{\ssf{Id}}}}
789
790 \RULE{\rt{typedef\_ident}}
791 \CASE{\T{id} \OR \mth{\T{metaid}^{\ssf{Type}}}}
792
793 \RULE{\rt{type}}
794 \CASE{\NT{ctype} \OR \mth{\T{metaid}^{\ssf{Type}}}}
795
796 \RULE{\rt{pathToIsoFile}}
797 \CASE{<.*>}
798\end{grammar}
799
800\include{examples}
801\end{document}
802
803%%% Local Variables:
804%%% mode: LaTeX
805%%% TeX-master: "cocci_syntax"
806%%% coding: latin-9
807%%% TeX-PDF-mode: t
808%%% ispell-local-dictionary: "american"
809%%% End: