Compiler/Preprocessor

Revision as of 23:42, 23 May 2022 by rosettacode>Jwells1213 (First Preprocessor Task)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

This task modifies the source code prior to the lexical analysis similar to the C built-in preprocessor.

Task
Compiler/Preprocessor
You are encouraged to solve this task according to the task description, using any language you may know.

Create a preprocessor for the simple programming language specified below. The program should read input from a file and/or stdin, and write output to a file and/or stdout.

The program should treat any line starting with a hashtag (#) as a command to process. There are currently two valid commands, include and define. No space between the hashtag and its command. Multiple whitespace is treated the same as one.

The include command must be followed by whitespace and a string who contents is the actual file to read. Includes should allow the inclusion of other files to a recursive limit of five active header files plus the original source file.

The define command must be followed by whitespace and a new macro name. Redefinition is illegal. The same character convention for naming variables in the language is used for macro names. No whitespace is required in the arguments but is allowed between every token. Both no parenthesis or empty parenthesis are equivalent on the define or its usage. This yields no arguments. If there is a close parenthesis, the whitespace trailing it is optional. Otherwise, it is required. From that point to end of line is the definition, whitespace removed from both the start and end of it. Whitespace within the definition between tokens must be maintained. All constant math that can be performed before defining a new macro name must be. Any names within the definition are treated as macro names first before it is assumed they are variable in the language.

To make it easier to find, the usage will be within hashtags, and replaces its usage elsewhere in the files processed. The calling arguments replace the define's parameters as a simple string substitution.

Input Specification

This is an example usage of this concept, given a header and source the output should be able to feed straight into the lexical analyzer task.

~~ Header.h ~~
#define area(h, w) h * w

~~ Source.t ~~
#include "Header.h"
#define width 5
#define height() 6
area = #area(height, width())#;
Output Specification

If you do not support a runtine debugging flag, your code should support only the second version. Otherwise, it should provide either. Yielding code output of:

area = 6 * 5;

Or:

/* Include Header.h */
/* Define area(h, w) as h * w */
/* End Header.h */
/* Define width() as 5 */
/* Define height() as 6 */
/* Use area, height, and width */
area = 6 * 5;