Calling lex/yacc from a separate program
I've been reading up on lex/yacc. The books and examples are not hard to follow. In fact, the whole concept is clear as a bell. With one exception. Lex/yacc seem to generate standalone programs. What if I wanted to call them as a parser function? Yacc seems to generate a main function, so how w开发者_开发百科ould I call from my own without conflicts.
I'm also unclear at this point how they operate on file input. I'm okay with grabbing a line from a file and sending it to the parser, but what if your parser is looking for a multi-line structure like a block enclosed in braces?
I realize I'm being stupid about both of these, but any help is appreciated. Thanks.
input to the lexx / yacc combo is via a FILE * called yyin.
this defaults to stdin - trawl around lex.yy.c to find it
if you assign a FILE * to yyin, the lexer will read from that file, you do something like
yyin = fopen ("parseme", "rt");
before the call to yyparse(), typically in your main().
Both programs generate the yylex()
and yyparse()
functions, but none of them generates a main function. You have to add your own main()
function somewhere. Many tutorials place them into the .l or .y file, but you are free to place them where ever you want.
精彩评论