You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Tokenization.html 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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-2020 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 6.5, http://www.gnu.org/software/texinfo/ -->
  18. <head>
  19. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  20. <title>Tokenization (The C Preprocessor)</title>
  21. <meta name="description" content="Tokenization (The C Preprocessor)">
  22. <meta name="keywords" content="Tokenization (The C Preprocessor)">
  23. <meta name="resource-type" content="document">
  24. <meta name="distribution" content="global">
  25. <meta name="Generator" content="makeinfo">
  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.indentedblock {margin-right: 0em}
  36. blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
  37. blockquote.smallquotation {font-size: smaller}
  38. div.display {margin-left: 3.2em}
  39. div.example {margin-left: 3.2em}
  40. div.lisp {margin-left: 3.2em}
  41. div.smalldisplay {margin-left: 3.2em}
  42. div.smallexample {margin-left: 3.2em}
  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.nolinebreak {white-space: nowrap}
  54. span.roman {font-family: initial; font-weight: normal}
  55. span.sansserif {font-family: sans-serif; font-weight: normal}
  56. ul.no-bullet {list-style: none}
  57. -->
  58. </style>
  59. </head>
  60. <body lang="en">
  61. <a name="Tokenization"></a>
  62. <div class="header">
  63. <p>
  64. 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>
  65. </div>
  66. <hr>
  67. <a name="Tokenization-1"></a>
  68. <h3 class="section">1.3 Tokenization</h3>
  69. <a name="index-tokens"></a>
  70. <a name="index-preprocessing-tokens"></a>
  71. <p>After the textual transformations are finished, the input file is
  72. converted into a sequence of <em>preprocessing tokens</em>. These mostly
  73. correspond to the syntactic tokens used by the C compiler, but there are
  74. a few differences. White space separates tokens; it is not itself a
  75. token of any kind. Tokens do not have to be separated by white space,
  76. but it is often necessary to avoid ambiguities.
  77. </p>
  78. <p>When faced with a sequence of characters that has more than one possible
  79. tokenization, the preprocessor is greedy. It always makes each token,
  80. starting from the left, as big as possible before moving on to the next
  81. token. For instance, <code>a+++++b</code> is interpreted as
  82. <code>a&nbsp;++&nbsp;++&nbsp;+&nbsp;b<!-- /@w --></code>, not as <code>a&nbsp;++&nbsp;+&nbsp;++&nbsp;b<!-- /@w --></code>, even though the
  83. latter tokenization could be part of a valid C program and the former
  84. could not.
  85. </p>
  86. <p>Once the input file is broken into tokens, the token boundaries never
  87. change, except when the &lsquo;<samp>##</samp>&rsquo; preprocessing operator is used to paste
  88. tokens together. See <a href="Concatenation.html#Concatenation">Concatenation</a>. For example,
  89. </p>
  90. <div class="smallexample">
  91. <pre class="smallexample">#define foo() bar
  92. foo()baz
  93. &rarr; bar baz
  94. <em>not</em>
  95. &rarr; barbaz
  96. </pre></div>
  97. <p>The compiler does not re-tokenize the preprocessor&rsquo;s output. Each
  98. preprocessing token becomes one compiler token.
  99. </p>
  100. <a name="index-identifiers"></a>
  101. <p>Preprocessing tokens fall into five broad classes: identifiers,
  102. preprocessing numbers, string literals, punctuators, and other. An
  103. <em>identifier</em> is the same as an identifier in C: any sequence of
  104. letters, digits, or underscores, which begins with a letter or
  105. underscore. Keywords of C have no significance to the preprocessor;
  106. they are ordinary identifiers. You can define a macro whose name is a
  107. keyword, for instance. The only identifier which can be considered a
  108. preprocessing keyword is <code>defined</code>. See <a href="Defined.html#Defined">Defined</a>.
  109. </p>
  110. <p>This is mostly true of other languages which use the C preprocessor.
  111. However, a few of the keywords of C++ are significant even in the
  112. preprocessor. See <a href="C_002b_002b-Named-Operators.html#C_002b_002b-Named-Operators">C++ Named Operators</a>.
  113. </p>
  114. <p>In the 1999 C standard, identifiers may contain letters which are not
  115. part of the &ldquo;basic source character set&rdquo;, at the implementation&rsquo;s
  116. discretion (such as accented Latin letters, Greek letters, or Chinese
  117. ideograms). This may be done with an extended character set, or the
  118. &lsquo;<samp>\u</samp>&rsquo; and &lsquo;<samp>\U</samp>&rsquo; escape sequences.
  119. </p>
  120. <p>As an extension, GCC treats &lsquo;<samp>$</samp>&rsquo; as a letter. This is for
  121. compatibility with some systems, such as VMS, where &lsquo;<samp>$</samp>&rsquo; is commonly
  122. used in system-defined function and object names. &lsquo;<samp>$</samp>&rsquo; is not a
  123. letter in strictly conforming mode, or if you specify the <samp>-$</samp>
  124. option. See <a href="Invocation.html#Invocation">Invocation</a>.
  125. </p>
  126. <a name="index-numbers"></a>
  127. <a name="index-preprocessing-numbers"></a>
  128. <p>A <em>preprocessing number</em> has a rather bizarre definition. The
  129. category includes all the normal integer and floating point constants
  130. one expects of C, but also a number of other things one might not
  131. initially recognize as a number. Formally, preprocessing numbers begin
  132. with an optional period, a required decimal digit, and then continue
  133. with any sequence of letters, digits, underscores, periods, and
  134. exponents. Exponents are the two-character sequences &lsquo;<samp>e+</samp>&rsquo;,
  135. &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
  136. &lsquo;<samp>P-</samp>&rsquo;. (The exponents that begin with &lsquo;<samp>p</samp>&rsquo; or &lsquo;<samp>P</samp>&rsquo; are
  137. used for hexadecimal floating-point constants.)
  138. </p>
  139. <p>The purpose of this unusual definition is to isolate the preprocessor
  140. from the full complexity of numeric constants. It does not have to
  141. distinguish between lexically valid and invalid floating-point numbers,
  142. which is complicated. The definition also permits you to split an
  143. identifier at any position and get exactly two tokens, which can then be
  144. pasted back together with the &lsquo;<samp>##</samp>&rsquo; operator.
  145. </p>
  146. <p>It&rsquo;s possible for preprocessing numbers to cause programs to be
  147. misinterpreted. For example, <code>0xE+12</code> is a preprocessing number
  148. which does not translate to any valid numeric constant, therefore a
  149. syntax error. It does not mean <code>0xE&nbsp;+&nbsp;12<!-- /@w --></code>, which is what you
  150. might have intended.
  151. </p>
  152. <a name="index-string-literals"></a>
  153. <a name="index-string-constants"></a>
  154. <a name="index-character-constants"></a>
  155. <a name="index-header-file-names"></a>
  156. <p><em>String literals</em> are string constants, character constants, and
  157. header file names (the argument of &lsquo;<samp>#include</samp>&rsquo;).<a name="DOCF2" href="#FOOT2"><sup>2</sup></a> String constants and character
  158. constants are straightforward: <tt>&quot;&hellip;&quot;</tt> or <tt>'&hellip;'</tt>. In
  159. either case embedded quotes should be escaped with a backslash:
  160. <tt>'\''</tt> is the character constant for &lsquo;<samp>'</samp>&rsquo;. There is no limit on
  161. the length of a character constant, but the value of a character
  162. constant that contains more than one character is
  163. implementation-defined. See <a href="Implementation-Details.html#Implementation-Details">Implementation Details</a>.
  164. </p>
  165. <p>Header file names either look like string constants, <tt>&quot;&hellip;&quot;</tt>, or are
  166. written with angle brackets instead, <tt>&lt;&hellip;&gt;</tt>. In either case,
  167. backslash is an ordinary character. There is no way to escape the
  168. closing quote or angle bracket. The preprocessor looks for the header
  169. file in different places depending on which form you use. See <a href="Include-Operation.html#Include-Operation">Include Operation</a>.
  170. </p>
  171. <p>No string literal may extend past the end of a line. You may use continued
  172. lines instead, or string constant concatenation.
  173. </p>
  174. <a name="index-punctuators"></a>
  175. <a name="index-digraphs"></a>
  176. <a name="index-alternative-tokens"></a>
  177. <p><em>Punctuators</em> are all the usual bits of punctuation which are
  178. meaningful to C and C++. All but three of the punctuation characters in
  179. ASCII are C punctuators. The exceptions are &lsquo;<samp>@</samp>&rsquo;, &lsquo;<samp>$</samp>&rsquo;, and
  180. &lsquo;<samp>`</samp>&rsquo;. In addition, all the two- and three-character operators are
  181. punctuators. There are also six <em>digraphs</em>, which the C++ standard
  182. calls <em>alternative tokens</em>, which are merely alternate ways to spell
  183. other punctuators. This is a second attempt to work around missing
  184. punctuation in obsolete systems. It has no negative side effects,
  185. unlike trigraphs, but does not cover as much ground. The digraphs and
  186. their corresponding normal punctuators are:
  187. </p>
  188. <div class="smallexample">
  189. <pre class="smallexample">Digraph: &lt;% %&gt; &lt;: :&gt; %: %:%:
  190. Punctuator: { } [ ] # ##
  191. </pre></div>
  192. <a name="index-other-tokens"></a>
  193. <p>Any other single byte is considered &ldquo;other&rdquo; and passed on to the
  194. preprocessor&rsquo;s output unchanged. The C compiler will almost certainly
  195. reject source code containing &ldquo;other&rdquo; tokens. In ASCII, the only
  196. &ldquo;other&rdquo; characters are &lsquo;<samp>@</samp>&rsquo;, &lsquo;<samp>$</samp>&rsquo;, &lsquo;<samp>`</samp>&rsquo;, and control
  197. characters other than NUL (all bits zero). (Note that &lsquo;<samp>$</samp>&rsquo; is
  198. normally considered a letter.) All bytes with the high bit set
  199. (numeric range 0x7F&ndash;0xFF) that were not succesfully interpreted as
  200. part of an extended character in the input encoding are also &ldquo;other&rdquo;
  201. in the present implementation.
  202. </p>
  203. <p>NUL is a special case because of the high probability that its
  204. appearance is accidental, and because it may be invisible to the user
  205. (many terminals do not display NUL at all). Within comments, NULs are
  206. silently ignored, just as any other character would be. In running
  207. text, NUL is considered white space. For example, these two directives
  208. have the same meaning.
  209. </p>
  210. <div class="smallexample">
  211. <pre class="smallexample">#define X^@1
  212. #define X 1
  213. </pre></div>
  214. <p>(where &lsquo;<samp>^@</samp>&rsquo; is ASCII NUL). Within string or character constants,
  215. NULs are preserved. In the latter two cases the preprocessor emits a
  216. warning message.
  217. </p>
  218. <div class="footnote">
  219. <hr>
  220. <h4 class="footnotes-heading">Footnotes</h4>
  221. <h3><a name="FOOT2" href="#DOCF2">(2)</a></h3>
  222. <p>The C
  223. standard uses the term <em>string literal</em> to refer only to what we are
  224. calling <em>string constants</em>.</p>
  225. </div>
  226. <hr>
  227. <div class="header">
  228. <p>
  229. 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>
  230. </div>
  231. </body>
  232. </html>