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

NAME

6       Yacc - an LALR(1) parser generator
7

SYNOPSIS

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

EXTENSIONS

117       Yacc provides some extensions for compatibility with  bison  and  other
118       implementations  of  yacc.  The %destructor and %locations features are
119       available only if yacc has been configured and compiled to support  the
120       back-tracking  (btyacc)  functionality.   The  remaining  features  are
121       always available:
122
123        %destructor { code } symbol+
124              defines code that is invoked when a symbol is automatically dis‐
125              carded  during error recovery.  This code can be used to reclaim
126              dynamically allocated memory associated with  the  corresponding
127              semantic  value  for  cases where user actions cannot manage the
128              memory explicitly.
129
130              On encountering a parse error,  the  generated  parser  discards
131              symbols  on  the stack and input tokens until it reaches a state
132              that will  allow  parsing  to  continue.   This  error  recovery
133              approach  results  in  a memory leak if the YYSTYPE value is, or
134              contains, pointers to dynamically allocated memory.
135
136              The bracketed code is invoked whenever the parser  discards  one
137              of  the  symbols.  Within code, “$$” or “$<tag>$” designates the
138              semantic value associated with the discarded symbol,  and   “@$
139              designates its location (see %locations directive).
140
141              A  per-symbol  destructor is defined by listing a grammar symbol
142              in symbol+.  A per-type destructor  is  defined   by  listing  a
143              semantic type tag (e.g., “<some_tag>”) in symbol+; in this case,
144              the parser will invoke code whenever  it  discards  any  grammar
145              symbol  that  has that semantic type tag, unless that symbol has
146              its own per-symbol destructor.
147
148              Two categories of default  destructor  are  supported  that  are
149              invoked  when discarding any grammar symbol that has no per-sym‐
150              bol and no per-type destructor:
151
152              ·   the code for “<*>” is used for grammar symbols that have  an
153                  explicitly declared semantic type tag (via “%type”);
154
155              ·   the  code  for “<>” is used for grammar symbols that have no
156                  declared semantic type tag.
157
158        %expect number
159              tells yacc the expected number of shift/reduce conflicts.   That
160              makes it only report the number if it differs.
161
162        %expect-rr number
163              tell  yacc the expected number of reduce/reduce conflicts.  That
164              makes it only report the number if it differs.  This is  (unlike
165              bison) allowable in LALR parsers.
166
167        %locations
168              tells yacc to enable  management of position information associ‐
169              ated with each token, provided by the lexer in the global  vari‐
170              able yylloc, similar to management of semantic value information
171              provided in yylval.
172
173              As for semantic  values,  locations  can  be  referenced  within
174              actions  using @$ to refer to the location of the left hand side
175              symbol, and @N (N an integer) to refer to the location of one of
176              the  right hand side symbols.  Also as for semantic values, when
177              a rule is matched, a default action  is  used  the  compute  the
178              location  represented by @$ as the beginning of the first symbol
179              and the end of the last symbol in the right  hand  side  of  the
180              rule.   This  default  computation can be overridden by explicit
181              assignment to @$ in a rule action.
182
183              The type of yylloc is YYLTYPE, which is defined by default as:
184
185              typedef struct YYLTYPE {
186                  int first_line;
187                  int first_column;
188                  int last_line;
189                  int last_column;
190              } YYLTYPE;
191
192              YYLTYPE can be redefined by the user (YYLTYPE_IS_DEFINED must be
193              defined,  to inhibit the default) in the declarations section of
194              the specification file.  As in bison, the  macro  YYLLOC_DEFAULT
195              is  invoked  each time a rule is matched to calculate a position
196              for the left hand side of the rule, before the associated action
197              is executed; this macro can be redefined by the user.
198
199              This  directive  adds  a YYLTYPE parameter to yyerror().  If the
200              %pure-parser directive is present, a YYLTYPE parameter is  added
201              to yylex() calls.
202
203        %lex-param { argument-declaration }
204              By default, the lexer accepts no parameters, e.g., yylex().  Use
205              this directive to add parameter declarations for your customized
206              lexer.
207
208        %parse-param { argument-declaration }
209              By  default,  the parser accepts no parameters, e.g., yyparse().
210              Use this directive to add parameter declarations for  your  cus‐
211              tomized parser.
212
213        %pure-parser
214              Most variables (other than yydebug and yynerrs) are allocated on
215              the stack within yyparse, making  the  parser  reasonably  reen‐
216              trant.
217
218        %token-table
219              Make  the  parser's  names  for  tokens available in the yytname
220              array.  However, yacc does not  predefine  “$end”,  “$error”  or
221              “$undefined” in this array.
222

PORTABILITY

224       According to Robert Corbett,
225
226              Berkeley Yacc is an LALR(1) parser generator.  Berkeley Yacc
227              has been made as compatible as possible with AT&T Yacc.
228              Berkeley Yacc can accept any input specification that
229              conforms to the AT&T Yacc documentation.  Specifications
230              that take advantage of undocumented features of AT&T Yacc
231              will probably be rejected.
232
233       The rationale in
234
235              http://pubs.opengroup.org/onlinepubs/9699919799/utilities/yacc.html
236
237       documents  some  features of AT&T yacc which are no longer required for
238       POSIX compliance.
239
240       That said, you may be interested in reusing  grammar  files  with  some
241       other  implementation  which is not strictly compatible with AT&T yacc.
242       For instance, there is bison.  Here are a few differences:
243
244       ·   Yacc accepts an equals mark preceding the left curly  brace  of  an
245           action (as in the original grammar file ftp.y):
246
247                  |   STAT CRLF
248                      = {
249                              statcmd();
250                      }
251
252       ·   Yacc  and  bison  emit  code  in different order, and in particular
253           bison makes forward reference to common functions  such  as  yylex,
254           yyparse and yyerror without providing prototypes.
255
256       ·   Bison's  support  for “%expect” is broken in more than one release.
257           For best results using bison, delete that directive.
258
259       ·   Bison has no equivalent for some of yacc's  commmand-line  options,
260           relying on directives embedded in the grammar file.
261
262       ·   Bison's  “-y”  option  does  not affect bison's lack of support for
263           features of AT&T yacc which were deemed obsolescent.
264
265       ·   Yacc accepts multiple parameters with %lex-param  and  %parse-param
266           in two forms
267
268              {type1 name1} {type2 name2} ...
269              {type1 name1,  type2 name2 ...}
270
271           Bison  accepts  the  latter (though undocumented), but depending on
272           the release may generate bad code.
273
274       ·   Like bison, yacc will add parameters specified via %parse-param  to
275           yyparse,  yyerror  and  (if  configured  for  back-tracking) to the
276           destructor declared using %destructor.  Bison puts  the  additional
277           parameters  first for yyparse and yyerror but last for destructors.
278           Yacc matches this behavior.
279

DIAGNOSTICS

281       If there are rules that are never reduced, the number of such rules  is
282       reported  on  standard  error.  If there are any LALR(1) conflicts, the
283       number of conflicts is reported on standard error.
284
285
286
287Berkeley Yacc                    June 16, 2019                         YACC(1)
Impressum