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