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