How can I generate various yyparse functions from lex / yacc for use in the same program?

I want to create two separate parsing functions from lex / yacc. Usually yacc provides you with the yyparse () function, which you can call when you need to parse, but I need to have several different yyparses, each of which is associated with different lexers and grammars. It seems that the -man page offers the -p (prefix) flag, but this did not work for me. I got errors from gcc that indicated that yylval was not properly re-marked (i.e., it claims that several different tokens are undefined). Does anyone know a general rpocedure for generating these individual functions?

thank

+3
source share
3 answers

I had the same problem a while ago and ended up writing the following header file:

#ifndef RENAME_FLEX_H
#define RENAME_FLEX_H

#define yy_create_buffer scan__create_buffer
#define yy_delete_buffer scan__delete_buffer
#define yy_init_buffer scan_init_buffer
#define yy_load_buffer_state scan_load_buffer_state
#define yy_switch_to_buffer scan_switch_to_buffer
#define yyin scan_in
#define yyleng scan_leng
#define yylex scan_lex
#define yyout scan_out
#define yyrestart scan_restart
#define yytext scan_text
#define yy_flex_debug scab_flex_debug
#define yywrap scan_wrap
#define yyrealloc scan_realloc
#define yyfree scan_free
#define yy_flush_buffer scan_flush_buffer
#define yypush_buffer_state scan_push_buffer_state
#define yypop_buffer_state scan_pop_buffer_state
#define yy_scan_buffer scan_scan_buffer
#define yy_scan_string scan_scan_string
#define yy_scan_bytes scan_scan_bytes
#define yyget_in scan_get_in
#define yyget_out scan_get_out
#define yyget_leng scan_get_leng
#define yyset_text scan_set_text
#define yyset_in scan_set_in
#define yyset_out scan_set_out
#define yyget_debug scan_get_debug
#define yyset_debug scan_set_debug
#define yylex_destroy scan_lex_destroy
#define yyalloc scan_alloc
#define yyget_text scan_get_text

#endif

and include it in .l, so from my file .cI can use the scan_-prefixed characters for the second scanner, and not yy-prefixed

+2
source

How about a function pointer in the surrounding code that swaps parses, assuming you don't want to change the parsing context in the middle yylex()in the same buffer.

The function pointer can be set from the inclusion of parsers with the prefix option or from loaded from DSO at runtime, a-la plug-in configuration.

, , , AST, , /.

+1

flex/bison, "", ( ) . Flex. / . .

- ++- , C -.

+1

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


All Articles