1YACC(1)                          User Commands                         YACC(1)
2
3
4

NAME

6       Yacc - an LALR(1) parser generator
7

SYNOPSIS

9       yacc  [  -BdghilLPrtvVy  ]  [ -b file_prefix ] [ -H defines_file ] [ -o
10       output_file ] [ -p symbol_prefix ] filename
11

DESCRIPTION

13       Yacc reads the grammar specification in the file filename and generates
14       an  LALR(1)  parser  for  it.   The parsers consist of a set of LALR(1)
15       parsing tables and a driver routine written in the C  programming  lan‐
16       guage.  Yacc normally writes the parse tables and the driver routine to
17       the file y.tab.c.
18
19       The following options are available:
20
21       -b file_prefix
22            The -b option changes the prefix  prepended  to  the  output  file
23            names to the string denoted by file_prefix.  The default prefix is
24            the character y.
25
26       -B   create a backtracking parser (compile-time configuration for  bty‐
27            acc).
28
29       -d   causes  the  header  file y.tab.h to be written.  It contains #de‐
30            fine's for the token identifiers.
31
32       -h   print a usage message to the standard error.
33
34       -H defines_file
35            causes #define's for the token identifiers to be  written  to  the
36            given defines_file rather than the y.tab.h file used by the -d op‐
37            tion.
38
39       -g   The -g option causes a  graphical  description  of  the  generated
40            LALR(1) parser to be written to the file y.dot in graphviz format,
41            ready to be processed by dot(1).
42
43       -i   The -i option causes a supplementary header  file  y.tab.i  to  be
44            written.   It  contains extern declarations and supplementary #de‐
45            fine's as needed to map the conventional yacc yy-prefixed names to
46            whatever  the -p option may specify.  The code file, e.g., y.tab.c
47            is modified to #include this file as well as the y.tab.h file, en‐
48            forcing consistent usage of the symbols defined in those files.
49
50            The  supplementary header file makes it simpler to separate compi‐
51            lation of lex- and yacc-files.
52
53       -l   If the -l option is not specified, yacc will insert  #line  direc‐
54            tives  in the generated code.  The #line directives let the C com‐
55            piler relate errors in the generated code to the  user's  original
56            code.   If  the  -l  option is specified, yacc will not insert the
57            #line directives.  #line directives specified by the user will  be
58            retained.
59
60       -L   enable  position processing, e.g., “%locations” (compile-time con‐
61            figuration for btyacc).
62
63       -o output_file
64            specify the filename for the parser file.  If this option  is  not
65            given,  the  output  filename is the file prefix concatenated with
66            the file suffix, e.g., y.tab.c.  This overrides the -b option.
67
68       -p symbol_prefix
69            The -p option changes the prefix prepended to yacc-generated  sym‐
70            bols  to  the string denoted by symbol_prefix.  The default prefix
71            is the string yy.
72
73       -P   create a reentrant parser, e.g., “%pure-parser”.
74
75       -r   The -r option causes yacc to produce separate files for  code  and
76            tables.   The  code file is named y.code.c, and the tables file is
77            named y.tab.c.  The prefix “y.” can be overridden using the -b op‐
78            tion.
79
80       -s   suppress  “#define”  statements generated for string literals in a
81%token” statement, to more closely match original yacc behavior.
82
83            Normally when yacc sees a line such as
84
85              %token OP_ADD "ADD"
86
87            it notices that the quoted “ADD” is a valid C identifier, and gen‐
88            erates a #define not only for OP_ADD, but for ADD as well, e.g.,
89
90              #define OP_ADD 257
91              #define ADD 258
92
93            The  original yacc does not generate the second “#define”.  The -s
94            option suppresses this “#define”.
95
96            POSIX (IEEE 1003.1 2004) documents  only  names  and  numbers  for
97%token”, though original yacc and bison also accept string liter‐
98            als.
99
100       -t   The -t option changes the  preprocessor  directives  generated  by
101            yacc so that debugging statements will be incorporated in the com‐
102            piled code.
103
104            Yacc sends debugging output to  the  standard  output  (compatible
105            with  both  the original yacc and btyacc), while btyacc writes de‐
106            bugging output to the standard error (like bison).
107
108       -v   The -v option causes a human-readable description of the generated
109            parser to be written to the file y.output.
110
111       -V   print the version number to the standard output.
112
113       -y   yacc  ignores  this  option,  which  bison supports for ostensible
114            POSIX compatibility.
115
116       The filename parameter is not optional.  However, yacc accepts a single
117       “-”  to read the grammar from the standard input.  A double “--” marker
118       denotes the end of options.  A single filename  parameter  is  expected
119       after a “--” marker.
120

