1YACC(1) User Commands YACC(1)
2
3
4
6 Yacc - an LALR(1) parser generator
7
9 yacc [ -dgilrtv ] [ -b file_prefix ] [ -p symbol_prefix ] filename
10
12 Yacc reads the grammar specification in the file filename and generates
13 an LALR(1) parser for it. The parsers consist of a set of LALR(1)
14 parsing tables and a driver routine written in the C programming lan‐
15 guage. Yacc normally writes the parse tables and the driver routine to
16 the file y.tab.c.
17
18 The following options are available:
19
20 -b file_prefix
21 The -b option changes the prefix prepended to the output file
22 names to the string denoted by file_prefix. The default prefix is
23 the character y.
24
25 -d The -d option causes the header file y.tab.h to be written. It
26 contains #define's for the token identifiers.
27
28 -g The -g option causes a graphical description of the generated
29 LALR(1) parser to be written to the file y.dot in graphviz format,
30 ready to be processed by dot(1).
31
32 -i The -i option causes a supplementary header file y.tab.i to be
33 written. It contains extern declarations and supplementary
34 #define's as needed to map the conventional yacc yy-prefixed names
35 to whatever the -p option may specify. The code file, e.g.,
36 y.tab.c is modified to #include this file as well as the y.tab.h
37 file, enforcing consistent usage of the symbols defined in those
38 files.
39
40 The supplementary header file makes it simpler to separate compi‐
41 lation of lex- and yacc-files.
42
43 -l If the -l option is not specified, yacc will insert #line direc‐
44 tives in the generated code. The #line directives let the C com‐
45 piler relate errors in the generated code to the user's original
46 code. If the -l option is specified, yacc will not insert the
47 #line directives. #line directives specified by the user will be
48 retained.
49
50 -o output_file
51 specify the filename for the parser file. If this option is not
52 given, the output filename is the file prefix concatenated with
53 the file suffix, e.g., y.tab.c. This overrides the -p option.
54
55 -p symbol_prefix
56 The -p option changes the prefix prepended to yacc-generated sym‐
57 bols to the string denoted by symbol_prefix. The default prefix
58 is the string yy.
59
60 -P create a reentrant parser, e.g., "%pure-parser".
61
62 -r The -r option causes yacc to produce separate files for code and
63 tables. The code file is named y.code.c, and the tables file is
64 named y.tab.c. The prefix "y." can be overridden using the -b
65 option.
66
67 -s suppress "#define" statements generated for string literals in a
68 "%token" statement, to more closely match original yacc behavior.
69
70 Normally when yacc sees a line such as
71
72 %token OP_ADD "ADD"
73
74 it notices that the quoted "ADD" is a valid C identifier, and gen‐
75 erates a #define not only for OP_ADD, but for ADD as well, e.g.,
76
77 #define OP_ADD 257
78 #define ADD 258
79
80 The original yacc does not generate the second "#define". The -s
81 option suppresses this "#define".
82
83 POSIX (IEEE 1003.1 2004) documents only names and numbers for
84 "%token", though original yacc and bison also accept string liter‐
85 als.
86
87 -t The -t option changes the preprocessor directives generated by
88 yacc so that debugging statements will be incorporated in the com‐
89 piled code.
90
91 -v The -v option causes a human-readable description of the generated
92 parser to be written to the file y.output.
93
94 -V print the version number to the standard output.
95
96 -y yacc ignores this option, which bison supports for ostensible
97 POSIX compatibility.
98
100 yacc provides some extensions for compatibility with bison and other
101 implementations of yacc:
102
103 %expect number
104 tell yacc the expected number of shift/reduce conflicts. That
105 makes it only report the number if it differs.
106
107 %expect-rr number
108 tell yacc the expected number of reduce/reduce conflicts. That
109 makes it only report the number if it differs. This is (unlike
110 bison) allowable in LALR parsers.
111
112 %lex-param { argument-declaration }
113 By default, the lexer accepts no parameters, e.g., yylex(). Use
114 this directive to add parameter declarations for your customized
115 lexer.
116
117 %parse-param { argument-declaration }
118 By default, the parser accepts no parameters, e.g., yyparse().
119 Use this directive to add parameter declarations for your cus‐
120 tomized parser.
121
122 %pure-parser
123 Most variables (other than yydebug and yynerrs) are allocated on
124 the stack within yyparse, making the parser reasonably reen‐
125 trant.
126
128 According to Robert Corbett,
129
130 Berkeley Yacc is an LALR(1) parser generator. Berkeley Yacc has been made
131 as compatible as possible with AT&T Yacc. Berkeley Yacc can accept any input
132 specification that conforms to the AT&T Yacc documentation. Specifications
133 that take advantage of undocumented features of AT&T Yacc will probably be
134 rejected.
135
136 The rationale in
137
138 http://pubs.opengroup.org/onlinepubs/9699919799/utilities/yacc.html
139
140 documents some features of AT&T yacc which are no longer required for
141 POSIX compliance.
142
143 That said, you may be interested in reusing grammary files with some
144 other implementation which is not strictly compatible with AT&T yacc.
145 For instance, there is bison. Here are a few differences:
146
147 · Yacc accepts an equals mark preceding the left curly brace of an
148 action (as in the original grammar file ftp.y):
149
150 | STAT CRLF
151 = {
152 statcmd();
153 }
154
155 · Yacc and bison emit code in different order, and in particular
156 bison makes forward reference to common functions such as yylex,
157 yyparse and yyerror without providing prototypes.
158
159 · Bison's support for "%expect" is broken in more than one release.
160 For best results using bison, delete that directive.
161
162 · Bison has no equivalent for some of yacc's commmand-line options,
163 relying on directives embedded in the grammar file.
164
165 · Bison's "-y" option does not affect bison's lack of support for
166 features of AT&T yacc which were deemed obsolescent.
167
169 If there are rules that are never reduced, the number of such rules is
170 reported on standard error. If there are any LALR(1) conflicts, the
171 number of conflicts is reported on standard error.
172
173
174
175Berkeley Yacc September 7, 2011 YACC(1)