I have a lex input .l file that includes a bunch of rules and function definitions for when those rules are encountered.
I am using this scanner from another source file, main.c
In main.c, I can call yylex() without having to have any kind of reference to my generated lex file. Why does this work? I am using cmake, and I noticed that it creates an object out of my lexer.c file, and links that to my executable. I would have thought I would need an external reference to yylex() though for that to work.
My question is should I create forward declarations for all of the functions I use in my lex .l file in a separate header, and include that in the lex file? Taking it a step further, should I define those functions outside of the lex file, and have them be used by way of referencing the include file?
You can call yylex()
with no problem because it's an int
function. So technically you don't need a prototype (although this is not proper C99 or C1x behavior).
It's always a good idea to have a header file for all of your external (public) types, variables, and functions. You are not required to include it in your lex source file, but it's a very good idea to do so (if nothing else, to check that your declarations are correct). You definitely should include the header it in any file that uses the lex functions.