System Programming and Operating Systems Lab
ASSIGNMENT A3
1 Aim:
To design suitable data structure and implement pass-1 of two pass macro-processor using OOP
features in Java.
2 Objectives:
To design pass-1 of two pass macro-processor.
3 Theory:
A macro processor is a program that copies a stream of text from one place to another, making
a systematic set of replacements as it does so. Macro processors are often embedded in other
programs, such as assemblers and compilers. Sometimes they are standalone programs that can
be used to process any kind of text.
A macro instruction (macro) is a notational convenience for the programmer. It allows the
programmer to write shorthand version of a program (module programming). A Macro represents
a commonly used group of statements in the source programming language.A macro instruction
(macro) is a notational convenience for the programmer. It allows the programmer to write
shorthand version of a program (module programming)
The macro processor replaces each macro instruction with the corresponding group of source
language statements (expanding) Normally, it performs no analysis of the text it handles.It does
not concern the meaning of the involved statements during macro expansion. The design of a
macro processor generally is machine independent.
Two new assembler directives are used in macro definition
MACRO: identify the beginning of a macro definition
MEND: identify the end of a macro definition
Usually in the Pass 1 of the two pass Macro-Processor,Macros are defined and in the second
pass of the macro-processor,macros are expanded.Data Structures required in the Pass 1 of the
1
Macro-Processor are as follows:-
1. Input Source File
2. Intermediate File
3. MNT (Macro Name Table)
4. MDT (Macro Defination Table)
5. MNT Pointer
6. MDT Pointer
4 Algorithm:
1. begin macro processor
2. EXPANDING : = FALSE
3. while OPCODE END do
4. begin
5. GETLINE
6. PROCESSLINE
7. end while
8. end macro processor
9. procedure PROCESSLINE
10. begin
11. search MNT for OPCODE
12. if found then
13. EXPAND
14. else if OPCODE = MACRO then
15. DEFINE
16. else write source line to expanded file
17. end PROCESSLINE
5 Flowchart:
6 Code:
;− − − − − − − − − − − −
i mpo rt j a v a . i o . ∗ ;
i mpo rt j a v a . l a n g . S t r i n g ;
i mpo rt j a v a . u t i l . S c anne r ;
p u b l i c c l a s s M a c ro Pro c e s s o r
{
s t a t i c i n t mdtp=0 ,mntp=0;
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) thro w s E x c e p ti o n
{
F i l e f=new F i l e ( ” program . t x t ” ) ;
S c anne r s=new S c anne r ( f ) ;
Strin g tokens [ ] ; // Array f o r s t o r i n g t o k e n s g e n e r a t e d
S t r i n g MDT Def , m a c r o d e f l i n e ;
MNT[ ] mnt obj=new MNT[ 5 ] ; // Array o f o b j e c t s f o r MNT
MDT[ ] mdt obj=new MDT[ 5 ] ; // Array o f o b j e c t s f o r MDT
S t r i n g [ ] ALA; // Data member o f MNT C l a s s
w h i l e ( s . has N e x tL i ne ( ) )
{
m a c r o d e f l i n e=s . n e x t L i n e ( ) ; // re a d l i n e i n m a c r o d e f l i n e v a r i a b l e
t o k e n s=m a c r o d e f l i n e . s p l i t ( ” ” ) ; // s p l i t th e l i n e on s p a c e
f o r ( i n t i =0; i <t o k e n s . l e n g t h ; i ++)
{
i f ( t o k e n s [ i ] . e q u a l s ( ”MACRO”) ) // i f macro
{
//− − − − − MNT − −−
mnt obj [ mntp]=new MNT( mdtp , mntp ) ; // c r e a t e MNT o b j e c t
mnt obj [ mntp ] . name=t o k e n s [ i + 1 ] ; // name o f macro
// mnt obj [ mntp ] . p r i n t ( ) ; // p r i n t MNT Co nte nts
//− − − − − − − − − − MDT −−−−−−−−−−−−−−−−−−−−−
MDT Def=m a c r o d e f l i n e ; // s t o r e l i n e i n MDT Def
while ( true ) // re a d u n t i l MEND not found
{
S t r i n g x=s . n e x t L i n e ( ) ; // re a d macro d e f i n t i o n l i n e by l i n e
i f ( x . e q u a l s ( ”MEND” ) )
{
MDT Def=MDT Def . c o n c a t ( x ) ; //MDT a l s o c o n t a i n s MEND s ta te m e n t
mdtp++; // i n c r e m e n t mdtp f o r ne x t macro d e f i n i t i o n
bre ak ;
}
MDT Def=MDT Def+”\ n ” ; // add new l i n e to MDT c o n t e n t s
MDT Def=MDT Def . c o n c a t ( x ) ; // put ne x t s ta te m e n t o f macro d e f i n i t i o n
mdtp++;
i f (MDT Def . c o n t a i n s ( ”X” ) ) // R e p l ac e argument wi th #1 , # 2 . . . . . .
MDT Def=MDT Def . r e p l a c e A l l ( ”X” , ” #0 ” );
i f (MDT Def . c o n t a i n s ( ”Y” ) )
MDT Def=MDT Def . r e p l a c e A l l ( ”Y” , ” #1 ” );
MDT Def=MDT Def . r e p l a c e F i r s t (”#0” ,”X” ) ; // Macro name l i n e c o n t a i n s p a r a m e te r s a
is
MDT Def=MDT Def . r e p l a c e F i r s t (”#1” ,”Y” ) ;
mdt obj [ mntp]=new MDT(MDT Def ) ; // c r e a t e mdt o b j e c t and s t o r e c o n t e n t s
// mdt obj [ mntp ] . p r i n t ( ) ; // p r i n t mdt c o n t e n t s
//− − − − ALA− − − −
ALA=t o k e n s [ i + 2 ] . s p l i t ( ” , ” ) ; // arguments a t 3 rd i n d e x o f to k e n a r r a
mnt obj [ mntp ] . setALA (ALA ) ; // s e t ALA f o r eac h macro
mntp++;
}
}
p r i n t ( mnt obj , mdt obj ) ;
s.close();
}
s t a t i c v o i d p r i n t (MNT[ ] obj ,MDT[ ] o b j 1 )
{
System . out . p r i n t l n(”− − − − −MNT−−−−−−−−”);
f o r ( i n t i =0; i <mntp ; i ++)
obj [ i ] . p r i n t ( ) ;
System . out . p r i n t l n ( ) ;
System . out . p r i n t l n(”− − − − − MDT−−−−−−−− ”);
f o r ( i n t i =0; i <mntp ; i ++)
obj1[i].print();
System . out . p r i n t l n ( ) ;
System . out . p r i n t l n(”− − − − ALA−−−−−−−− ”);
f o r ( i n t i =0; i <mntp ; i ++)
o b j [ i ] . printALA ( ) ;
System . out . p r i n t l n ( ) ;
c l a s s MNT
{
i n t no , a d d r e s s ;
S t r i n g name ;
S t r i n g [ ] ALA;
MNT( i n t mdtp , i n t mntp )
{
no=mntp ;
i f ( mntp==0)
a d d r e s s=mdtp ; // F i r s t Macro w i l l be a t 0 th p o s i t i o n i n MDT
else
a d d r e s s=mdtp+1;
name=””;
}
publicvoidprint()
{
System . out . p r i n t ( t h i s . no + ” \t”+ t h i s . name + ” t ”\ + t h i s . a d d r e s s ) ;
System . out . p r i n t l n ( ) ;
}
p u b l i c v o i d setALA ( S t r i n g [ ] o b j )
{
t h i s .ALA=o b j ;
}
p u b l i c v o i d printALA ( )
{
System . out . p r i n t l n ( ” 0 ” + t h i s .ALA [ 0 ] ) ;
System . out . p r i n t l n ( ” 1 ” + t h i s .ALA [ 1 ] ) ;
c l a s s MDT
{
String def ;
MDT( S t r i n g d e f i n t i o n )
{
t h i s . d e f=d e f i n t i o n ;
}
publi c void pri nt ()
{
System . out . p r i n t ( t h i s . d e f ) ;
System . out . p r i n t l n ( ) ;
}
}
7 Output:
8 Conclusion:
Through this assignment we understood the working of Pass 1 of Macro-Processor in which the
macro is defined in the form of 2 major data structures which are MNT(Macro Name Table) and
MDT(Macro Defination Table).