4.3Macro processor
design options
1.
Recursive macro expansion
2.General-purpose macro
processors
3.
Macro processing within
language translators
2
Recursive macro expansion
z
It is desirable to allow macro invocation
statements appearing within the body of
a macro instruction.
3
Example of nested macro
invocation
Read characters
into a buffer
Read a character
Can the algorithm shown in Fig.4.5 handle
nested macro invocation correctly?
EXPANDING? Get next line
from DEFTAB
Get next line from input file
Substitute arguments from ARGTAB yes no END GETLINE Search NAMTAB for OPCODE PROCESSLINE EXPAND End of Macro Definition? no PROCESSLINE GETLINE Setup ARGTAB END yes DEFINE MACRO-MEND pair LEVEL=0? no GETLINE Enter Macro name into NAMTAB END yes OPCODE= ‘END’? no PROCESSLINE GETLINE END yes
5
Algorithms (Fig. 4.5)
for MACRO-MEND pairs
6
Algorithms (Fig. 4.5)
GETLINE reads a line either from input file or from DEFTAB.
GETLINE reads a line either from input file or from DEFTAB.
7
The algorithm shown in Fig.4.5 cannot
handle nested macro invocations correctly
z The procedure EXPAND should be redesigned.•
If EXPAND processes a nested macro invocation, the ARGTAB for the original macro invocation would be lost.•
At the end of expansion for a nested macro invocation, the flag EXPANDING would be set to FALSE. Thus, the procedure GETLINE would not read the next line from the DEFTAB of the original macro invocation. . . (unused) 4 F1 3 LENGTH 2 BUFFER 1 Value Paremeter ARGTAB . . (unused) 2 F1 1 Value Paremeter9
Solution
z
Using recursive calls.
•
The compiler would be sure that previous
values of any variables declared within a
procedure were saved when that procedure
was called recursively.
•
It would also take care of other details
involving return from the procedure.
10
General-purpose macro processors
z
The advantages of a general-purpose
approach to macro processing
•
Savings in the time and expense involved in training.•
The programmer does not need to learn about a different macro facility for each compiler or assembler language.•
Savings in overall software development cost.•
Although the costs involved in producing a general-purpose macro are greater than those for developing a language-specific processor, the expense does not need to be repeated for each language.11
Difficulties in developing
general-purpose macro processors
z
The large number of details that must be
dealt with in a real programming
language
•
A special-purpose macro processor can have
these details built into its logic and structure.
•
A general-purpose macro processor must
provide some way for a user to define the
specific set of rule to be followed.
Difficulties in developing
general-purpose macro processors
z
Normal macro parameter substitution should
not occur in some situations
•
Comments should be ignored by a macro processor.•
Each programming language has its own methods foridentifying comments.
•
e.g.•
C: /* */•
C++: //, /* */•
Pascal: {}, (* *)•
Fortran: C•
13
Difficulties in developing
general-purpose macro processors
z The problem involves the tokens of the programming language.
•
identifiers,constants,keywords,operators•
In some languages, there are multiple-character operators such as ** in FORTRAN and := in Pascal.•
Problems may arise if these are treated by a macro processor as two separate characters rather than as a single operator.•
Arrangement of the source statements in the input file may create difficulty.•
The macro processor must be concerned with whether or not blanks are significant, with the way statements are continued from one line to another, and with special statementformatting convention such as those found in FORTRAN and COBOL.
14
Difficulties in developing
general-purpose macro processors
z
The syntax used for macro definition and
macro invocation statements should be
similar in form to the source
programming language.
•
It tends to make the source program easier to
read and write if macro invocations are similar
in form to statements in the source
15
Macro processing within
language translators
z
The macro processor that we have discussed
so far might be called
preprocessors
.
•
They process macro definitions, expand macro invocations, and produce an expanded version of the source program.•
This expanded program is then used as input to an assembler or compiler.z
Alternatively, the macro processing functions
can be combined with the language translator.
•
line-by-line macro processors•
integrated macro processorsLine-by-line macro processor
z
The macro processor operate as a sort
of input routine for the assembler or
compiler.
•
The macro processor reads the source
program statements and performs all of its
function as previously described.
•
The output files are passed to the language
translator as the are generated (one at a time),
17
Line-by-line macro processor
z Advantages•
It avoids making an extra pass over the source program, so it can be more efficient than sing a macro processor.•
Some of the data structures required by the macro processor and the language translator can be combined.•
e.g. OPTAB in an assembler and NAMTAB in the macro processor can be combined.•
Many utility subroutines and functions can be used by both the language translator and the macro processor.•
operations such as • scanning input lines• searching tables
• converting numeric values from external to internal representations
•
It makes it easier to give diagnostic messages that are related to the source statement containing the error (i.e., the macro invocation statement)•
With a macro preprocessor, such an error might be detected only in relation to some statement in the macro expansion.18
Integrated macro processor
z An integrated macro processor can potentially make use
of any information about the source program that is extracted by the language translator.
•
The macro processor can use the tokens scanned by the translator.•
DO 100 I = 1,20•
DO 100 I = 1•
DO100I=1•
The macro processor can support macro instructions that depend upon the context in which they occur.•
e.g. A macro could specify a substitution to be applied only to variables or constants of a certain type, or only to variables appearing as loop indices in DO statements.It is difficult to handle by using
an ordinary macro processor.
19
Disadvantages of line-by-line and
integrated macro processors
z
They must be specially designed and written to
work with a particular implementation of an
assembler or compiler.
z
The costs of macro processor development
must be added to the cost of the language
translator.
z