Repository navigation
Expand file tree
/
Copy pathlex.tex
More file actions
1772 lines (1552 loc) · 65.6 KB
/
Copy pathlex.tex
File metadata and controls
1772 lines (1552 loc) · 65.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%!TEX root = std.tex
\rSec0[lex]{Lexical conventions}
%gram: \rSec1[gram.lex]{Lexical conventions}
%gram:
\indextext{lexical conventions|see{conventions, lexical}}
\indextext{translation!separate|see{compilation, separate}}
\indextext{separate translation|see{compilation, separate}}
\indextext{separate compilation|see{compilation, separate}}
\indextext{phases of translation|see{translation, phases}}
\indextext{source file character|see{character, source file}}
\indextext{alternative token|see{token, alternative}}
\indextext{digraph|see{token, alternative}}
\indextext{integer literal|see{literal, integer}}
\indextext{character literal|see{literal, character}}
\indextext{floating literal|see{literal, floating}}
\indextext{floating-point literal|see{literal, floating}}
\indextext{string literal|see{literal, string}}
\indextext{boolean literal|see{literal, boolean}}
\indextext{pointer literal|see{literal, pointer}}
\indextext{user-defined literal|see{literal, user defined}}
\indextext{file, source|see{source file}}
\rSec1[lex.separate]{Separate translation}
\pnum
\indextext{conventions!lexical|(}%
\indextext{compilation!separate|(}%
The text of the program is kept in units called
\indextext{source file}\term{source files} in this International
Standard. A source file together with all the headers~(\ref{headers})
and source files included~(\ref{cpp.include}) via the preprocessing
directive \tcode{\#include}, less any source lines skipped by any of the
conditional inclusion~(\ref{cpp.cond}) preprocessing directives, is
called a \defn{translation unit}.
\enternote A \Cpp program need not all be translated at the same time.
\exitnote
\pnum
\enternote Previously translated translation units and instantiation
units can be preserved individually or in libraries. The separate
translation units of a program communicate~(\ref{basic.link}) by (for
example) calls to functions whose identifiers have external linkage,
manipulation of objects whose identifiers have external linkage, or
manipulation of data files. Translation units can be separately
translated and then later linked to produce an executable
program~(\ref{basic.link}). \exitnote%
\indextext{compilation!separate|)}
\rSec1[lex.phases]{Phases of translation}%
\pnum
\indextext{translation!phases|(}
The precedence among the syntax rules of translation is specified by the
following phases.\footnote{Implementations must behave as if these separate phases
occur, although in practice different phases might be folded together.}
\begin{enumerate}
\indextext{source file}%
\indextext{character!source file}%
\indextext{character set!basic source}%
\item Physical source file characters are mapped, in an
\impldef{mapping physical source file characters to basic source character set} manner,
to the basic source character set (introducing new-line characters for end-of-line
indicators) if necessary.
The set of physical source file characters accepted is \impldef{physical source file
characters}.
Any
source file character not in the basic source character
set~(\ref{lex.charset}) is replaced by the
\indextext{universal character name}universal-character-name that
designates that character. (An implementation may use any internal
encoding, so long as an actual extended character encountered in the
source file, and the same extended character expressed in the source
file as a universal-character-name (i.e., using the \tcode{\textbackslash
uXXXX} notation), are handled equivalently
except where this replacement is reverted in a raw string literal.)
\indextext{line splicing}%
\item Each instance of a backslash character (\textbackslash)
immediately followed by a new-line character is deleted, splicing
physical source lines to form logical source lines. Only the last
backslash on any physical source line shall be eligible for being part
of such a splice.
Except for splices reverted in a raw string literal, if a splice results in
a character sequence that matches the
syntax of a universal-character-name, the behavior is
undefined. A source file that is not empty and that does not end in a new-line
character, or that ends in a new-line character immediately preceded by a
backslash character before any such splicing takes place,
shall be processed as if an additional new-line character were appended
to the file.
\item The source file is decomposed into preprocessing
tokens~(\ref{lex.pptoken}) and sequences of white-space characters
(including comments). A source file shall not end in a partial
preprocessing token or in a partial comment.\footnote{A partial preprocessing
token would arise from a source file
ending in the first portion of a multi-character token that requires a
terminating sequence of characters, such as a \grammarterm{header-name}
that is missing the closing \tcode{"}
or \tcode{>}. A partial comment
would arise from a source file ending with an unclosed \tcode{/*}
comment.}
Each comment is replaced by one space character. New-line characters are
retained. Whether each nonempty sequence of white-space characters other
than new-line is retained or replaced by one space character is
unspecified. The process of dividing a source file's
characters into preprocessing tokens is context-dependent.
\enterexample
see the handling of \tcode{<} within a \tcode{\#include} preprocessing
directive.
\exitexample
\item Preprocessing directives are executed, macro invocations are
expanded, and \tcode{_Pragma} unary operator expressions are executed.
If a character sequence that matches the syntax of a
universal-character-name is produced by token
concatenation~(\ref{cpp.concat}), the behavior is undefined. A
\tcode{\#include} preprocessing directive causes the named header or
source file to be processed from phase 1 through phase 4, recursively.
All preprocessing directives are then deleted.
\item Each source character set member in a character literal or a string
literal, as well as each escape sequence and universal-character-name in a
character literal or a non-raw string literal, is converted to the corresponding
member of the execution character set~(\ref{lex.ccon}, \ref{lex.string}); if
there is no corresponding member, it is converted to an \impldef{converting
characters from source character set to execution character set} member other
than the null (wide) character.\footnote{An implementation need not convert all
non-corresponding source characters to the same execution character.}
\item Adjacent string literal tokens are concatenated.
\item White-space characters separating tokens are no longer
significant. Each preprocessing token is converted into a
token.~(\ref{lex.token}). The resulting tokens are syntactically and
semantically analyzed and translated as a translation unit. \enternote
The process of analyzing and translating the tokens may occasionally
result in one token being replaced by a sequence of other
tokens~(\ref{temp.names}).\exitnote \enternote Source files, translation
units and translated translation units need not necessarily be stored as
files, nor need there be any one-to-one correspondence between these
entities and any external representation. The description is conceptual
only, and does not specify any particular implementation. \exitnote
\item Translated translation units and instantiation units are combined
as follows: \enternote Some or all of these may be supplied from a
library. \exitnote Each translated translation unit is examined to
produce a list of required instantiations. \enternote This may include
instantiations which have been explicitly
requested~(\ref{temp.explicit}). \exitnote The definitions of the
required templates are located. It is \impldef{whether source of translation units must
be available to locate template definitions} whether the
source of the translation units containing these definitions is required
to be available. \enternote An implementation could encode sufficient
information into the translated translation unit so as to ensure the
source is not required here. \exitnote All the required instantiations
are performed to produce
\defn{instantiation units}. \enternote These are similar
to translated translation units, but contain no references to
uninstantiated templates and no template definitions. \exitnote The
program is ill-formed if any instantiation fails.
\item All external entity references are resolved. Library
components are linked to satisfy external references to
entities not defined in the current translation. All such translator
output is collected into a program image which contains information
needed for execution in its execution environment.%
\indextext{translation!phases|)}
\end{enumerate}
\rSec1[lex.charset]{Character sets}
\pnum
\indextext{character set|(}%
\indextext{character set!basic source}%
The \term{basic source character set} consists of 96 characters: the space character,
the control characters representing horizontal tab, vertical tab, form feed, and
new-line, plus the following 91 graphical characters:\footnote{The glyphs for
the members of the basic source character set are intended to
identify characters from the subset of ISO/IEC 10646 which corresponds to the ASCII
character set. However, because the mapping from source file characters to the source
character set (described in translation phase 1) is specified as implementation-defined,
an implementation is required to document how the basic source characters are
represented in source files.}
\begin{codeblock}
a b c d e f g h i j k l m n o p q r s t u v w x y z
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
0 1 2 3 4 5 6 7 8 9
_ { } [ ] # ( ) < > % : ; . ? * + - / ^ & | ~ ! = , @\textbackslash@ " '
\end{codeblock}
\pnum
The \grammarterm{universal-character-name} construct provides a way to name
other characters.
\begin{bnf}
\nontermdef{hex-quad}\br
hexadecimal-digit hexadecimal-digit hexadecimal-digit hexadecimal-digit
\end{bnf}
\begin{bnf}
\nontermdef{universal-character-name}\br
\terminal{\textbackslash u} hex-quad\br
\terminal{\textbackslash U} hex-quad hex-quad
\end{bnf}
The character designated by the universal-character-name \tcode{\textbackslash
UNNNNNNNN} is that character whose character short name in ISO/IEC 10646 is
\tcode{NNNNNNNN}; the character designated by the universal-character-name
\tcode{\textbackslash uNNNN} is that character whose character short name in
ISO/IEC 10646 is \tcode{0000NNNN}. If the hexadecimal value for a
universal-character-name corresponds to a surrogate code point (in the
range 0xD800--0xDFFF, inclusive), the program is ill-formed. Additionally, if
the hexadecimal value for a universal-character-name outside
the \grammarterm{c-char-sequence}, \grammarterm{s-char-sequence}, or
\grammarterm{r-char-sequence} of
a character or
string literal corresponds to a control character (in either of the
ranges 0x00--0x1F or 0x7F--0x9F, both inclusive) or to a character in the basic
source character set, the program is ill-formed.\footnote{A sequence of characters resembling a universal-character-name in an
\grammarterm{r-char-sequence}~(\ref{lex.string}) does not form a
universal-character-name.}
\pnum
The \term{basic execution character set} and the \term{basic
execution wide-character set} shall each contain all the members of the
basic source character set, plus control characters representing alert,
backspace, and carriage return, plus a \term{null character}
(respectively, \term{null wide character}), whose value is 0.
For each basic execution character set, the values of the
members shall be non-negative and distinct from one another. In both the
source and execution basic character sets, the value of each character
after \tcode{0} in the above list of decimal digits shall be one greater
than the value of the previous. The \term{execution character set}
and the \term{execution wide-character set} are
implementation-defined
\indeximpldef{execution character-set and execution wide-character set}
supersets of the
basic execution character set and the basic execution wide-character
set, respectively. The values of the members of the execution character sets
and the sets of additional members
are locale-specific.%
\indextext{character set|)}
\rSec1[lex.pptoken]{Preprocessing tokens}
\begin{bnf}
\indextext{token!preprocessing|(}%
\nontermdef{preprocessing-token}\br
header-name\br
identifier\br
pp-number\br
character-literal\br
user-defined-character-literal\br
string-literal\br
user-defined-string-literal\br
preprocessing-op-or-punc\br
\textnormal{each non-white-space character that cannot be one of the above}
\end{bnf}
\pnum
Each preprocessing token that is converted to a token~(\ref{lex.token})
shall have the lexical form of a keyword, an identifier, a literal, an
operator, or a punctuator.
\pnum
A preprocessing token is the minimal lexical element of the language in translation
phases 3 through 6. The categories of preprocessing token are: header names,
identifiers, preprocessing numbers, character literals (including user-defined character
literals), string literals (including user-defined string literals), preprocessing
operators and punctuators, and single non-white-space characters that do not lexically
match the other preprocessing token categories. If a \tcode{'} or a \tcode{"} character
matches the last category, the behavior is undefined. Preprocessing tokens can be
separated by
\indextext{space!white}%
white space;
\indextext{comment}%
this consists of comments~(\ref{lex.comment}), or white-space
characters (space, horizontal tab, new-line, vertical tab, and
form-feed), or both. As described in Clause~\ref{cpp}, in certain
circumstances during translation phase 4, white space (or the absence
thereof) serves as more than preprocessing token separation. White space
can appear within a preprocessing token only as part of a header name or
between the quotation characters in a character literal or string
literal.
\pnum
If the input stream has been parsed into preprocessing tokens up to a
given character:
\begin{itemize}
\item If the next character begins a sequence of characters that could be the prefix
and initial double quote of a raw string literal, such as \tcode{R"}, the next preprocessing
token shall be a raw string literal. Between the initial and final
double quote characters of the raw string, any transformations performed in phases
1 and 2 (universal-character-names and line splicing) are reverted; this reversion
shall apply before any \grammarterm{d-char}, \grammarterm{r-char}, or delimiting
parenthesis is identified. The raw string literal is defined as the shortest sequence
of characters that matches the raw-string pattern
\begin{ncsimplebnf}
encoding-prefix\opt \terminal{R} raw-string
\end{ncsimplebnf}
\item Otherwise, if the next three characters are \tcode{<::} and the subsequent character
is neither \tcode{:} nor \tcode{>}, the \tcode{<} is treated as a preprocessor token by
itself and not as the first character of the alternative token \tcode{<:}.
\item Otherwise,
the next preprocessing token is the longest sequence of
characters that could constitute a preprocessing token, even if that
would cause further lexical analysis to fail.
\end{itemize}
\enterexample
\begin{codeblock}
#define R "x"
const char* s = R"y"; // ill-formed raw string, not \tcode{"x" "y"}
\end{codeblock}
\exitexample
\pnum
\enterexample The program fragment \tcode{0xe+foo} is parsed as a
preprocessing number token (one that is not a valid floating or integer
literal token), even though a parse as three preprocessing tokens
\tcode{0xe}, \tcode{+}, and \tcode{foo} might produce a valid expression (for example,
if \tcode{foo} were a macro defined as \tcode{1}). Similarly, the
program fragment \tcode{1E1} is parsed as a preprocessing number (one
that is a valid floating literal token), whether or not \tcode{E} is a
macro name. \exitexample
\pnum
\enterexample The program fragment \tcode{x+++++y} is parsed as \tcode{x
++ ++ + y}, which, if \tcode{x} and \tcode{y} have integral types,
violates a constraint on increment operators, even though the parse
\tcode{x ++ + ++ y} might yield a correct expression. \exitexample%
\indextext{token!preprocessing|)}
\rSec1[lex.digraph]{Alternative tokens}
\pnum
\indextext{token!alternative|(}%
Alternative token representations are provided for some operators and
punctuators.\footnote{\indextext{digraph}%
These include ``digraphs'' and additional reserved words. The term
``digraph'' (token consisting of two characters) is not perfectly
descriptive, since one of the alternative preprocessing-tokens is
\tcode{\%:\%:} and of course several primary tokens contain two
characters. Nonetheless, those alternative tokens that aren't lexical
keywords are colloquially known as ``digraphs''. }
\pnum
In all respects of the language, each alternative token behaves the
same, respectively, as its primary token, except for its spelling.\footnote{Thus the ``stringized'' values~(\ref{cpp.stringize}) of
\tcode{[} and \tcode{<:} will be different, maintaining the source
spelling, but the tokens can otherwise be freely interchanged. }
The set of alternative tokens is defined in
Table~\ref{tab:alternative.tokens}.
\begin{tokentable}{Alternative tokens}{tab:alternative.tokens}{Alternative}{Primary}
\tcode{<\%} & \tcode{\{} &
\tcode{and} & \tcode{\&\&} &
\tcode{and_eq} & \tcode{\&=} \\ \rowsep
\tcode{\%>} & \tcode{\}} &
\tcode{bitor} & \tcode{|} &
\tcode{or_eq} & \tcode{|=} \\ \rowsep
\tcode{<:} & \tcode{[} &
\tcode{or} & \tcode{||} &
\tcode{xor_eq} & \tcode{\^{}=} \\ \rowsep
\tcode{:>} & \tcode{]} &
\tcode{xor} & \tcode{\^{}} &
\tcode{not} & \tcode{!} \\ \rowsep
\tcode{\%:} & \tcode{\#} &
\tcode{compl} & \tcode{\~} &
\tcode{not_eq} & \tcode{!=} \\ \rowsep
\tcode{\%:\%:} & \tcode{\#\#} &
\tcode{bitand} & \tcode{\&} &
& \\
\end{tokentable}%
\indextext{token!alternative|)}
\rSec1[lex.token]{Tokens}
\indextext{token|(}%
\begin{bnf}
\nontermdef{token}\br
identifier\br
keyword\br
literal\br
operator\br
punctuator
\end{bnf}
\pnum
\indextext{\idxgram{token}}%
There are five kinds of tokens: identifiers, keywords, literals,\footnote{Literals include strings and character and numeric literals.
}
operators, and other separators.
\indextext{white~space}%
Blanks, horizontal and vertical tabs, newlines, formfeeds, and comments
(collectively, ``white space''), as described below, are ignored except
as they serve to separate tokens. \enternote Some white space is
required to separate otherwise adjacent identifiers, keywords, numeric
literals, and alternative tokens containing alphabetic characters.
\exitnote%
\indextext{token|)}
\rSec1[lex.comment]{Comments}
\pnum
\indextext{comment|(}%
\indextext{comment!\tcode{/*}~\tcode{*/}}%
\indextext{comment!\tcode{//}}%
The characters \tcode{/*} start a comment, which terminates with the
characters \tcode{*/}. These comments do not nest.
\indextext{comment!\tcode{//}}%
The characters \tcode{//} start a comment, which terminates immediately before the
next new-line character. If there is a form-feed or a vertical-tab
character in such a comment, only white-space characters shall appear
between it and the new-line that terminates the comment; no diagnostic
is required. \enternote The comment characters \tcode{//}, \tcode{/*},
and \tcode{*/} have no special meaning within a \tcode{//} comment and
are treated just like other characters. Similarly, the comment
characters \tcode{//} and \tcode{/*} have no special meaning within a
\tcode{/*} comment. \exitnote%
\indextext{comment|)}
\rSec1[lex.header]{Header names}
\indextext{header!name|(}%
\begin{bnf}
\nontermdef{header-name}\br
\terminal{<} h-char-sequence \terminal{>}\br
\terminal{"} q-char-sequence \terminal{"}
\end{bnf}
\begin{bnf}
\nontermdef{h-char-sequence}\br
h-char\br
h-char-sequence h-char
\end{bnf}
\begin{bnf}
\nontermdef{h-char}\br
\textnormal{any member of the source character set except new-line and \terminal{>}}
\end{bnf}
\begin{bnf}
\nontermdef{q-char-sequence}\br
q-char\br
q-char-sequence q-char
\end{bnf}
\begin{bnf}
\nontermdef{q-char}\br
\textnormal{any member of the source character set except new-line and \terminal{"}}
\end{bnf}
\pnum
Header name preprocessing tokens shall only appear within a
\tcode{\#include} preprocessing directive~(\ref{cpp.include}). The
sequences in both forms of \grammarterm{header-name}{s} are mapped in an
\impldef{mapping header name to header or external source file} manner to headers or to
external source file names as specified in~\ref{cpp.include}.
\pnum
The appearance of either of the characters \tcode{'} or \tcode{\textbackslash} or of
either of the character sequences \tcode{/*} or \tcode{//} in a
\grammarterm{q-char-sequence} or an \grammarterm{h-char-sequence}
is conditionally-supported with implementation-defined semantics,
as is the appearance of the
character \tcode{"} in an \grammarterm{h-char-sequence}.\footnote{Thus, a
sequence of characters that resembles an escape sequence might result in an
error, be interpreted as the character corresponding to the escape sequence, or
have a completely different meaning, depending on the implementation.}%
\indextext{header!name|)}
\rSec1[lex.ppnumber]{Preprocessing numbers}
\index{number!preprocessing|(}%
\begin{bnf}
\nontermdef{pp-number}\br
digit\br
\terminal{.} digit\br
pp-number digit\br
pp-number identifier-nondigit\br
pp-number \terminal{'} digit\br
pp-number \terminal{'} nondigit\br
pp-number \terminal{e} sign\br
pp-number \terminal{E} sign\br
pp-number \terminal{.}
\end{bnf}
\pnum
Preprocessing number tokens lexically include all integer literal
tokens~(\ref{lex.icon}) and all floating literal
tokens~(\ref{lex.fcon}).
\pnum
A preprocessing number does not have a type or a value; it acquires both
after a successful conversion to an integer literal token or a floating literal
token.%
\index{number!preprocessing|)}
\rSec1[lex.name]{Identifiers}
\indextext{identifier|(}%
\begin{bnf}
\nontermdef{identifier}\br
identifier-nondigit\br
identifier identifier-nondigit\br
identifier digit
\end{bnf}
\begin{bnf}
\nontermdef{identifier-nondigit}\br
nondigit\br
universal-character-name\br
\textnormal{other implementation-defined characters}
\end{bnf}
\begin{bnf}
\nontermdef{nondigit} \textnormal{one of}\br
\terminal{a b c d e f g h i j k l m}\br
\terminal{n o p q r s t u v w x y z}\br
\terminal{A B C D E F G H I J K L M}\br
\terminal{N O P Q R S T U V W X Y Z _}
\end{bnf}
\begin{bnf}
\nontermdef{digit} \textnormal{one of}\br
\terminal{0 1 2 3 4 5 6 7 8 9}
\end{bnf}
\pnum
\indextext{name!length~of}%
\indextext{name}%
An identifier is an arbitrarily long sequence of letters and digits.
Each universal-character-name in an identifier shall designate a
character whose encoding in ISO 10646 falls into one of the ranges
specified in~\ref{charname.allowed}.
The initial element shall not be a universal-character-name
designating a character whose encoding falls into one of the ranges
specified in~\ref{charname.disallowed}.
Upper- and lower-case letters are
different. All characters are significant.\footnote{On systems in which linkers cannot accept extended
characters, an encoding of the universal-character-name may be used in
forming valid external identifiers. For example, some otherwise unused
character or sequence of characters may be used to encode the
\tcode{\textbackslash u} in a universal-character-name. Extended
characters may produce a long external identifier, but \Cpp does not
place a translation limit on significant characters for external
identifiers. In \Cpp, upper- and lower-case letters are considered
different for all identifiers, including external identifiers. }
\pnum
The identifiers in Table~\ref{tab:identifiers.special} have a special meaning when
appearing in a certain context. When referred to in the grammar, these identifiers
are used explicitly rather than using the \grammarterm{identifier} grammar production.
Unless otherwise specified, any ambiguity as to whether a given
\grammarterm{identifier} has a special meaning is resolved to interpret the
token as a regular \grammarterm{identifier}.
\begin{floattable}{Identifiers with special meaning}{tab:identifiers.special}
{ll}
\topline
\tcode{override} &
\tcode{final} \\
\end{floattable}
\pnum
\indextext{\idxcode{_}|see{character, underscore}}%
\indextext{character!underscore!in identifier}%
\indextext{reserved~identifier}%
In addition, some identifiers are reserved for use by \Cpp
implementations and shall
not be used otherwise; no diagnostic is required.%
\indextext{identifier|)}
\begin{itemize}
\item
Each identifier that contains a double underscore
\tcode{\unun}
\indextext{character!underscore}%
or begins with an underscore followed by
an uppercase letter
\indextext{uppercase}%
is reserved to the implementation for any use.
\item
Each identifier that begins with an underscore is
\indextext{character!underscore}%
reserved to the implementation for use as a name in the global namespace.%
\indextext{namespace!global}
\end{itemize}
\rSec1[lex.key]{Keywords}
\enlargethispage{\baselineskip}%
\pnum
\indextext{keyword|(}%
The identifiers shown in Table~\ref{tab:keywords} are reserved for use
as keywords (that is, they are unconditionally treated as keywords in
phase 7) except in an \grammarterm{attribute-token}~(\ref{dcl.attr.grammar})
\enternote The \tcode{export} keyword is unused but is reserved for future use.\exitnote:
\begin{floattable}{Keywords}{tab:keywords}
{lllll}
\topline
\tcode{alignas} &
\tcode{continue} &
\tcode{friend} &
\tcode{register} &
\tcode{true} \\
\tcode{alignof} &
\tcode{decltype} &
\tcode{goto} &
\tcode{reinterpret_cast} &
\tcode{try} \\
\tcode{asm} &
\tcode{default} &
\tcode{if} &
\tcode{return} &
\tcode{typedef} \\
\tcode{auto} &
\tcode{delete} &
\tcode{inline} &
\tcode{short} &
\tcode{typeid} \\
\tcode{bool} &
\tcode{do} &
\tcode{int} &
\tcode{signed} &
\tcode{typename} \\
\tcode{break} &
\tcode{double} &
\tcode{long} &
\tcode{sizeof} &
\tcode{union} \\
\tcode{case} &
\tcode{dynamic_cast} &
\tcode{mutable} &
\tcode{static} &
\tcode{unsigned} \\
\tcode{catch} &
\tcode{else} &
\tcode{namespace} &
\tcode{static_assert} &
\tcode{using} \\
\tcode{char} &
\tcode{enum} &
\tcode{new} &
\tcode{static_cast} &
\tcode{virtual} \\
\tcode{char16_t} &
\tcode{explicit} &
\tcode{noexcept} &
\tcode{struct} &
\tcode{void} \\
\tcode{char32_t} &
\tcode{export} &
\tcode{nullptr} &
\tcode{switch} &
\tcode{volatile} \\
\tcode{class} &
\tcode{extern} &
\tcode{operator} &
\tcode{template} &
\tcode{wchar_t} \\
\tcode{const} &
\tcode{false} &
\tcode{private} &
\tcode{this} &
\tcode{while} \\
\tcode{constexpr} &
\tcode{float} &
\tcode{protected} &
\tcode{thread_local} & \\
\tcode{const_cast} &
\tcode{for} &
\tcode{public} &
\tcode{throw} & \\
\end{floattable}
\pnum
Furthermore, the alternative representations shown in
Table~\ref{tab:alternative.representations} for certain operators and
punctuators~(\ref{lex.digraph}) are reserved and shall not be used
otherwise:
\begin{floattable}{Alternative representations}{tab:alternative.representations}
{llllll}
\topline
\tcode{and} & \tcode{and_eq} & \tcode{bitand} & \tcode{bitor} & \tcode{compl} & \tcode{not} \\
\tcode{not_eq} & \tcode{or} & \tcode{or_eq} & \tcode{xor} & \tcode{xor_eq} & \\
\end{floattable}%
\indextext{keyword|)}%
\rSec1[lex.operators]{Operators and punctuators}
\pnum
\indextext{operator|(}%
\indextext{punctuator|(}%
The lexical representation of \Cpp programs includes a number of
preprocessing tokens which are used in the syntax of the preprocessor or
are converted into tokens for operators and punctuators:
\begin{bnfkeywordtab}
\nontermdef{preprocessing-op-or-punc} \textnormal{one of}\br
\>\{ \>\} \>[ \>] \>\# \>\#\# \>( \>)\br
\><: \>:> \><\% \>\%> \>\%: \>\%:\%: \>; \>: \>.{..}\br
\>new \>delete \>? \>:: \>. \>.*\br
\>+ \>- \>* \>/ \>\% \>\^{} \>\& \>| \>\tilde\br
\>! \>= \>< \>> \>+= \>-= \>*= \>/= \>\%=\br
\>\^{}= \>\&= \>|= \>\shl \>\shr \>\shr= \>\shl= \>== \>!=\br
\><= \>>= \>\&\& \>|| \>++ \>-{-} \>, \>->* \>->\br
\>and \>and_eq \>bitand \>bitor \>compl \>not \>not_eq\br
\>or \>or_eq \>xor \>xor_eq
\end{bnfkeywordtab}
Each \grammarterm{preprocessing-op-or-punc} is converted to a single token
in translation phase 7~(\ref{lex.phases}).%
\indextext{punctuator|)}%
\indextext{operator|)}
\rSec1[lex.literal]{Literals}%
\indextext{literal|(}
\rSec2[lex.literal.kinds]{Kinds of literals}
\pnum
\indextext{constant}%
\indextext{literal!constant}%
There are several kinds of literals.\footnote{The term ``literal'' generally designates, in this
International Standard, those tokens that are called ``constants'' in
ISO C. }
\begin{bnf}
\nontermdef{literal}\br
integer-literal\br
character-literal\br
floating-literal\br
string-literal\br
boolean-literal\br
pointer-literal\br
user-defined-literal
\end{bnf}
\rSec2[lex.icon]{Integer literals}
\indextext{literal!integer}%
\begin{bnf}
\nontermdef{integer-literal}\br
binary-literal integer-suffix\opt\br
octal-literal integer-suffix\opt\br
decimal-literal integer-suffix\opt\br
hexadecimal-literal integer-suffix\opt
\end{bnf}
\begin{bnf}
\nontermdef{binary-literal}\br
\terminal{0b} binary-digit\br
\terminal{0B} binary-digit\br
binary-literal \terminal{'}\opt binary-digit
\end{bnf}
\begin{bnf}
\nontermdef{octal-literal}\br
\terminal{0}\br
octal-literal \terminal{'}\opt octal-digit
\end{bnf}
\begin{bnf}
\nontermdef{decimal-literal}\br
nonzero-digit\br
decimal-literal \terminal{'}\opt digit
\end{bnf}
\begin{bnf}
\nontermdef{hexadecimal-literal}\br
\terminal{0x} hexadecimal-digit\br
\terminal{0X} hexadecimal-digit\br
hexadecimal-literal \terminal{'}\opt hexadecimal-digit
\end{bnf}
\begin{bnf}
\nontermdef{binary-digit}\br
\terminal{0}\br
\terminal{1}
\end{bnf}
\begin{bnf}
\nontermdef{octal-digit} \textnormal{one of}\br
\terminal{0 1 2 3 4 5 6 7}
\end{bnf}
\begin{bnf}
\nontermdef{nonzero-digit} \textnormal{one of}\br
\terminal{1 2 3 4 5 6 7 8 9}
\end{bnf}
\begin{bnf}
\nontermdef{hexadecimal-digit} \textnormal{one of}\br
\terminal{0 1 2 3 4 5 6 7 8 9}\br
\terminal{a b c d e f}\br
\terminal{A B C D E F}
\end{bnf}
\begin{bnf}
\nontermdef{integer-suffix}\br
unsigned-suffix long-suffix\opt \br
unsigned-suffix long-long-suffix\opt \br
long-suffix unsigned-suffix\opt \br
long-long-suffix unsigned-suffix\opt
\end{bnf}
\begin{bnf}
\nontermdef{unsigned-suffix} \textnormal{one of}\br
\terminal{u U}
\end{bnf}
\begin{bnf}
\nontermdef{long-suffix} \textnormal{one of}\br
\terminal{l L}
\end{bnf}
\begin{bnf}
\nontermdef{long-long-suffix} \textnormal{one of}\br
\terminal{ll LL}
\end{bnf}
\pnum
\indextext{literal!\idxcode{unsigned}}%
\indextext{literal!\idxcode{long}}%
\indextext{literal!integer}%
\indextext{literal!binary}%
\indextext{literal!octal}%
\indextext{literal!decimal}%
\indextext{literal!hexadecimal}%
\indextext{literal!base~of integer}%
An \term{integer literal} is a sequence of digits that has no period
or exponent part, with optional separating single quotes that are ignored
when determining its value. An integer literal may have a prefix that specifies
its base and a suffix that specifies its type. The lexically first digit
of the sequence of digits is the most significant.
A \term{binary} integer literal (base two) begins with
\tcode{0b} or \tcode{0B} and consists of a sequence of binary digits.
An \term{octal} integer
literal (base eight) begins with the digit \tcode{0} and consists of a
sequence of octal digits.\footnote{The digits \tcode{8} and \tcode{9} are not octal digits. }
A \term{decimal}
integer literal (base ten) begins with a digit other than \tcode{0} and
consists of a sequence of decimal digits.
A \term{hexadecimal} integer literal (base sixteen) begins with
\tcode{0x} or \tcode{0X} and consists of a sequence of hexadecimal
digits, which include the decimal digits and the letters \tcode{a}
through \tcode{f} and \tcode{A} through \tcode{F} with decimal values
ten through fifteen.
\enterexample The number twelve can be written \tcode{12}, \tcode{014},
\tcode{0XC}, or \tcode{0b1100}. The literals \tcode{1048576},
\tcode{1'048'576}, \tcode{0X100000}, \tcode{0x10'0000}, and
\tcode{0'004'000'000} all have the same value.
\exitexample
\pnum
\indextext{literal!\idxcode{long}}%
\indextext{literal!\idxcode{unsigned}}%
\indextext{suffix!\idxcode{L}}%
\indextext{suffix!\idxcode{U}}%
\indextext{suffix!\idxcode{l}}%
\indextext{suffix!\idxcode{u}}%
\indextext{literal!type~of integer}%
The type of an integer literal is the first of the corresponding list
in Table~\ref{tab:lex.type.integer.literal} in which its value can be
represented.
\enlargethispage{\baselineskip}%
\begin{LongTable}{Types of integer literals}{tab:lex.type.integer.literal}{l|l|l}
\\ \topline
\lhdr{Suffix} & \chdr{Decimal literal} & \rhdr{Binary, octal, or hexadecimal literal} \\ \capsep
\endfirsthead
\continuedcaption\\
\hline
\lhdr{Suffix} & \chdr{Decimal literal} & \rhdr{Binary, octal, or hexadecimal literal} \\ \capsep
\endhead
none &
\tcode{int} &
\tcode{int}\\
&
\tcode{long int} &
\tcode{unsigned int}\\
&
\tcode{long long int} &
\tcode{long int}\\
&
&
\tcode{unsigned long int}\\
&
&
\tcode{long long int}\\
&
&
\tcode{unsigned long long int}\\\hline
\tcode{u} or \tcode{U} &
\tcode{unsigned int} &
\tcode{unsigned int}\\
&
\tcode{unsigned long int} &
\tcode{unsigned long int}\\
&
\tcode{unsigned long long int} &
\tcode{unsigned long long int}\\\hline
\tcode{l} or \tcode{L} &
\tcode{long int} &
\tcode{long int}\\
&
\tcode{long long int} &
\tcode{unsigned long int}\\
&
&
\tcode{long long int}\\
&
&
\tcode{unsigned long long int}\\\hline
Both \tcode{u} or \tcode{U} &
\tcode{unsigned long int} &
\tcode{unsigned long int}\\
and \tcode{l} or \tcode{L} &
\tcode{unsigned long long int} &
\tcode{unsigned long long int}\\\hline
\tcode{ll} or \tcode{LL} &
\tcode{long long int} &
\tcode{long long int}\\
&
&
\tcode{unsigned long long int}\\\hline
Both \tcode{u} or \tcode{U} &
\tcode{unsigned long long int} &
\tcode{unsigned long long int}\\
and \tcode{ll} or \tcode{LL} &
&
\\
\end{LongTable}
\pnum
If an integer literal cannot be represented by any type in its list and
an extended integer type~(\ref{basic.fundamental}) can represent its value, it may have that
extended integer type. If all of the types in the list for the literal
are signed, the extended integer type shall be signed. If all of the
types in the list for the literal are unsigned, the extended integer
type shall be unsigned. If the list contains both signed and unsigned
types, the extended integer type may be signed or unsigned. A program is
ill-formed if one of its translation units contains an integer literal
that cannot be represented by any of the allowed types.
\rSec2[lex.ccon]{Character literals}
\indextext{literal!character}%
\begin{bnf}
\nontermdef{character-literal}\br
encoding-prefix\opt \terminal{'} c-char-sequence \terminal{'}
\end{bnf}
\begin{bnf}
\nontermdef{encoding-prefix} \textnormal{one of}\br
\terminal{u8}\quad\terminal{u}\quad\terminal{U}\quad\terminal{L}
\end{bnf}
\begin{bnf}
\nontermdef{c-char-sequence}\br
c-char\br
c-char-sequence c-char
\end{bnf}
\begin{bnftab}
\nontermdef{c-char}\br
\>\textnormal{any member of the source character set except}\br