Next: , Previous: , Up: FAQ   [Contents][Index]


How can I expand macros in the input?

The best way to approach this problem is at a higher level, e.g., in the parser.

However, you can do this using multiple input buffers.

%%
macro/[a-z]+   {
/* Saw the macro "macro" followed by extra stuff. */
main_buffer = yy_current_buffer();
expansion_buffer = yy_scan_string(expand(yytext));
yy_switch_to_buffer(expansion_buffer);
}

<<EOF>> {
                if ( expansion_buffer ) {
                // We were doing an expansion, return to where
                // we were.
                yy_switch_to_buffer(main_buffer);
                yy_delete_buffer(expansion_buffer);
                expansion_buffer = 0;
                } else {
                        yyterminate();
                }
        }

You probably will want a stack of expansion buffers to allow nested macros. From the above though hopefully the idea is clear.