CSc 231 Assignment 4
|CSc 231 Perl Assignments|
Due: April 30
A macro processor reads a file containing macro definitions and uses, and replaces the uses with the definitions. A macro is a named replacement text, usually with parameters to substitute. For instance, if assignment four reads this input:
frankis defined as the given string, and occurrences of
@frankare replaced with the string. The
@frankidentifier is followed by an argument list. The arguments are separated by commas and replace items of the form
#nnn in the macro definition, where nnn is the number of the parameter, starting from 1.
Operation is essentially line-oriented. Input lines starting
! control the program. There are two types of these (see below).
These are read by the program, obeyed, and do not appear in the output.
All other lines should be printed on standard output, with any
macro calls expanded.
!as the first non-blank character. There are two directives:
define. An include line follows the
!with the word
include, then one or more spaces, then a file name. The program will read read from this file until EOF, then return to reading from the file which included it. If the file will not open, the program should print a message on standard output and continue processing the original file.
The second directive is
define. The keyword follows the
one or more spaces, then the name of the macro, then one or more spaces,
then an equal sign. The remainder of the line following the equal sign
is the definition. This allows the macro definition to have spaces
leading, trailing, or internal. The program remembers the
definition for use on expansion lines. If the macro has already been
defined, this new definition replaces it.
Macro names follow the C identifier rules: They must start with a letter or underscore, and proceed with letters, underscore, or digits. Names are case-sensitive.
If a line starts with
! as the first non-blank, but he directive
name is not recognized, or the line does not have the right form,
print an error message and discard the input line.
!is printed on standard output after macro expansion. Macro expansion follows these rules.
@, followed immediately by a defined macro name, followed immediately by a list of arguments enclosed in parentheses. The argument list must be present, even if it is empty. Anything that sort of looks like a macro use, but does not follow these rules, is not a macro use. That means the text is simply output unchanged. This includes uses of undefined macro names, or when the parameter list or the
@fred. Note that this rule depends on parenthesis nesting, even if the parens are not part of another invocation.
Note that parameters may be empty, and may contain spaces.
#nnn, where nnn is a positive integer, is replaced with the nnn-th argument, numbering starting from 1. If there are not that many arguments,
#nnn is replaced with the empty string.
#*is replaced with all the arguments, separated by commas.
#-is replaced with all the arguments but the first one, separated by commas.
@ALICEis expanded first, producing:
@JOEis expanded giving