automake: Yacc and Lex

1 
1 8.8 Yacc and Lex support
1 ========================
1 
1 Automake has somewhat idiosyncratic support for Yacc and Lex.
1 
1    Automake assumes that the ‘.c’ file generated by ‘yacc’ (or ‘lex’)
1 should be named using the basename of the input file.  That is, for a
1 yacc source file ‘foo.y’, Automake will cause the intermediate file to
1 be named ‘foo.c’ (as opposed to ‘y.tab.c’, which is more traditional).
1 
1    The extension of a yacc source file is used to determine the
1 extension of the resulting C or C++ source and header files.  Note that
1 header files are generated only when the ‘-d’ Yacc option is used; see
1 below for more information about this flag, and how to specify it.
1 Files with the extension ‘.y’ will thus be turned into ‘.c’ sources and
1 ‘.h’ headers; likewise, ‘.yy’ will become ‘.cc’ and ‘.hh’, ‘.y++’ will
1 become ‘c++’ and ‘h++’, ‘.yxx’ will become ‘.cxx’ and ‘.hxx’, and ‘.ypp’
1 will become ‘.cpp’ and ‘.hpp’.
1 
1    Similarly, lex source files can be used to generate C or C++; the
1 extensions ‘.l’, ‘.ll’, ‘.l++’, ‘.lxx’, and ‘.lpp’ are recognized.
1 
1    You should never explicitly mention the intermediate (C or C++) file
1 in any ‘SOURCES’ variable; only list the source file.
1 
1    The intermediate files generated by ‘yacc’ (or ‘lex’) will be
1 included in any distribution that is made.  That way the user doesn’t
1 need to have ‘yacc’ or ‘lex’.
1 
1    If a ‘yacc’ source file is seen, then your ‘configure.ac’ must define
1 the variable ‘YACC’.  This is most easily done by invoking the macro
11 ‘AC_PROG_YACC’ (⇒Particular Program Checks (autoconf)Particular
 Programs.).
1 
1    When ‘yacc’ is invoked, it is passed ‘AM_YFLAGS’ and ‘YFLAGS’.  The
1 latter is a user variable and the former is intended for the
1 ‘Makefile.am’ author.
1 
1    ‘AM_YFLAGS’ is usually used to pass the ‘-d’ option to ‘yacc’.
1 Automake knows what this means and will automatically adjust its rules
1 to update and distribute the header file built by ‘yacc -d’(1).  What
1 Automake cannot guess, though, is where this header will be used: it is
1 up to you to ensure the header gets built before it is first used.
1 Typically this is necessary in order for dependency tracking to work
1 when the header is included by another file.  The common solution is
1 listing the header file in ‘BUILT_SOURCES’ (⇒Sources) as follows.
1 
1      BUILT_SOURCES = parser.h
1      AM_YFLAGS = -d
1      bin_PROGRAMS = foo
1      foo_SOURCES = ... parser.y ...
1 
1    If a ‘lex’ source file is seen, then your ‘configure.ac’ must define
11 the variable ‘LEX’.  You can use ‘AC_PROG_LEX’ to do this (⇒
 Particular Program Checks (autoconf)Particular Programs.), but using
1 ‘AM_PROG_LEX’ macro (⇒Macros) is recommended.
1 
1    When ‘lex’ is invoked, it is passed ‘AM_LFLAGS’ and ‘LFLAGS’.  The
1 latter is a user variable and the former is intended for the
1 ‘Makefile.am’ author.
1 
1    When ‘AM_MAINTAINER_MODE’ (⇒maintainer-mode) is used, the
1 rebuild rule for distributed Yacc and Lex sources are only used when
1 ‘maintainer-mode’ is enabled, or when the files have been erased.
1 
1    When ‘lex’ or ‘yacc’ sources are used, ‘automake -a’ automatically
11 installs an auxiliary program called ‘ylwrap’ in your package (⇒
 Auxiliary Programs).  This program is used by the build rules to
1 rename the output of these tools, and makes it possible to include
1 multiple ‘yacc’ (or ‘lex’) source files in a single directory.  (This is
1 necessary because yacc’s output file name is fixed, and a parallel make
1 could conceivably invoke more than one instance of ‘yacc’
1 simultaneously.)
1 
1    For ‘yacc’, simply managing locking is insufficient.  The output of
1 ‘yacc’ always uses the same symbol names internally, so it isn’t
1 possible to link two ‘yacc’ parsers into the same executable.
1 
1    We recommend using the following renaming hack used in ‘gdb’:
1      #define yymaxdepth c_maxdepth
1      #define yyparse c_parse
1      #define yylex   c_lex
1      #define yyerror c_error
1      #define yylval  c_lval
1      #define yychar  c_char
1      #define yydebug c_debug
1      #define yypact  c_pact
1      #define yyr1    c_r1
1      #define yyr2    c_r2
1      #define yydef   c_def
1      #define yychk   c_chk
1      #define yypgo   c_pgo
1      #define yyact   c_act
1      #define yyexca  c_exca
1      #define yyerrflag c_errflag
1      #define yynerrs c_nerrs
1      #define yyps    c_ps
1      #define yypv    c_pv
1      #define yys     c_s
1      #define yy_yys  c_yys
1      #define yystate c_state
1      #define yytmp   c_tmp
1      #define yyv     c_v
1      #define yy_yyv  c_yyv
1      #define yyval   c_val
1      #define yylloc  c_lloc
1      #define yyreds  c_reds
1      #define yytoks  c_toks
1      #define yylhs   c_yylhs
1      #define yylen   c_yylen
1      #define yydefred c_yydefred
1      #define yydgoto  c_yydgoto
1      #define yysindex c_yysindex
1      #define yyrindex c_yyrindex
1      #define yygindex c_yygindex
1      #define yytable  c_yytable
1      #define yycheck  c_yycheck
1      #define yyname   c_yyname
1      #define yyrule   c_yyrule
1 
1    For each define, replace the ‘c_’ prefix with whatever you like.
1 These defines work for ‘bison’, ‘byacc’, and traditional ‘yacc’s.  If
1 you find a parser generator that uses a symbol not covered here, please
1 report the new name so it can be added to the list.
1 
1    ---------- Footnotes ----------
1 
1    (1) Please note that ‘automake’ recognizes ‘-d’ in ‘AM_YFLAGS’ only
1 if it is not clustered with other options; for example, it won’t be
1 recognized if ‘AM_YFLAGS’ is ‘-dt’, but it will be if ‘AM_YFLAGS’ is ‘-d
1 -t’ or ‘-t -d’.
1