MAN page from OpenSuSE 13.X byacc-20140715-2.2.2.i586.rpm


Section: User Commands (1)
Updated: January 1, 2014


Yacc - an LALR(1) parser generator 


yacc [ -BdgilLPrtvVy ] [ -bfile_prefix] [ -ooutput_file] [ -psymbol_prefix]filename 


Yaccreads the grammar specification in the filefilenameand generates an LALR(1) parser for it.The parsers consist of a set of LALR(1) parsing tables and a driver routinewritten in the C programming language.Yaccnormally writes the parse tables and the driver routine to the

The following options are available:

-b file_prefix
The-boption changes the prefix prepended to the output file names tothe string denoted byfile_prefix.The default prefix is the charactery.
create a backtracking parser (compile-type configuration for btyacc).
The -d option causes the header be written.It contains #define's for the token identifiers.
The-goption causes a graphical description of the generated LALR(1) parser tobe written to the filey.dotin graphviz format, ready to be processed by dot(1).
The -i option causes a supplementary header be written.It contains extern declarationsand supplementary #define's as needed to map the conventional yaccyy-prefixed names to whatever the -p option may specify.The code file, e.g., is modified to #include this fileas well as the file, enforcing consistent usage of thesymbols defined in those files.
The supplementary header file makes it simpler to separate compilationof lex- and yacc-files.
If the-loption is not specified,yaccwill insert #line directives in the generated code.The #line directives let the C compiler relate errors in thegenerated code to the user's original code.If the -l option is specified,yaccwill not insert the #line directives.#line directives specified by the user will be retained.
enable position processing, e.g., ``%locations'' (compile-type configuration for btyacc).
-o output_file
specify the filename for the parser file.If this option is not given, the output filename isthe file prefix concatenated with the file suffix, e.g., overrides the -b option.
-p symbol_prefix
The-poption changes the prefix prepended to yacc-generated symbols tothe string denoted bysymbol_prefix.The default prefix is the stringyy.
create a reentrant parser, e.g., ``%pure-parser''.
The-roption causesyaccto produce separate files for code and tables.The code file is namedy.code.c,and the tables file is prefix ``y.'' can be overridden using the -b option.
suppress ``#define'' statements generated for string literals ina ``%token'' statement,to more closely match original yacc behavior.
Normally when yacc sees a line such as
%token OP_ADD "ADD"
it notices that the quoted ``ADD'' is a valid C identifier,and generates a #define not only for OP_ADD,but for ADD as well,e.g.,
#define OP_ADD 257
#define ADD 258
The original yacc does not generate the second ``#define''.The -s option suppresses this ``#define''.
POSIX (IEEE 1003.1 2004) documents only names and numbersfor ``%token'',though original yacc and bison also accept string literals.
The-toption changes the preprocessor directives generated byyaccso that debugging statements will be incorporated in the compiled code.
The-voption causes a human-readable description of the generated parser tobe written to the filey.output.
print the version number to the standard output.
yacc ignores this option,which bison supports for ostensible POSIX compatibility.


