1.2. So What's a Metaprogram?If you dissect the word metaprogram literally, it means "a program about a program."[1] A little less poetically, a metaprogram is a program that manipulates code. It may be an odd-sounding concept, but you're probably already familiar with several such beasts. Your C++ compiler is one example: it manipulates your C++ code to produce assembly language or machine code.
Parser generators such as YACC [Joh79] are another kind of program-manipulating program. The input to YACC is a high-level parser description written in terms of grammar rules and attached actions brace-enclosed. For instance, to parse and evaluate arithmetic expressions with the usual precedence rules, we might feed YACC the following grammar description: expression : term | expression '+' term { $$ = $1 + $3; } | expression '-' term { $$ = $1 - $3; } ; term : factor | term '*' factor { $$ = $1 * $3; } | term '/' factor { $$ = $1 / $3; } ; factor : INTEGER | group ; group : '(' expression ')' ; In response, YACC would generate a C/C++ source file containing (among other things), a yyparse function that we can call to parse text against the grammar and execute the appropriate actions:[2]
int main() { extern int yyparse(); return yyparse(); } The user of YACC is operating mostly in the domain of parser design, so we'll call YACC's input language the domain language of this system. Because the rest of the user's program typically requires a general-purpose programming system and must interact with the generated parser, YACC translates the domain language into the host language, C, which the user then compiles and links together with her other code. The domain language thus undergoes two translation steps, and the user is always very conscious of the boundary between it and the rest of her program. |