This articleneeds additional citations forverification. Please helpimprove this article byadding citations to reliable sources. Unsourced material may be challenged and removed. Find sources: "Digraphs and trigraphs" programming – news ·newspapers ·books ·scholar ·JSTOR(September 2008) (Learn how and when to remove this message) |
Incomputer programming,digraphs and trigraphs are sequences of two and threecharacters, respectively, that appear insource code and, according to aprogramming language's specification, should be treated as if they were single characters.
Various reasons exist for using digraphs and trigraphs: keyboards may not have keys to cover the entirecharacter set of the language, input of special characters may be difficult,text editors may reserve some characters for special use and so on. Trigraphs might also be used for someEBCDICcode pages that lack characters such as{
and}
.
The basic character set of theC programming language is a subset of theASCII character set that includes nine characters which lie outside theISO 646 invariant character set. This can pose a problem for writingsource code when theencoding (and possiblykeyboard) being used does not support any of these nine characters. TheANSI C committee invented trigraphs as a way of entering source code using keyboards that support any version of the ISO 646 character set.[1]
With the widespread adoption ofASCII andUnicode/UTF-8, trigraph use is limited today, and trigraph support has been removed from C as of C23.[2]
Trigraphs are not commonly encountered outsidecompilertest suites.[3] Some compilers support an option to turn recognition of trigraphs off, or disable trigraphs by default and require an option to turn them on. Some can issue warnings when they encounter trigraphs in source files.Borland supplied a separate program, the trigraph preprocessor (TRIGRAPH.EXE
), to be used only when trigraph processing is desired (the rationale was to maximise speed of compilation).
Different systems define different sets of digraphs and trigraphs, as described below.
Early versions ofALGOL predated the standardized ASCII and EBCDIC character sets, and were typically implemented using a manufacturer-specificsix-bit character code. A number of ALGOL operations either lackedcodepoints in the available character set or were not supported by peripherals, leading to a number of substitutions including:=
for←
(assignment) and>=
for≥
(greater than or equal).
ThePascal programming language supports digraphs(.
,.)
,(*
and*)
for[
,]
,{
and}
respectively. Unlike all other cases mentioned here,(*
and*)
were and still are in wide use. However, many compilers treat them as a different type of commenting block rather than as actual digraphs, that is, a comment started with(*
cannot be closed with}
and vice versa.
TheJ programming language is a descendant ofAPL but uses the ASCII character set rather thanAPL symbols. Because the printable range of ASCII is smaller than APL's specialized set of symbols,.
(dot) and:
(colon) characters are used to inflect ASCII symbols, effectively interpreting unigraphs, digraphs or rarely trigraphs as standalone "symbols".[4]
Unlike the use of digraphs and trigraphs in C andC++, there are no single-character equivalents to these in J.
Trigraph | Equivalent |
---|---|
??= | # |
??/ | \ |
??' | ^ |
??( | [ |
??) | ] |
??! | | |
??< | { |
??> | } |
??- | ~ |
TheC preprocessor (used for C and with slight differences inC++; seebelow) replaces all occurrences of the nine trigraph sequences in this table by their single-character equivalents before any other processing (untilC23[5]).[6][7]
A programmer may want to place two question marks together yet not have the compiler treat them as introducing a trigraph. The C grammar does not permit two consecutive?
tokens, so the only places in a C file where two question marks in a row may be used are in multi-character constants,string literals, and comments. This is particularly a problem for theclassic Mac OS, where the constant'????'
may be used as a file type or creator.[8] To safely place two consecutive question marks within a string literal, the programmer can use string concatenation"...?""?..."
or anescape sequence"...?\?..."
.
???
is not itself a trigraph sequence, but when followed by a character such as-
it will be interpreted as?
+??-
, as in the example below which has 16?
s before the/
.
The??/
trigraph can be used to introduce an escaped newline for line splicing; this must be taken into account for correct and efficient handling of trigraphs within the preprocessor. It can also cause surprises, particularly within comments. For example:
// Will the next line be executed????????????????/a++;
which is a single logical comment line (used in C++ andC99), and
/??/*Acomment*??//
which is a correctly formed block comment. The concept can be used to check for trigraphs as in the following C99 example, where only one return statement will be executed.
inttrigraphsavailable()// returns 0 or 1; language standard C99 or later{// are trigraphs available??/return0;return1;}
Digraph | Equivalent |
---|---|
<: | [ |
:> | ] |
<% | { |
%> | } |
%: | # |
In 1994, a normative amendment to the C standard,C95,[9][10] included in C99, supplied digraphs as more readable alternatives to five of the trigraphs.
Unlike trigraphs, digraphs are handled duringtokenization, and any digraph must always represent a full token by itself, or compose the token%:%:
replacing the preprocessor concatenation token##
. If a digraph sequence occurs inside another token, for example a quoted string, or a character constant, it will not be replaced.
C++ (throughC++14, seebelow) behaves like C, including the C99 additions.[11]
As a note,%:%:
is treated as a single token, rather than two occurrences of%:
.
In the sequence<::
if the subsequent character is neither:
nor>
, the<
is treated as a preprocessing token by itself and not as the first character of the alternative token<:
. This is done so certain uses of templates are not broken by the substitution.
The C++ Standard makes this comment with regards to the term "digraph":[12]
The term "digraph" (token consisting of two characters) is not perfectly descriptive, since one of the alternative preprocessing-tokens is
%:%:
and of course several primary tokens contain two characters. Nonetheless, those alternative tokens that aren't lexical keywords are colloquially known as "digraphs".
Trigraphs were proposed for deprecation inC++0x, which was released asC++11.[13] This was opposed byIBM, speaking on behalf of itself and other users of C++,[14] and as a result trigraphs were retained in C++11. Trigraphs were then proposed again for removal (not only deprecation) inC++17.[15] This passed a committee vote, and trigraphs (but not the additional tokens) are removed from C++17 despite the opposition from IBM.[16] Existing code that uses trigraphs can be supported by translating from the source files (parsing trigraphs) to the basic source character set that does not include trigraphs.[15]
Hewlett-Packard calculators supporting theRPL language and input method provide support for a large number of trigraphs (also calledTIO codes) to reliably transcribe non-seven-bit ASCII characters of thecalculators' extended character set[17][18][19] on foreign platforms, and to ease keyboard input without using theCHARS application.[20][21][18][19] The first character of all TIO codes is a\
, followed by two other ASCII characters vaguely resembling the glyph to be substituted.[20][21][18][19][22] All other characters can be entered using the special\nnn
TIO code syntax with nnn being a three-digitdecimal number (withleading zeros if necessary) of the correspondingcode point (thereby formally representing atetragraph).[20][18][19]
TheVim text editor supports digraphs for actual entry of text characters, followingRFC 1345. The entry of digraphs isbound toCtrl+K by default.[23] The list of all possible digraphs inVim can be displayed by typing:dig.
GNU Screen has a digraph command, bound toCtrl+ACtrl+V by default.[24]
Lotus 1-2-3 forDOS usesAlt+F1 ascompose key to allow easier input of many special characters of theLotus International Character Set (LICS)[25] andLotus Multi-Byte Character Set (LMBCS).