8.8 Yacc and Lex support

Automake has somewhat idiosyncratic support for Yacc and Lex.

Automake assumes that the .c file generated by yacc or lex should be named using the basename of the input file. That is, for a Yacc source file foo.y, Automake will cause the intermediate file to be named foo.c (as opposed to y.tab.c, which is more traditional).

The extension of a Yacc source file is used to determine the extension of the resulting C or C++ source and header files. Be aware that header files are generated only when the option -d is given to Yacc; see below for more information about this flag, and how to specify it. Files with the extension .y will thus be turned into .c sources and .h headers; likewise, .yy will become .cc and .hh, .y++ will become c++ and h++, .yxx will become .cxx and .hxx, and .ypp will become .cpp and .hpp.

Similarly, Lex source files can be used to generate C or C++; the extensions .l, .ll, .l++, .lxx, and .lpp are recognized.

You should never explicitly mention the intermediate (C or C++) file in any SOURCES variable (except BUILT_SOURCES, see below); only list the source file.

The intermediate files generated by yacc (or lex) will be included in any distribution that is made. That way the user doesn’t need to have yacc or lex.

If a Yacc source file is seen, then your configure.ac must define the variable YACC. This is most easily done by invoking the macro AC_PROG_YACC (see Particular Program Checks in The Autoconf Manual).

When yacc is invoked, it is passed AM_YFLAGS and YFLAGS. The latter is a user variable and the former is intended for the Makefile.am author.

AM_YFLAGS is usually used to pass the -d option to yacc. Automake knows what this means and will automatically adjust its rules to update and distribute the header file built by ‘yacc -d’. Caveat: automake recognizes -d in AM_YFLAGS only if it is not clustered with other options; for example, it won’t be recognized if AM_YFLAGS is -dt, but it will be if AM_YFLAGS is -d -t or -t -d.

What Automake cannot guess, though, is where this header will be used: it is up to you to ensure the header gets built before it is first used. Typically this is necessary in order for dependency tracking to work when the header is included by another file. The common solution is listing the header file, and the corresponding C file, in BUILT_SOURCES (see Built Sources) as follows.

BUILT_SOURCES = parser.h parser.c
AM_YFLAGS = -d
bin_PROGRAMS = foo
foo_SOURCES = ... parser.y ...

If a Lex source file is seen, then your configure.ac must define the variable LEX. You can use AC_PROG_LEX to do this (see Particular Program Checks in The Autoconf Manual), but using the AM_PROG_LEX macro (see Autoconf macros supplied with Automake) is recommended.

When lex is invoked, it is passed AM_LFLAGS and LFLAGS. The latter is a user variable and the former is intended for the Makefile.am author.

When AM_MAINTAINER_MODE (see missing and AM_MAINTAINER_MODE) is in effect, the rebuild rules for distributed Yacc and Lex sources are only used when maintainer-mode is enabled, or when the files have been erased.

When Yacc or Lex sources are used, automake -a automatically installs an auxiliary program called ylwrap in your package (see Programs automake might require). This program is used by the build rules to rename the output of these tools, and makes it possible to include multiple yacc (or lex) source files in a single directory. This is necessary because Yacc’s output file name is fixed, and a parallel make could invoke more than one instance of yacc simultaneously.