P29821 [洛谷] [Provincial Selection Joint Examination 2022] Preprocessor
提高-
题目描述

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 #define and macro undefinition command #undef.
  • The format of the macro definition command is #define <name> <content>, where the first part #define is 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 #undef is 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:

  1. 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 A is defined as b, then the text A expands to b (replacement occurs), the text B remains B (undefined, no replacement), the text AA remains AA (different from A, undefined), and the text A*B expands to b*B.

  2. After replacement, if the expansion content contains identifiers, repeat the expansion operation, known as "multiple expansions". For example, if macro A is defined as B, and macro B is defined as c, then the text A expands to c.

  3. 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 A is defined as B+a, and macro B is defined as A+b, then the text A expands to A+b+a, and since the initial A is in the expanding state, the A in A+b+a is not expanded further.

  4. 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 部分

样例输入
5 #define BEGIN { #define END } #define INTEGER int class C BEGIN INTEGER x; END; INTEGER main() BEGIN C c; END
样例输出
class C { int x; }; int main() { C c; }