题目描述
Problem Description
Macros are a feature of C/C++ languages that perform text substitution based on predefined rules (also known as "macro expansion"), enabling functionalities such as defining constants and simplifying repetitive code input. For example:
#define PI 3.14159
double area = PI * r * r;
The above code, after macro expansion, becomes:
double area = 3.14159 * r * r;
Here, the macro definition command becomes an empty line, while macros in other lines are expanded into the text defined by the rules.
The preprocessing of macros in C/C++ code is handled by the preprocessor during compilation. Your task is to implement a simplified version of a preprocessor with the following requirements:
-
The code consists of lines, each containing printable ASCII characters (ASCII code range $32\sim 126$) except for the newline character at the end of each line. Each line is either a preprocessing command (starting with
#) or ordinary text (otherwise). -
The preprocessor processes the code line by line:
- If it is a preprocessing command, execute the command and output an empty line.
- If it is ordinary text, perform macro expansion on it and output the result.
- There are two types of preprocessing commands: macro definition command
#defineand macro undefinition command#undef. - The format of the macro definition command is
#define <name> <content>, where the first part#defineis the command name, the second part<name>is the name of the macro to be defined, and the third part<content>is the expansion content of the macro. - The format of the macro undefinition command is
#undef <name>, where the first part#undefis the command name, and the second part<name>is the name of the macro to be undefined.
In both preprocessing commands, adjacent parts are strictly separated by a single space. <name> is an identifier consisting of uppercase and lowercase letters, digits, and underscores (one or more characters), and <content> can contain any printable ASCII characters (zero or more characters). The effective scope of a macro definition starts from the line where it is defined to the line where the nearest matching undefinition occurs (if no corresponding undefinition exists, the scope covers until the end of the file).
When expanding macros in ordinary text, each continuous longest sequence of uppercase and lowercase letters, digits, and underscores is considered an identifier (not just a part of it), and the rest are other characters. Identifiers in the text are expanded from left to right:
-
If the identifier is a valid macro name, replace it with the corresponding expansion content, and the macro name enters the expanding state until this process ends; otherwise, keep the macro name as is. For example, if macro
Ais defined asb, then the textAexpands tob(replacement occurs), the textBremainsB(undefined, no replacement), the textAAremainsAA(different fromA, undefined), and the textA*Bexpands tob*B. -
After replacement, if the expansion content contains identifiers, repeat the expansion operation, known as "multiple expansions". For example, if macro
Ais defined asB, and macroBis defined asc, then the textAexpands toc. -
If the macro name to be expanded is the same as one currently being expanded, it is called "recursive expansion", and the macro name is not expanded further. This rule prevents infinite recursive expansion. For example, if macro
Ais defined asB+a, and macroBis defined asA+b, then the textAexpands toA+b+a, and since the initialAis in the expanding state, theAinA+b+ais not expanded further. -
Other characters are kept as is.
Note: For simplicity, the requirements of this problem do not fully align with the descriptions in the C/C++ language standards. Please follow the requirements above. The most notable difference is that this problem only distinguishes between identifiers and other characters, without numerical values, strings, comments, etc.
Input Format
The first line of input contains a positive integer $n$, representing the number of lines of code to be processed.
The next $n$ lines are the code to be processed.
Output Format
Output $n$ lines, which are the results of the input code processed line by line.
Sample Input and Output
Sample Input #1
5
#define BEGIN {
#define END }
#define INTEGER int
class C BEGIN INTEGER x; END;
INTEGER main() BEGIN C c; END
Sample Output #1
```
输入格式
见题面 Input 部分
输出格式
见题面 Output 部分