S4 - 5 One-Pass Macro Processor. will process macro definitions and expand macro invocations at the same time; will alternate between macro definition and macro expansion. the definition of a macro must come before any of its invocations Macro Processor Data Structures definition table DEFTAB DEFTBL - macro definitions are stored in this table - references to parameters are converted to a positional notation for efficiency in substituting arguments
S4 - 6 name table NAMTAB NAMTBL - macro names are stored in this table along with pointers to the beginning and end of definitions in DEFTAB; serves as an index to DEFTAB argument table ARGTAB ARGTBL - used during the expansion of macro invocations - arguments in a macro invocation are stored in this table Macro Processor Logic pages of text Figure 4.5 Algorithm for a one-pass macro processor
S4 - 7 Section 4.2 Machine-Independent Macro Processor Features Generation of Unique Labels MAC MACRO LBL MEND first macro expansion AALBL second macro expansion ABLBL...
S4 - 8 Conditional Macro Expansion IF ( Boolean expression )... ELSE... ENDIF WHILE ( Boolean expression )... ENDW
S4 - 9 Section 4.3 Macro Processor Design Options Recursive Macro Expansion Problems with previous macro processor - when a macro invokes another macro ARGTAB is overwritten - when the second macro finishes EXPANDING is set to FALSE
S Solution - if macro processor is written in a high-level language that supports recursion, then there won't be problems since the compiler saves the previous values of variables on recursive calls - if macro processor must be written in a programming language that does not support recursion, then the system programmer must save the data values (this can be done using a stack)
S General-Purpose Macro Processors not dependent on any particular programming language; can be used with a variety of different languages Problems - problems related to different programming languages. comments. facilities for grouping. tokens (identifiers, operators,...) - syntax used for macro definitions and macro invocations
S Macro Processing within Language Translators previous macro processor: pre-processor alternative: combining the macro processing functions with the language translator. line-by-line macro processor. integrated macro processor