yaccprovides some extensions forcompatibility with bison and other implementations of yacc.The %destructor and %locations features are availableonly if yacc has been configured and compiled to support theback-tracking (btyacc) functionality.The remaining features are always available:
%destructor { code } symbol+
defines code that is invoked when a symbol is automaticallydiscarded during error recovery.This code can be used toreclaim dynamically allocated memory associated with the correspondingsemantic value for cases where user actions cannot manage the memoryexplicitly.
On encountering a parse error, the generated parserdiscards symbols on the stack and input tokens until it reaches a statethat will allow parsing to continue.This error recovery approach results in a memory leakif the YYSTYPE value is, or contains,pointers to dynamically allocated memory.
The bracketed code is invoked whenever the parser discards one ofthe symbols. Within code, ``$$'' or``$<tag>$'' designates the semantic value associated with thediscarded symbol, and ``@$'' designates its location (see%locations directive).
A per-symbol destructor is defined by listing a grammar symbolin symbol+. A per-type destructor is defined by listinga semantic type tag (e.g., ``<some_tag>'') in symbol+; in thiscase, the parser will invoke code whenever it discards any grammarsymbol that has that semantic type tag, unless that symbol has its ownper-symbol destructor.
Two categories of default destructor are supported that areinvoked when discarding any grammar symbol that has no per-symbol and noper-type destructor:
the code for ``<*>'' is usedfor grammar symbols that have an explicitly declared semantic type tag(via ``%type'');
the code for ``<>'' is usedfor grammar symbols that have no declared semantic type tag.
%expect number
tells yacc the expected number of shift/reduce conflicts.That makes it only report the number if it differs.
%expect-rr number
tell yacc the expected number of reduce/reduce conflicts.That makes it only report the number if it differs.This is (unlike bison) allowable in LALR parsers.
tells yacc to enable management of position information associatedwith each token, provided by the lexer in the global variable yylloc,similar to management of semantic value information provided in yylval.
As for semantic values, locations can be referenced within actions using@$ to refer to the location of the left hand side symbol, and @N(N an integer) to refer to the location of one of the right hand sidesymbols. Also as for semantic values, when a rule is matched, a defaultaction is used the compute the location represented by @$ as thebeginning of the first symbol and the end of the last symbol in the righthand side of the rule. This default computation can be overridden byexplicit assignment to @$ in a rule action.
The type of yylloc is YYLTYPE, which is defined by default as:
typedef struct YYLTYPE {    int first_line;    int first_column;    int last_line;    int last_column;} YYLTYPE;
YYLTYPE can be redefined by the user(YYLTYPE_IS_DEFINED must be defined, to inhibit the default)in the declarations section of the specification file.As in bison, the macro YYLLOC_DEFAULT is invokedeach time a rule is matched to calculate a position for the left hand side ofthe rule, before the associated action is executed; this macro can beredefined by the user.
This directive adds a YYLTYPE parameter to yyerror().If the %pure-parser directive is present,a YYLTYPE parameter is added to yylex() calls.
%lex-param { argument-declaration }
By default, the lexer accepts no parameters, e.g., yylex().Use this directive to add parameter declarations for your customized lexer.
%parse-param { argument-declaration }
By default, the parser accepts no parameters, e.g., yyparse().Use this directive to add parameter declarations for your customized parser.
Most variables (other than yydebug and yynerrs) areallocated on the stack within yyparse, making the parser reasonablyreentrant.
Make the parser's names for tokens available in the yytname array.However,yaccdoes not predefine ``$end'', ``$error''or ``$undefined'' in this array.


According to Robert Corbett,
    Berkeley Yacc is an LALR(1) parser generator.  Berkeley Yacc has been madeas compatible as possible with AT&T Yacc.  Berkeley Yacc can accept any inputspecification that conforms to the AT&T Yacc documentation.  Specificationsthat take advantage of undocumented features of AT&T Yacc will probably berejected.

The rationale in

documents some features of AT&T yacc which are no longer required for POSIXcompliance.

That said, you may be interested in reusing grammar files with someother implementation which is not strictly compatible with AT&T yacc.For instance, there is bison.Here are a few differences:

Yacc accepts an equals mark preceding the left curly braceof an action (as in the original grammar file ftp.y):
        |       STAT CRLF                = {                        statcmd();                }
Yacc and bison emit code in different order, and in particular bisonmakes forward reference to common functions such as yylex, yyparse andyyerror without providing prototypes.
Bison's support for ``%expect'' is broken in more than one release.For best results using bison, delete that directive.
Bison has no equivalent for some of yacc's commmand-line options,relying on directives embedded in the grammar file.
Bison's ``-y'' option does not affect bison's lack of support forfeatures of AT&T yacc which were deemed obsolescent.


If there are rules that are never reduced, the number of such rules isreported on standard error.If there are any LALR(1) conflicts, the number of conflicts is reportedon standard error.




This document was created byman2html,using the manual pages.