]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - share/doc/psd/15.yacc/ss3
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / share / doc / psd / 15.yacc / ss3
1 .\" Copyright (C) Caldera International Inc. 2001-2002.  All rights reserved.
2 .\" 
3 .\" Redistribution and use in source and binary forms, with or without
4 .\" modification, are permitted provided that the following conditions are
5 .\" met:
6 .\" 
7 .\" Redistributions of source code and documentation must retain the above
8 .\" copyright notice, this list of conditions and the following
9 .\" disclaimer.
10 .\" 
11 .\" Redistributions in binary form must reproduce the above copyright
12 .\" notice, this list of conditions and the following disclaimer in the
13 .\" documentation and/or other materials provided with the distribution.
14 .\" 
15 .\" All advertising materials mentioning features or use of this software
16 .\" must display the following acknowledgement:
17 .\" 
18 .\" This product includes software developed or owned by Caldera
19 .\" International, Inc.  Neither the name of Caldera International, Inc.
20 .\" nor the names of other contributors may be used to endorse or promote
21 .\" products derived from this software without specific prior written
22 .\" permission.
23 .\" 
24 .\" USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
25 .\" INTERNATIONAL, INC.  AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
26 .\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
27 .\" WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 .\" DISCLAIMED.  IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE
29 .\" FOR ANY DIRECT, INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 .\" BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
33 .\" WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
34 .\" OR OTHERWISE) RISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
35 .\" IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 .\"
37 .\"     @(#)ss3 8.1 (Berkeley) 6/8/93
38 .\"
39 .\" $FreeBSD$
40 .SH
41 3: Lexical Analysis
42 .PP
43 The user must supply a lexical analyzer to read the input stream and communicate tokens
44 (with values, if desired) to the parser.
45 The lexical analyzer is an integer-valued function called
46 .I yylex .
47 The function returns an integer, the
48 .I "token number" ,
49 representing the kind of token read.
50 If there is a value associated with that token, it should be assigned
51 to the external variable
52 .I yylval .
53 .PP
54 The parser and the lexical analyzer must agree on these token numbers in order for
55 communication between them to take place.
56 The numbers may be chosen by Yacc, or chosen by the user.
57 In either case, the ``# define'' mechanism of C is used to allow the lexical analyzer
58 to return these numbers symbolically.
59 For example, suppose that the token name DIGIT has been defined in the declarations section of the
60 Yacc specification file.
61 The relevant portion of the lexical analyzer might look like:
62 .DS
63 yylex(){
64         extern int yylval;
65         int c;
66         . . .
67         c = getchar();
68         . . .
69         switch( c ) {
70                 . . .
71         case \'0\':
72         case \'1\':
73           . . .
74         case \'9\':
75                 yylval = c\-\'0\';
76                 return( DIGIT );
77                 . . .
78                 }
79         . . .
80 .DE
81 .PP
82 The intent is to return a token number of DIGIT, and a value equal to the numerical value of the
83 digit.
84 Provided that the lexical analyzer code is placed in the programs section of the specification file,
85 the identifier DIGIT will be defined as the token number associated
86 with the token DIGIT.
87 .PP
88 This mechanism leads to clear,
89 easily modified lexical analyzers; the only pitfall is the need
90 to avoid using any token names in the grammar that are reserved
91 or significant in C or the parser; for example, the use of
92 token names
93 .I if
94 or
95 .I while
96 will almost certainly cause severe
97 difficulties when the lexical analyzer is compiled.
98 The token name
99 .I error
100 is reserved for error handling, and should not be used naively
101 (see Section 7).
102 .PP
103 As mentioned above, the token numbers may be chosen by Yacc or by the user.
104 In the default situation, the numbers are chosen by Yacc.
105 The default token number for a literal
106 character is the numerical value of the character in the local character set.
107 Other names are assigned token numbers
108 starting at 257.
109 .PP
110 To assign a token number to a token (including literals),
111 the first appearance of the token name or literal
112 .I
113 in the declarations section
114 .R
115 can be immediately followed by
116 a nonnegative integer.
117 This integer is taken to be the token number of the name or literal.
118 Names and literals not defined by this mechanism retain their default definition.
119 It is important that all token numbers be distinct.
120 .PP
121 For historical reasons, the endmarker must have token
122 number 0 or negative.
123 This token number cannot be redefined by the user; thus, all
124 lexical analyzers should be prepared to return 0 or negative as a token number
125 upon reaching the end of their input.
126 .PP
127 A very useful tool for constructing lexical analyzers is
128 the
129 .I Lex
130 program developed by Mike Lesk.
131 .[
132 Lesk Lex
133 .]
134 These lexical analyzers are designed to work in close
135 harmony with Yacc parsers.
136 The specifications for these lexical analyzers
137 use regular expressions instead of grammar rules.
138 Lex can be easily used to produce quite complicated lexical analyzers,
139 but there remain some languages (such as FORTRAN) which do not
140 fit any theoretical framework, and whose lexical analyzers
141 must be crafted by hand.