- Do not include %option main in your lex
specification file. This will cause a default main to be created only
for lex and the yacc source will be ignored.
- Since you will not be using the default routines provided
by %option main, you have to include the definition
of yywrap() in your lex specification file. A simple example
would look like this:
...definitions...
%%
...rules...
%%
...additional user code...
int yywrap() { return 1; }
- Since on some Linux systems, default libraries are not provided
for yacc, we recommend that you define your own main()
and yyerror() functions in your yacc specification file. So,
the following would be a simple yacc specification file which does this:
...declarations...
%%
...rules...
%%
...additional user code...
main() {
return yyparse();
}
int yyerror( char *s ) { fprintf( stderr, "%s\n", s); }
- For large projects, you could compile lex.yy.c
and y.tab.c and link them afterwards. However, for simple
cases (and most likely your projects), you could directly
include lex.yy.c in the user code section of your yacc
specification. Your yacc specification would then look something
like the following:
...declarations...
%%
...rules...
%%
#include "lex.yy.c"
...additional user code...
main() {
return yyparse();
}
int yyerror( char *s ) { fprintf( stderr, "%s\n", s); }
- These custom definitions eliminate the need to link any external
libraries. So, assuming your have the specification
files example.l and example.y, the following commands would
get you a nice executable which hopefully does what you want:
lex example.l
yacc example.y
gcc -o example y.tab.c
./example