70 likes | 1.26k Views
Algorithm for One-pass Macro Processor . main () /* main program */ { EXPANDING = 0; /* definition case ; a control variable for reading input record from input sources */ while (OPCODE != ‘END’) {
E N D
Algorithm for One-pass Macro Processor main () /* main program */ { EXPANDING = 0; /* definition case; a control variable for reading input record from input sources */ while (OPCODE != ‘END’) { GETLINE (line); /* get a next line (from expansion or definition); label, opcode, operands */ PROCESSLINE (line); /* define or expand a line after getting it; label, opcode, operands*/ } } PROCESSLINE (line) { search NAMTAB for line.opcode; if (found) EXPAND (line); /* expand macro definition with arguments from invocation */ else if DEFINE (line); /* put macro definition in DEFTAB */ else write the source line to expand file; } CmpE 220 - Software Systems
Algorithm for One-pass Macro Processor DEFINE (line) { enter line.label into NAMTAB; /* enter macro name into NAMTAB */ enter macro prototype into DEFTAB; /* the first declaration statement */ LEVEL = 1; while (LEVEL > 0) { GETLINE (line); /* get the next line of the macro definition */ if (line != comment line) { substitute positional notation for parameters; /* ¶m_1 ?1, etc. */ enter line into DEFTAB; if (OPCODE = ‘MACRO’) LEVEL = LEVEL +1; else if (OPCODE = ‘MEND’) LEVEL = LEVEL –1; } } store pointers (label->begin and label->end) in NAMTAB; /* store macro def. begin and end pointers */ } CmpE 220 - Software Systems
Algorithm for One-pass Macro Processor EXPAND (line) { EXPANDING = 1; /* expansion case; ready to read input from DEFTAB*/ get the first line of macro definition (macro prototype) from DEFTAB; set up arguments (from macro invocation) in ARGTAB; write macro invocation to expanded file (output file) as a comment; while (OPCODE != MEND) { /* while not end of macro definition */ GETLINE (line); PROCESSLINE (line); } EXPANDING = 0; /* macro expansion is done */ } GETLINE (line) { if (EXPANDING = 1) { get next line of macro definition from DEFTAB; substitute arguments from ARGTAB for position notation; } else read next line from input file; /* read the source program file */ } } CmpE 220 - Software Systems
GETLINE One-pass Macro Processor Algorithm 2 • A flow chart for the one-pass macro processor algorithm. INPUT FILE READ (next line) 2 1 READ (next line) MACRO PROCESSOR DEFTAB 5 SUBSTITUTE (arguments) 4 PROCESSLINE DEFINE ENTER (macro prototype/line) WRITE (source line) 3 5 5 EXPANDED FILE SEARCH EXPAND ARGTAB NAMTAB ENTER (macro name) SET UP (arguments) CmpE 220 - Software Systems