Call lex / yacc from a separate program

I read lex / yacc. Books and examples are not hard to follow. In fact, the whole concept is understood as a bell. With one exception. Lex / yacc seems to be creating standalone programs. What if I would like to name them as a function of the parser? It seems that Yacc is generating the main function, so how could I call from my own without conflicts.

I also do not understand how they work with file input. I'm fine, grab a line from a file and send it to the parser, but what if your parser is looking for a multi-line structure, such as a block enclosed in braces?

I understand that I am stupid in both of them, but any help is appreciated. Thank.

+3
source share
2 answers

input to the lexx / yacc combination is done via FILE *, called yyin.

for this, stdin is used by default - trawl around lex.yy.c to find it

if you assign FILE * to yyin, lexer will read from this file, you are doing something like

yyin = fopen ("parseme", "rt");

before calling yyparse (), usually in your main ().

+3
source

Both programs generate functions yylex()and yyparse(), but none of them generate the main function. You must add your own function main()somewhere. Many tutorials put them in a .l or .y file, but you can put them wherever you want.

+2
source

Source: https://habr.com/ru/post/1761326/


All Articles