Algorithm for One-pass Macro Processor

Download Report

Transcript Algorithm for One-pass Macro Processor

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
Prof. Weider Yu
1
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;
/* &param_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
Prof. Weider Yu
2
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
Prof. Weider Yu
3
One-pass Macro Processor Algorithm

2
A flow chart for the one-pass macro processor algorithm.
READ (next line)
INPUT FILE
2
1
MACRO
READ (next line)
GETLINE
DEFTAB
PROCESSOR
5
SUBSTITUTE (arguments)
PROCESSLINE
ARGTAB
DEFINE
ENTER (macro prototype/line)
WRITE (source line)
EXPANDED FILE
4
3
SEARCH
SET UP (arguments)
5
5
EXPAND
NAMTAB
CmpE 220 - Software Systems
Prof. Weider Yu
ENTER (macro name)
4