4.3Macro Processor Design Options

Share Embed Donate


Short Description

4.3Macro Processor Design Options...

Description

4.3Macro processor design options 1. 2.

3.

Recursive macro expansion General-purpose macro processors Macro processing within language translators

Recursive macro expansion z

It is desirable to allow macro invocation statements appearing within the body of a macro instruction.

2

1

Example of nested macro invocation Read characters into a buffer

Read a character

3

Can the algorithm shown in Fig.4.5 handle nested macro invocation correctly? OPCODE= yes END ‘END’?

GETLINE

EXPANDING?

yes

Get next line from DEFTAB

Substitute arguments from ARGTAB

Get next line from input file

END

no no

GETLINE

END PROCESSLINE

yes Setup ARGTAB

EXPAND

End of Macro Definition?

no

GETLINE PROCESSLINE

PROCESSLINE

END yes

Search NAMTAB for OPCODE

DEFINE

Enter Macro name into NAMTAB

MACROMEND pair LEVEL=0?

no

GETLINE

EXPAND Found? no

yes OPCODE= ‘MACRO’

yes

DEFINE

END

Enter into DEFTAB

Substitute positional notations for parameters

no

4

2

Algorithms (Fig. 4.5)

for MACRO-MEND pairs

5

Algorithms (Fig. 4.5)

GETLINE GETLINEreads readsaaline lineeither eitherfrom frominput inputfile fileoror from fromDEFTAB. DEFTAB.

6

3

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.

7

ARGTAB Paremeter

Value

1

BUFFER

2

LENGTH

3

F1

4

(unused)

.

.

Paremeter

Value

1

F1

2

(unused)

.

.

4

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.

9

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 generalpurpose macro are greater than those for developing a language-specific processor, the expense does not need to be repeated for each language.



Savings in software maintenance effort. 10

5

Difficulties in developing generalpurpose 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.

11

Difficulties in developing generalpurpose 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 for identifying comments.

• e.g. • • • • • •

C: /* */ C++: //, /* */ Pascal: {}, (* *) Fortran: C Basic: REM Ada: --

• Therefore, the user must provide some rules for the macro processor to recognize comments. 12

6

Difficulties in developing generalpurpose 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 statement formatting convention such as those found in FORTRAN and COBOL. 13

Difficulties in developing generalpurpose 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 programming language.

14

7

Macro processing within language translators z

The macro processor that we have discussed so far might be called preprocessors.

• • z

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.

Alternatively, the macro processing functions can be combined with the language translator.

• •

line-by-line macro processors integrated macro processors 15

Line-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), instead of being written to an expanded source file. 16

8

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.





Many utility subroutines and functions can be used by both the language translator and the macro processor.





e.g. OPTAB in an assembler and NAMTAB in the macro processor can be combined.

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. 17

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



It is difficult to handle by using an ordinary macro processor.

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.

18

9

Disadvantages of line-by-line and integrated macro processors z

z

z

They must be specially designed and written to work with a particular implementation of an assembler or compiler. The costs of macro processor development must be added to the cost of the language translator. The assembler or compiler will be larger and more complex than it would be if a macro preprocessor were used. 19

10

View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF