Bump to 20230521
[platform/upstream/byacc.git] / yacc.1
1 .\" $Id: yacc.1,v 1.42 2022/11/06 17:07:16 tom Exp $
2 .\"
3 .\" .TH YACC 1 "July\ 15,\ 1990"
4 .\" .UC 6
5 .ds N Yacc
6 .ds n yacc
7 .de Ex
8 .RS +7
9 .PP
10 .nf
11 .ft CW
12 ..
13 .de Ee
14 .fi
15 .ft R
16 .RE
17 ..
18 .\" Escape single quotes in literal strings from groff's Unicode transform.
19 .ie \n(.g .ds AQ \(aq
20 .el       .ds AQ '
21 .ie \n(.g .ds `` \(lq
22 .el       .ds `` ``
23 .ie \n(.g .ds '' \(rq
24 .el       .ds '' ''
25 .\" Bulleted paragraph
26 .de bP
27 .ie n  .IP \(bu 4
28 .el    .IP \(bu 2
29 ..
30 .TH YACC 1 "November 6, 2022" "Berkeley Yacc" "User Commands"
31 .SH NAME
32 \*N \- an LALR(1) parser generator
33 .SH SYNOPSIS
34 .B \*n [ \-BdghilLPrtvVy ] [ \-b
35 .I file_prefix
36 .B ] [ \-H
37 .I defines_file
38 .B ] [ \-o
39 .I output_file
40 .B ] [ \-p
41 .I symbol_prefix
42 .B ]
43 .I filename
44 .SH DESCRIPTION
45 .B \*N
46 reads the grammar specification in the file
47 .I filename
48 and generates an LALR(1) parser for it.
49 The parsers consist of a set of LALR(1) parsing tables and a driver routine
50 written in the C programming language.
51 .B \*N
52 normally writes the parse tables and the driver routine to the file
53 .IR  y.tab.c .
54 .PP
55 The following options are available:
56 .TP 5
57 \fB\-b \fIfile_prefix\fR
58 The
59 .B \-b
60 option changes the prefix prepended to the output file names to
61 the string denoted by
62 .I file_prefix.
63 The default prefix is the character
64 .I y.
65 .TP
66 .B \-B
67 create a backtracking parser (compile-time configuration for \fBbtyacc\fP).
68 .TP
69 .B \-d
70 causes the header file
71 .B y.tab.h
72 to be written.
73 It contains #define's for the token identifiers.
74 .TP
75 .B \-h
76 print a usage message to the standard error.
77 .TP
78 \fB\-H \fIdefines_file\fR
79 causes #define's for the token identifiers
80 to be written to the given \fIdefines_file\fP rather
81 than the \fBy.tab.h\fP file used by the \fB\-d\fP option.
82 .TP
83 .B \-g
84 The
85 .B \-g
86 option causes a graphical description of the generated LALR(1) parser to
87 be written to the file
88 .B y.dot
89 in graphviz format, ready to be processed by
90 .BR dot (1).
91 .TP
92 .B \-i
93 The \fB\-i\fR option causes a supplementary header file
94 .B y.tab.i
95 to be written.
96 It contains extern declarations
97 and supplementary #define's as needed to map the conventional \fIyacc\fP
98 \fByy\fP-prefixed names to whatever the \fB\-p\fP option may specify.
99 The code file, e.g., \fBy.tab.c\fP is modified to #include this file
100 as well as the \fBy.tab.h\fP file, enforcing consistent usage of the
101 symbols defined in those files.
102 .IP
103 The supplementary header file makes it simpler to separate compilation
104 of lex- and yacc-files.
105 .TP
106 .B \-l
107 If the
108 .B \-l
109 option is not specified,
110 .B \*n
111 will insert \fI#line\fP directives in the generated code.
112 The \fI#line\fP directives let the C compiler relate errors in the
113 generated code to the user's original code.
114 If the \fB\-l\fR option is specified,
115 .B \*n
116 will not insert the \fI#line\fP directives.
117 \&\fI#line\fP directives specified by the user will be retained.
118 .TP
119 .B \-L
120 enable position processing,
121 e.g., \*(``%locations\*('' (compile-time configuration for \fBbtyacc\fP).
122 .TP
123 \fB\-o \fIoutput_file\fR
124 specify the filename for the parser file.
125 If this option is not given, the output filename is
126 the file prefix concatenated with the file suffix, e.g., \fBy.tab.c\fP.
127 This overrides the \fB\-b\fP option.
128 .TP
129 \fB\-p \fIsymbol_prefix\fR
130 The
131 .B \-p
132 option changes the prefix prepended to yacc-generated symbols to
133 the string denoted by
134 .I symbol_prefix.
135 The default prefix is the string
136 .B yy.
137 .TP
138 .B \-P
139 create a reentrant parser, e.g., \*(``%pure\-parser\*(''.
140 .TP
141 .B \-r
142 The
143 .B \-r
144 option causes
145 .B \*n
146 to produce separate files for code and tables.
147 The code file is named
148 .IR y.code.c ,
149 and the tables file is named
150 .IR y.tab.c .
151 The prefix \*(``\fIy.\fP\*('' can be overridden using the \fB\-b\fP option.
152 .TP
153 .B \-s
154 suppress \*(``\fB#define\fP\*('' statements generated for string literals in
155 a \*(``\fB%token\fP\*('' statement,
156 to more closely match original \fByacc\fP behavior.
157 .IP
158 Normally when \fB\*n\fP sees a line such as
159 .Ex
160 %token OP_ADD "ADD"
161 .Ee
162 .IP
163 it notices that the quoted \*(``ADD\*('' is a valid C identifier,
164 and generates a #define not only for OP_ADD,
165 but for ADD as well,
166 e.g.,
167 .Ex
168 #define OP_ADD 257
169 .br
170 #define ADD 258
171 .Ee
172 .IP
173 The original \fByacc\fP does not generate the second \*(``\fB#define\fP\*(''.
174 The \fB\-s\fP option suppresses this \*(``\fB#define\fP\*(''.
175 .IP
176 POSIX (IEEE 1003.1 2004) documents only names and numbers
177 for \*(``\fB%token\fP\*('',
178 though original \fByacc\fP and bison also accept string literals.
179 .TP
180 .B \-t
181 The
182 .B \-t
183 option changes the preprocessor directives generated by
184 .B \*n
185 so that debugging statements will be incorporated in the compiled code.
186 .IP
187 \fB\*N\fR sends debugging output to the standard output
188 (compatible with both the original \fByacc\fP and \fBbtyacc\fP),
189 while \fBbtyacc\fP writes debugging output to the standard error
190 (like \fBbison\fP).
191 .TP
192 .B \-v
193 The
194 .B \-v
195 option causes a human-readable description of the generated parser to
196 be written to the file
197 .I y.output.
198 .TP
199 .B \-V
200 print the version number to the standard output.
201 .TP
202 .B \-y
203 \fB\*n\fP ignores this option,
204 which bison supports for ostensible POSIX compatibility.
205 .PP
206 The \fIfilename\fP parameter is not optional.
207 However, \fB\*n\fP accepts a single \*(``\-\*('' to read the grammar
208 from the standard input.
209 A double \*(``\-\-\*('' marker denotes the end of options.
210 A single \fIfilename\fP parameter is expected after a \*(``\-\-\*('' marker.
211 .SH EXTENSIONS
212 .B \*N
213 provides some extensions for
214 compatibility with bison and other implementations of yacc.
215 It accepts several \fIlong options\fP which have equivalents in \*n.
216 The \fB%destructor\fP and \fB%locations\fP features are available
217 only if \fB\*n\fP has been configured and compiled to support the
218 back-tracking (\fBbtyacc\fP) functionality.
219 The remaining features are always available:
220 .TP
221 \fB %code\fP \fIkeyword\fP { \fIcode\fP }
222 Adds the indicated source \fIcode\fP at a given point in the output file.
223 The optional \fIkeyword\fP tells \fB\*n\fP where to insert the \fIcode\fP:
224 .RS 7
225 .TP 5
226 \fBtop\fP
227 just after the version-definition in the generated code-file.
228 .TP 5
229 \fBrequires\fP
230 just after the declaration of public parser variables.
231 If the \fB\-d\fP option is given, the code is inserted at the
232 beginning of the defines-file.
233 .TP 5
234 \fBprovides\fP
235 just after the declaration of private parser variables.
236 If the \fB\-d\fP option is given, the code is inserted at the
237 end of the defines-file.
238 .RE
239 .IP
240 If no \fIkeyword\fP is given, the code is inserted at the
241 beginning of the section of code copied verbatim from the source file.
242 Multiple \fB%code\fP directives may be given;
243 \fB\*n\fP inserts those into the corresponding code- or defines-file
244 in the order that they appear in the source file.
245 .TP
246 \fB %debug\fP
247 This has the same effect as the \*(``\-t\*('' command-line option.
248 .TP
249 \fB %destructor\fP { \fIcode\fP } \fIsymbol+\fP
250 defines code that is invoked when a symbol is automatically
251 discarded during error recovery.
252 This code can be used to
253 reclaim dynamically allocated memory associated with the corresponding
254 semantic value for cases where user actions cannot manage the memory
255 explicitly.
256 .IP
257 On encountering a parse error, the generated parser
258 discards symbols on the stack and input tokens until it reaches a state
259 that will allow parsing to continue.
260 This error recovery approach results in a memory leak
261 if the \fBYYSTYPE\fP value is, or contains,
262 pointers to dynamically allocated memory.
263 .IP
264 The bracketed \fIcode\fP is invoked whenever the parser discards one of
265 the symbols.
266 Within \fIcode\fP, \*(``\fB$$\fP\*('' or
267 \*(``\fB$<\fItag\fB>$\fR\*('' designates the semantic value associated with the
268 discarded symbol, and \*(``\fB@$\fP\*('' designates its location (see
269 \fB%locations\fP directive).
270 .IP
271 A per-symbol destructor is defined by listing a grammar symbol
272 in \fIsymbol+\fP.  A per-type destructor is defined by listing
273 a semantic type tag (e.g., \*(``<some_tag>\*('') in \fIsymbol+\fP; in this
274 case, the parser will invoke \fIcode\fP whenever it discards any grammar
275 symbol that has that semantic type tag, unless that symbol has its own
276 per-symbol destructor.
277 .IP
278 Two categories of default destructor are supported that are
279 invoked when discarding any grammar symbol that has no per-symbol and no
280 per-type destructor:
281 .RS
282 .bP
283 the code for \*(``\fB<*>\fP\*('' is used
284 for grammar symbols that have an explicitly declared semantic type tag
285 (via \*(``\fB%type\fP\*('');
286 .bP
287 the code for \*(``\fB<>\fP\*('' is used
288 for grammar symbols that have no declared semantic type tag.
289 .RE
290 .TP
291 \fB %empty\fP
292 ignored by \fB\*n\fP.
293 .TP
294 \fB %expect\fP \fInumber\fP
295 tells \fB\*n\fP the expected number of shift/reduce conflicts.
296 That makes it only report the number if it differs.
297 .TP
298 \fB %expect\-rr\fP \fInumber\fP
299 tell \fB\*n\fP the expected number of reduce/reduce conflicts.
300 That makes it only report the number if it differs.
301 This is (unlike bison) allowable in LALR parsers.
302 .TP
303 \fB %locations\fP
304 tells \fB\*n\fP to enable management of position information associated
305 with each token, provided by the lexer in the global variable \fByylloc\fP,
306 similar to management of semantic value information provided in \fByylval\fP.
307 .IP
308 As for semantic values, locations can be referenced within actions using
309 \fB@$\fP to refer to the location of the left hand side symbol, and \fB@\fIN\fR
310 (\fIN\fP an integer) to refer to the location of one of the right hand side
311 symbols.
312 Also as for semantic values, when a rule is matched, a default
313 action is used the compute the location represented by \fB@$\fP as the
314 beginning of the first symbol and the end of the last symbol in the right
315 hand side of the rule.
316 This default computation can be overridden by
317 explicit assignment to \fB@$\fP in a rule action.
318 .IP
319 The type of \fByylloc\fP is \fBYYLTYPE\fP, which is defined by default as:
320 .Ex
321 typedef struct YYLTYPE {
322     int first_line;
323     int first_column;
324     int last_line;
325     int last_column;
326 } YYLTYPE;
327 .Ee
328 .IP
329 \fBYYLTYPE\fP can be redefined by the user
330 (\fBYYLTYPE_IS_DEFINED\fP must be defined, to inhibit the default)
331 in the declarations section of the specification file.
332 As in bison, the macro \fBYYLLOC_DEFAULT\fP is invoked
333 each time a rule is matched to calculate a position for the left hand side of
334 the rule, before the associated action is executed; this macro can be
335 redefined by the user.
336 .IP
337 This directive adds a \fBYYLTYPE\fP parameter to \fByyerror()\fP.
338 If the \fB%pure\-parser\fP directive is present,
339 a \fBYYLTYPE\fP parameter is added to \fByylex()\fP calls.
340 .TP
341 \fB %lex\-param\fP { \fIargument-declaration\fP }
342 By default, the lexer accepts no parameters, e.g., \fByylex()\fP.
343 Use this directive to add parameter declarations for your customized lexer.
344 .TP
345 \fB %parse\-param\fP { \fIargument-declaration\fP }
346 By default, the parser accepts no parameters, e.g., \fByyparse()\fP.
347 Use this directive to add parameter declarations for your customized parser.
348 .TP
349 \fB %pure\-parser\fP
350 Most variables (other than \fByydebug\fP and \fByynerrs\fP) are
351 allocated on the stack within \fByyparse\fP, making the parser reasonably
352 reentrant.
353 .TP
354 \fB %token\-table\fP
355 Make the parser's names for tokens available in the \fByytname\fP array.
356 However,
357 .B \*n
358 does not predefine \*(``$end\*('', \*(``$error\*(''
359 or \*(``$undefined\*('' in this array.
360 .SH PORTABILITY
361 According to Robert Corbett,
362 .Ex
363 Berkeley Yacc is an LALR(1) parser generator.  Berkeley Yacc
364 has been made as compatible as possible with AT&T Yacc.
365 Berkeley Yacc can accept any input specification that
366 conforms to the AT&T Yacc documentation.  Specifications
367 that take advantage of undocumented features of AT&T Yacc
368 will probably be rejected.
369 .Ee
370 .PP
371 The rationale in
372 .Ex
373 http://pubs.opengroup.org/onlinepubs/9699919799/utilities/yacc.html
374 .Ee
375 .PP
376 documents some features of AT&T yacc which are no longer required for POSIX
377 compliance.
378 .PP
379 That said, you may be interested in reusing grammar files with some
380 other implementation which is not strictly compatible with AT&T yacc.
381 For instance, there is bison.
382 Here are a few differences:
383 .bP
384 \fBYacc\fP accepts an equals mark preceding the left curly brace
385 of an action (as in the original grammar file \fBftp.y\fP):
386 .Ex
387     |   STAT CRLF
388         = {
389                 statcmd();
390         }
391 .Ee
392 .bP
393 \fBYacc\fP and bison emit code in different order, and in particular bison
394 makes forward reference to common functions such as yylex, yyparse and
395 yyerror without providing prototypes.
396 .bP
397 Bison's support for \*(``%expect\*('' is broken in more than one release.
398 For best results using bison, delete that directive.
399 .bP
400 Bison has no equivalent for some of \fB\*n\fP's command-line options,
401 relying on directives embedded in the grammar file.
402 .bP
403 Bison's \*(``\fB\-y\fP\*('' option does not affect bison's lack of support for
404 features of AT&T yacc which were deemed obsolescent.
405 .bP
406 \fBYacc\fP accepts multiple parameters
407 with \fB%lex\-param\fP and \fB%parse\-param\fP in two forms
408 .Ex
409 {type1 name1} {type2 name2} ...
410 {type1 name1,  type2 name2 ...}
411 .Ee
412 .IP
413 Bison accepts the latter (though undocumented), but depending on the
414 release may generate bad code.
415 .bP
416 Like bison, \fB\*n\fP will add parameters specified via \fB%parse\-param\fP
417 to \fByyparse\fP, \fByyerror\fP and (if configured for back-tracking)
418 to the destructor declared using \fB%destructor\fP.
419 Bison puts the additional parameters \fIfirst\fP for
420 \fByyparse\fP and \fByyerror\fP but \fIlast\fP for destructors.
421 \fBYacc\fP matches this behavior.
422 .
423 .SH DIAGNOSTICS
424 If there are rules that are never reduced, the number of such rules is
425 reported on standard error.
426 If there are any LALR(1) conflicts, the number of conflicts is reported
427 on standard error.