Compiler/Preprocessor: Difference between revisions

From Rosetta Code
Content added Content deleted
(Remove empty parentheses)
m (→‎Task: matching parenthesis cleanup in the suggested output)
Line 42: Line 42:
/* Define area(h, w) as h * w */
/* Define area(h, w) as h * w */
/* End Header.h */
/* End Header.h */
/* Define width() as 5 */
/* Define width as 5 */
/* Define height() as 6 */
/* Define height as 6 */
/* Use area, height, and width */
/* Use area, height, and width */
area = 6 * 5;
area = 6 * 5;

Revision as of 11:30, 25 May 2022

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

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

Create a preprocessor for the simple programming language specified in the lexical analysis task referenced 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. When there are no parameters, neither the definition or usage is allowed to specify parenthesis. 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. Any names within the definition are treated as macro names first before it is assumed they are a 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 runtime 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;
Related Tasks