EXTENSIONS

122       Yacc  provides  some  extensions for compatibility with bison and other
123       implementations of yacc.  It accepts several long  options  which  have
124       equivalents  in  yacc.   The  %destructor  and  %locations features are
125       available only if yacc has been configured and compiled to support  the
126       back-tracking  (btyacc)  functionality.  The remaining features are al‐
127       ways available:
128
129        %code keyword { code }
130              Adds the indicated source code at a given point  in  the  output
131              file.  The optional keyword tells yacc where to insert the code:
132
133              top  just  after  the  version-definition in the generated code-
134                   file.
135
136              requires
137                   just after the declaration of public parser variables.   If
138                   the  -d option is given, the code is inserted at the begin‐
139                   ning of the defines-file.
140
141              provides
142                   just after the declaration of private parser variables.  If
143                   the  -d option is given, the code is inserted at the end of
144                   the defines-file.
145
146              If no keyword is given, the code is inserted at the beginning of
147              the  section of code copied verbatim from the source file.  Mul‐
148              tiple %code directives may be given; yacc inserts those into the
149              corresponding  code-  or defines-file in the order that they ap‐
150              pear in the source file.
151
152        %debug
153              This has the same effect as the “-t” command-line option.
154
155        %destructor { code } symbol+
156              defines code that is invoked when a symbol is automatically dis‐
157              carded  during error recovery.  This code can be used to reclaim
158              dynamically allocated memory associated with  the  corresponding
159              semantic  value  for  cases where user actions cannot manage the
160              memory explicitly.
161
162              On encountering a parse error,  the  generated  parser  discards
163              symbols  on  the stack and input tokens until it reaches a state
164              that will allow parsing to continue.  This  error  recovery  ap‐
165              proach results in a memory leak if the YYSTYPE value is, or con‐
166              tains, pointers to dynamically allocated memory.
167
168              The bracketed code is invoked whenever the parser  discards  one
169              of  the  symbols.  Within code, “$$” or “$<tag>$” designates the
170              semantic value associated with the discarded  symbol,  and  “@$
171              designates its location (see %locations directive).
172
173              A  per-symbol  destructor is defined by listing a grammar symbol
174              in symbol+.  A per-type destructor is defined by listing  a  se‐
175              mantic  type  tag (e.g., “<some_tag>”) in symbol+; in this case,
176              the parser will invoke code whenever  it  discards  any  grammar
177              symbol  that  has that semantic type tag, unless that symbol has
178              its own per-symbol destructor.
179
180              Two categories of default destructor are supported that are  in‐
181              voked  when discarding any grammar symbol that has no per-symbol
182              and no per-type destructor:
183
184              •   the code for “<*>” is used for grammar symbols that have  an
185                  explicitly declared semantic type tag (via “%type”);
186
187              •   the  code  for “<>” is used for grammar symbols that have no
188                  declared semantic type tag.
189
190        %empty
191              ignored by yacc.
192
193        %expect number
194              tells yacc the expected number of shift/reduce conflicts.   That
195              makes it only report the number if it differs.
196
197        %expect-rr number
198              tell  yacc the expected number of reduce/reduce conflicts.  That
199              makes it only report the number if it differs.  This is  (unlike
200              bison) allowable in LALR parsers.
201
202        %locations
203              tells  yacc to enable management of position information associ‐
204              ated with each token, provided by the lexer in the global  vari‐
205              able yylloc, similar to management of semantic value information
206              provided in yylval.
207
208              As for semantic values, locations can be referenced  within  ac‐
209              tions  using  @$  to refer to the location of the left hand side
210              symbol, and @N (N an integer) to refer to the location of one of
211              the  right hand side symbols.  Also as for semantic values, when
212              a rule is matched, a default action is used the compute the  lo‐
213              cation  represented  by  @$ as the beginning of the first symbol
214              and the end of the last symbol in the right  hand  side  of  the
215              rule.   This  default  computation can be overridden by explicit
216              assignment to @$ in a rule action.
217
218              The type of yylloc is YYLTYPE, which is defined by default as:
219
220              typedef struct YYLTYPE {
221                  int first_line;
222                  int first_column;
223                  int last_line;
224                  int last_column;
225              } YYLTYPE;
226
227              YYLTYPE can be redefined by the user (YYLTYPE_IS_DEFINED must be
228              defined,  to inhibit the default) in the declarations section of
229              the specification file.  As in bison, the  macro  YYLLOC_DEFAULT
230              is  invoked  each time a rule is matched to calculate a position
231              for the left hand side of the rule, before the associated action
232              is executed; this macro can be redefined by the user.
233
234              This  directive  adds  a YYLTYPE parameter to yyerror().  If the
235              %pure-parser directive is present, a YYLTYPE parameter is  added
236              to yylex() calls.
237
238        %lex-param { argument-declaration }
239              By default, the lexer accepts no parameters, e.g., yylex().  Use
240              this directive to add parameter declarations for your customized
241              lexer.
242
243        %parse-param { argument-declaration }
244              By  default,  the parser accepts no parameters, e.g., yyparse().
245              Use this directive to add parameter declarations for  your  cus‐
246              tomized parser.
247
248        %pure-parser
249              Most variables (other than yydebug and yynerrs) are allocated on
250              the stack within yyparse, making  the  parser  reasonably  reen‐
251              trant.
252
253        %token-table
254              Make  the parser's names for tokens available in the yytname ar‐
255              ray.  However, yacc does not predefine “$end”, “$error” or “$un‐
256              defined” in this array.
257

