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

EXTENSIONS

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

PORTABILITY

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

DIAGNOSTICS

270       If  there are rules that are never reduced, the number of such rules is
271       reported on standard error.  If there are any  LALR(1)  conflicts,  the
272       number of conflicts is reported on standard error.
273
274
275
276Berkeley Yacc                   October 5, 2014                        YACC(1)
Impressum