CSc 231 Assignment 4 | |
CSc 231 Perl Assignments |
70 pts |
Macrophilia |
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:
frank
is defined as the given
string, and occurrences of @frank
are replaced with the string.
The @frank
identifier 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.
STDOUT
.
Operation is essentially line-oriented. Input lines starting
with !
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: include
and 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 !
, then
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 @
is missing.
@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.
@ALICE
is expanded first, producing:
@JOE
is expanded giving
@if(
a,
b,
eb,
neb)