PageRenderTime 60ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/share/doc/cpp/Tokenization.html

https://gitlab.com/infected_/linaro_aarch64-linux-gnu-4.9.x
HTML | 255 lines | 196 code | 12 blank | 47 comment | 0 complexity | dc320d64029476ea3f9befff968a05ae MD5 | raw file
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- Copyright (C) 1987-2015 Free Software Foundation, Inc.
  4. Permission is granted to copy, distribute and/or modify this document
  5. under the terms of the GNU Free Documentation License, Version 1.3 or
  6. any later version published by the Free Software Foundation. A copy of
  7. the license is included in the
  8. section entitled "GNU Free Documentation License".
  9. This manual contains no Invariant Sections. The Front-Cover Texts are
  10. (a) (see below), and the Back-Cover Texts are (b) (see below).
  11. (a) The FSF's Front-Cover Text is:
  12. A GNU Manual
  13. (b) The FSF's Back-Cover Text is:
  14. You have freedom to copy and modify this GNU Manual, like GNU
  15. software. Copies published by the Free Software Foundation raise
  16. funds for GNU development. -->
  17. <!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ -->
  18. <head>
  19. <title>The C Preprocessor: Tokenization</title>
  20. <meta name="description" content="The C Preprocessor: Tokenization">
  21. <meta name="keywords" content="The C Preprocessor: Tokenization">
  22. <meta name="resource-type" content="document">
  23. <meta name="distribution" content="global">
  24. <meta name="Generator" content="makeinfo">
  25. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  26. <link href="index.html#Top" rel="start" title="Top">
  27. <link href="Index-of-Directives.html#Index-of-Directives" rel="index" title="Index of Directives">
  28. <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
  29. <link href="Overview.html#Overview" rel="up" title="Overview">
  30. <link href="The-preprocessing-language.html#The-preprocessing-language" rel="next" title="The preprocessing language">
  31. <link href="Initial-processing.html#Initial-processing" rel="prev" title="Initial processing">
  32. <style type="text/css">
  33. <!--
  34. a.summary-letter {text-decoration: none}
  35. blockquote.smallquotation {font-size: smaller}
  36. div.display {margin-left: 3.2em}
  37. div.example {margin-left: 3.2em}
  38. div.indentedblock {margin-left: 3.2em}
  39. div.lisp {margin-left: 3.2em}
  40. div.smalldisplay {margin-left: 3.2em}
  41. div.smallexample {margin-left: 3.2em}
  42. div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
  43. div.smalllisp {margin-left: 3.2em}
  44. kbd {font-style:oblique}
  45. pre.display {font-family: inherit}
  46. pre.format {font-family: inherit}
  47. pre.menu-comment {font-family: serif}
  48. pre.menu-preformatted {font-family: serif}
  49. pre.smalldisplay {font-family: inherit; font-size: smaller}
  50. pre.smallexample {font-size: smaller}
  51. pre.smallformat {font-family: inherit; font-size: smaller}
  52. pre.smalllisp {font-size: smaller}
  53. span.nocodebreak {white-space:nowrap}
  54. span.nolinebreak {white-space:nowrap}
  55. span.roman {font-family:serif; font-weight:normal}
  56. span.sansserif {font-family:sans-serif; font-weight:normal}
  57. ul.no-bullet {list-style: none}
  58. -->
  59. </style>
  60. </head>
  61. <body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
  62. <a name="Tokenization"></a>
  63. <div class="header">
  64. <p>
  65. Next: <a href="The-preprocessing-language.html#The-preprocessing-language" accesskey="n" rel="next">The preprocessing language</a>, Previous: <a href="Initial-processing.html#Initial-processing" accesskey="p" rel="prev">Initial processing</a>, Up: <a href="Overview.html#Overview" accesskey="u" rel="up">Overview</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Index-of-Directives.html#Index-of-Directives" title="Index" rel="index">Index</a>]</p>
  66. </div>
  67. <hr>
  68. <a name="Tokenization-1"></a>
  69. <h3 class="section">1.3 Tokenization</h3>
  70. <a name="index-tokens"></a>
  71. <a name="index-preprocessing-tokens"></a>
  72. <p>After the textual transformations are finished, the input file is
  73. converted into a sequence of <em>preprocessing tokens</em>. These mostly
  74. correspond to the syntactic tokens used by the C compiler, but there are
  75. a few differences. White space separates tokens; it is not itself a
  76. token of any kind. Tokens do not have to be separated by white space,
  77. but it is often necessary to avoid ambiguities.
  78. </p>
  79. <p>When faced with a sequence of characters that has more than one possible
  80. tokenization, the preprocessor is greedy. It always makes each token,
  81. starting from the left, as big as possible before moving on to the next
  82. token. For instance, <code>a+++++b</code> is interpreted as
  83. <code>a&nbsp;++&nbsp;++&nbsp;+&nbsp;b<!-- /@w --></code>, not as <code>a&nbsp;++&nbsp;+&nbsp;++&nbsp;b<!-- /@w --></code>, even though the
  84. latter tokenization could be part of a valid C program and the former
  85. could not.
  86. </p>
  87. <p>Once the input file is broken into tokens, the token boundaries never
  88. change, except when the &lsquo;<samp>##</samp>&rsquo; preprocessing operator is used to paste
  89. tokens together. See <a href="Concatenation.html#Concatenation">Concatenation</a>. For example,
  90. </p>
  91. <div class="smallexample">
  92. <pre class="smallexample">#define foo() bar
  93. foo()baz
  94. &rarr; bar baz
  95. <em>not</em>
  96. &rarr; barbaz
  97. </pre></div>
  98. <p>The compiler does not re-tokenize the preprocessor&rsquo;s output. Each
  99. preprocessing token becomes one compiler token.
  100. </p>
  101. <a name="index-identifiers"></a>
  102. <p>Preprocessing tokens fall into five broad classes: identifiers,
  103. preprocessing numbers, string literals, punctuators, and other. An
  104. <em>identifier</em> is the same as an identifier in C: any sequence of
  105. letters, digits, or underscores, which begins with a letter or
  106. underscore. Keywords of C have no significance to the preprocessor;
  107. they are ordinary identifiers. You can define a macro whose name is a
  108. keyword, for instance. The only identifier which can be considered a
  109. preprocessing keyword is <code>defined</code>. See <a href="Defined.html#Defined">Defined</a>.
  110. </p>
  111. <p>This is mostly true of other languages which use the C preprocessor.
  112. However, a few of the keywords of C++ are significant even in the
  113. preprocessor. See <a href="C_002b_002b-Named-Operators.html#C_002b_002b-Named-Operators">C++ Named Operators</a>.
  114. </p>
  115. <p>In the 1999 C standard, identifiers may contain letters which are not
  116. part of the &ldquo;basic source character set&rdquo;, at the implementation&rsquo;s
  117. discretion (such as accented Latin letters, Greek letters, or Chinese
  118. ideograms). This may be done with an extended character set, or the
  119. &lsquo;<samp>\u</samp>&rsquo; and &lsquo;<samp>\U</samp>&rsquo; escape sequences. The implementation of this
  120. feature in GCC is experimental; such characters are only accepted in
  121. the &lsquo;<samp>\u</samp>&rsquo; and &lsquo;<samp>\U</samp>&rsquo; forms and only if
  122. <samp>-fextended-identifiers</samp> is used.
  123. </p>
  124. <p>As an extension, GCC treats &lsquo;<samp>$</samp>&rsquo; as a letter. This is for
  125. compatibility with some systems, such as VMS, where &lsquo;<samp>$</samp>&rsquo; is commonly
  126. used in system-defined function and object names. &lsquo;<samp>$</samp>&rsquo; is not a
  127. letter in strictly conforming mode, or if you specify the <samp>-$</samp>
  128. option. See <a href="Invocation.html#Invocation">Invocation</a>.
  129. </p>
  130. <a name="index-numbers"></a>
  131. <a name="index-preprocessing-numbers"></a>
  132. <p>A <em>preprocessing number</em> has a rather bizarre definition. The
  133. category includes all the normal integer and floating point constants
  134. one expects of C, but also a number of other things one might not
  135. initially recognize as a number. Formally, preprocessing numbers begin
  136. with an optional period, a required decimal digit, and then continue
  137. with any sequence of letters, digits, underscores, periods, and
  138. exponents. Exponents are the two-character sequences &lsquo;<samp>e+</samp>&rsquo;,
  139. &lsquo;<samp>e-</samp>&rsquo;, &lsquo;<samp>E+</samp>&rsquo;, &lsquo;<samp>E-</samp>&rsquo;, &lsquo;<samp>p+</samp>&rsquo;, &lsquo;<samp>p-</samp>&rsquo;, &lsquo;<samp>P+</samp>&rsquo;, and
  140. &lsquo;<samp>P-</samp>&rsquo;. (The exponents that begin with &lsquo;<samp>p</samp>&rsquo; or &lsquo;<samp>P</samp>&rsquo; are new
  141. to C99. They are used for hexadecimal floating-point constants.)
  142. </p>
  143. <p>The purpose of this unusual definition is to isolate the preprocessor
  144. from the full complexity of numeric constants. It does not have to
  145. distinguish between lexically valid and invalid floating-point numbers,
  146. which is complicated. The definition also permits you to split an
  147. identifier at any position and get exactly two tokens, which can then be
  148. pasted back together with the &lsquo;<samp>##</samp>&rsquo; operator.
  149. </p>
  150. <p>It&rsquo;s possible for preprocessing numbers to cause programs to be
  151. misinterpreted. For example, <code>0xE+12</code> is a preprocessing number
  152. which does not translate to any valid numeric constant, therefore a
  153. syntax error. It does not mean <code>0xE&nbsp;+&nbsp;12<!-- /@w --></code>, which is what you
  154. might have intended.
  155. </p>
  156. <a name="index-string-literals"></a>
  157. <a name="index-string-constants"></a>
  158. <a name="index-character-constants"></a>
  159. <a name="index-header-file-names"></a>
  160. <p><em>String literals</em> are string constants, character constants, and
  161. header file names (the argument of &lsquo;<samp>#include</samp>&rsquo;).<a name="DOCF2" href="#FOOT2"><sup>2</sup></a> String constants and character
  162. constants are straightforward: <tt>&quot;&hellip;&quot;</tt> or <tt>'&hellip;'</tt>. In
  163. either case embedded quotes should be escaped with a backslash:
  164. <tt>'\''</tt> is the character constant for &lsquo;<samp>'</samp>&rsquo;. There is no limit on
  165. the length of a character constant, but the value of a character
  166. constant that contains more than one character is
  167. implementation-defined. See <a href="Implementation-Details.html#Implementation-Details">Implementation Details</a>.
  168. </p>
  169. <p>Header file names either look like string constants, <tt>&quot;&hellip;&quot;</tt>, or are
  170. written with angle brackets instead, <tt>&lt;&hellip;&gt;</tt>. In either case,
  171. backslash is an ordinary character. There is no way to escape the
  172. closing quote or angle bracket. The preprocessor looks for the header
  173. file in different places depending on which form you use. See <a href="Include-Operation.html#Include-Operation">Include Operation</a>.
  174. </p>
  175. <p>No string literal may extend past the end of a line. Older versions
  176. of GCC accepted multi-line string constants. You may use continued
  177. lines instead, or string constant concatenation. See <a href="Differences-from-previous-versions.html#Differences-from-previous-versions">Differences from previous versions</a>.
  178. </p>
  179. <a name="index-punctuators"></a>
  180. <a name="index-digraphs"></a>
  181. <a name="index-alternative-tokens"></a>
  182. <p><em>Punctuators</em> are all the usual bits of punctuation which are
  183. meaningful to C and C++. All but three of the punctuation characters in
  184. ASCII are C punctuators. The exceptions are &lsquo;<samp>@</samp>&rsquo;, &lsquo;<samp>$</samp>&rsquo;, and
  185. &lsquo;<samp>`</samp>&rsquo;. In addition, all the two- and three-character operators are
  186. punctuators. There are also six <em>digraphs</em>, which the C++ standard
  187. calls <em>alternative tokens</em>, which are merely alternate ways to spell
  188. other punctuators. This is a second attempt to work around missing
  189. punctuation in obsolete systems. It has no negative side effects,
  190. unlike trigraphs, but does not cover as much ground. The digraphs and
  191. their corresponding normal punctuators are:
  192. </p>
  193. <div class="smallexample">
  194. <pre class="smallexample">Digraph: &lt;% %&gt; &lt;: :&gt; %: %:%:
  195. Punctuator: { } [ ] # ##
  196. </pre></div>
  197. <a name="index-other-tokens"></a>
  198. <p>Any other single character is considered &ldquo;other&rdquo;. It is passed on to
  199. the preprocessor&rsquo;s output unmolested. The C compiler will almost
  200. certainly reject source code containing &ldquo;other&rdquo; tokens. In ASCII, the
  201. only other characters are &lsquo;<samp>@</samp>&rsquo;, &lsquo;<samp>$</samp>&rsquo;, &lsquo;<samp>`</samp>&rsquo;, and control
  202. characters other than NUL (all bits zero). (Note that &lsquo;<samp>$</samp>&rsquo; is
  203. normally considered a letter.) All characters with the high bit set
  204. (numeric range 0x7F&ndash;0xFF) are also &ldquo;other&rdquo; in the present
  205. implementation. This will change when proper support for international
  206. character sets is added to GCC.
  207. </p>
  208. <p>NUL is a special case because of the high probability that its
  209. appearance is accidental, and because it may be invisible to the user
  210. (many terminals do not display NUL at all). Within comments, NULs are
  211. silently ignored, just as any other character would be. In running
  212. text, NUL is considered white space. For example, these two directives
  213. have the same meaning.
  214. </p>
  215. <div class="smallexample">
  216. <pre class="smallexample">#define X^@1
  217. #define X 1
  218. </pre></div>
  219. <p>(where &lsquo;<samp>^@</samp>&rsquo; is ASCII NUL). Within string or character constants,
  220. NULs are preserved. In the latter two cases the preprocessor emits a
  221. warning message.
  222. </p>
  223. <div class="footnote">
  224. <hr>
  225. <h4 class="footnotes-heading">Footnotes</h4>
  226. <h3><a name="FOOT2" href="#DOCF2">(2)</a></h3>
  227. <p>The C
  228. standard uses the term <em>string literal</em> to refer only to what we are
  229. calling <em>string constants</em>.</p>
  230. </div>
  231. <hr>
  232. <div class="header">
  233. <p>
  234. Next: <a href="The-preprocessing-language.html#The-preprocessing-language" accesskey="n" rel="next">The preprocessing language</a>, Previous: <a href="Initial-processing.html#Initial-processing" accesskey="p" rel="prev">Initial processing</a>, Up: <a href="Overview.html#Overview" accesskey="u" rel="up">Overview</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Index-of-Directives.html#Index-of-Directives" title="Index" rel="index">Index</a>]</p>
  235. </div>
  236. </body>
  237. </html>