PORTABILITY

259       According to Robert Corbett,
260
261              Berkeley Yacc is an LALR(1) parser generator.  Berkeley Yacc
262              has been made as compatible as possible with AT&T Yacc.
263              Berkeley Yacc can accept any input specification that
264              conforms to the AT&T Yacc documentation.  Specifications
265              that take advantage of undocumented features of AT&T Yacc
266              will probably be rejected.
267
268       The rationale in
269
270              http://pubs.opengroup.org/onlinepubs/9699919799/utilities/yacc.html
271
272       documents  some  features of AT&T yacc which are no longer required for
273       POSIX compliance.
274
275       That said, you may be interested in reusing  grammar  files  with  some
276       other  implementation  which is not strictly compatible with AT&T yacc.
277       For instance, there is bison.  Here are a few differences:
278
279Yacc accepts an equals mark preceding the left curly  brace  of  an
280           action (as in the original grammar file ftp.y):
281
282                  |   STAT CRLF
283                      = {
284                              statcmd();
285                      }
286
287Yacc  and bison emit code in different order, and in particular bi‐
288           son makes forward reference to common functions such as yylex,  yy‐
289           parse and yyerror without providing prototypes.
290
291       •   Bison's  support  for “%expect” is broken in more than one release.
292           For best results using bison, delete that directive.
293
294       •   Bison has no equivalent for some of  yacc's  command-line  options,
295           relying on directives embedded in the grammar file.
296
297       •   Bison's  “-y”  option  does  not affect bison's lack of support for
298           features of AT&T yacc which were deemed obsolescent.
299
300Yacc accepts multiple parameters with %lex-param  and  %parse-param
301           in two forms
302
303              {type1 name1} {type2 name2} ...
304              {type1 name1,  type2 name2 ...}
305
306           Bison  accepts  the  latter (though undocumented), but depending on
307           the release may generate bad code.
308
309       •   Like bison, yacc will add parameters specified via %parse-param  to
310           yyparse,  yyerror  and (if configured for back-tracking) to the de‐
311           structor declared using %destructor.  Bison puts the additional pa‐
312           rameters  first  for  yyparse and yyerror but last for destructors.
313           Yacc matches this behavior.
314

DIAGNOSTICS

316       If there are rules that are never reduced, the number of such rules  is
317       reported  on  standard  error.  If there are any LALR(1) conflicts, the
318       number of conflicts is reported on standard error.
319
320
321
322Berkeley Yacc                  November 6, 2022                        YACC(1)
Impressum