Sunteți pe pagina 1din 44

Macro Processor

Chapter-1
Introduction
1.1. Problem Definition
Processing of source code for a compiler requires many tasks before actual
compilation of the program. Most of theses tasks require refinement of code for the machine
to understand. Programmers may write code to the best possible extent of their
understanding and simplicity. Therefore, thisrequires a translation to more extensive code that
the compiler can understand. Some of the problems faced by programmers in writing
extensive code are explained indetail in the next few paragraphs.

1.1.1 Code Readability:


Very often while writing large chunks of code, there may be many places where code is
repeated. Repetition of code can lead to confusion and reduce the readability of code.
Maintaining readability is one of the main essences of proper programming. It also is the
most difficult oftasks. Even with sufficient comments and spacing, it can still be hard to
maintain readability. Furthermore, it is hard for humans to understand the same level of
instructions as programming languages. The balance between human understanding and
mnemonics is often blurred. It would be feasible for the programmer to replace large chunks
of repeated code with a single code that has the same meaning. This would improve
readability to a great extent and help with debugging purposes too. However, replacement
text can sometimes cause more confusion if the programmer does not properly handle them.

1.1.2 Function Overhead:


Functions help solve this problem by sorting relevant code into one area. A program in
sequence can call sucha function, make it perform its task and return control to the line
following the function call. However, functions (or methods) have a large overhead and can
waste valuable processor time by repeatedly calling the same function. In fact there is nothing
wrong with repeated code for the compiler. If the program execution requires repeated
execution of codes, then functions will cause a significant loss of performance, especially if
Dept of ISE, RVCE

2014-15

Page 1

Macro Processor
the functions carry small amounts of code in them. Therefore, functions should not be usedto
hold replacement code, but to hold relevant code that needs to be called only when required.

1.1.3 Increased Processor Time:


As already mentioned, repeated codes and calls for replacement can cause considerable
processor wastage. Processing time is an important component of any system software.
Applications can afford to suffer from processing time, but if system software largely suffers
from processing time, then the whole setup is affected. Macros may increase compilation
time by replacing text, including more text and submitting partial parts for compilation, but
they help in producing more reliable code.

1.1.4 Compilation Issues:


To increase readability of code, often, methods are written in various files and included
wherever necessary. Unfortunately, these individual files cannot be compiled and then
included during linking and execution. Therefore, this approach cannot be used unless macros
are used to include the text from these files in the main source code file. This improves
readability, reliability and debugging of the entire project.

1.2 Basic Definitions


1.2.1 Simple Callable Macros:
Macros must first be defined, before they can be called in a program. A simple macro
definition consists of the macro name, which can be defined with the keyword MACRO, the
macro body, and a final ENDM (end macro) instruction.
<macro name> MACRO
<body line 1>
<body line 2>
.
.
<body line m>
ENDM
Dept of ISE, RVCE

2014-15

Page 2

Macro Processor

The macro name must be a valid, unique symbol. It cannot be redefined later. Keywords
cannot be used as macro names. The macro body may comprise any number of lines. Body
lines may be all kinds of assembler instructions, pseudo instructions, controls, meta
instructions, macro calls and even further macro definitions.
The macro body and the whole macro definition is terminated with the ENDM instruction.
Macros must be defined, before they can be called. Forward references to macros are not
allowed. Once defined, a macro can be called by its name in the subsequent program as often
as desired. Whenever a macro is called, the macro body will be "inserted" into the program
and then assembled as normal source lines. This process is called macro expansion.

1.2.2 Macro Parameters


Callable macros may have parameters, to allow more flexible use. The names of the formal
parameters are specified in the macro definition behind the keyword MACRO, separated by
commas. All parameter names of a macro must be different, valid symbols. Keywords cannot
be used as parameter names. Macros may have any number of parameters, as long as they fit
on one line. Parameter names are local symbols, which are known within the macro only.
Outside the macro they have no meaning!
<macro name> MACRO <parameter 1>, <parameter 2>, ... ,<parameter n>
<body line 1>
<body line 2>
.
.
<body line m>
ENDM
When called, actual arguments can be passed to the macro. The arguments must be separated
by commas. Valid macro arguments are
1. arbitrary sequences of printable characters, not containing blanks, tabs, commas, or
semicolons
2. quoted strings (in single or double quotes)
Dept of ISE, RVCE

2014-15

Page 3

Macro Processor
3. single printable characters, preceded by '!' as an escape character
4. character sequences, enclosed in literal brackets < ... >, which may be arbitrary
sequences of valid macro arguments (types 1. - 4.), blanks, commas and semicolons
5. arbitrary sequences of valid macro arguments (types 1. - 4.)
6. expressions preceded by a '%' character

1.2.3 Macro Operators


There are some special control characters, which are very useful for macro definition, call
and expansion:
Normally, comments in body lines are also contained in the
;;

Macro
commentary

expanded lines. If a commentary begins with ';;' however, it is


not stored during macro definition. Therefore, it doesn't
consume memory space, and appears in the list file in the
macro definition only, but not in the expanded lines.
If the escape character '!' precedes another printable character
in a macro argument, the assembler is forced to treat that

Literal operator

character literally. This means it will be passed to the macro,


even if it is a control character, while the literal operator itself
is removed.
If a macro argument is intended to contain separation or
control characters, it must be enclosed in literal brackets < ... >

<>

Literal brackets

to pass it to the macro as one argument string, while the


outermost pair of brackets is removed. Literal brackets can be
nested to any depth.

Evaluation

If a macro argument is preceded by the evaluation operator '%',


it is interpreted as an expression, which will be evaluated
before it is passed to the macro. The actual argument string
will not be the expression itself, but a decimal ASCII
representation of its value. The expression must be known

Dept of ISE, RVCE

2014-15

Page 4

Macro Processor
on pass 1.
The '&' character separates parameter names (local symbols)
from surrounding text. Outside quoted strings and commentary
it serves only as a general separation character. This applies
always when a local symbol directly precedes or follows
another alphanumeric string. Inside quoted strings and
&

Substitution

commentary, a local symbol must be preceded by '&' if it is to


be substituted there. During every macro expansion, the
assembler removes exactly one '&' from every sequence of '&'
characters. This allows for example, to define a nested macro
inside a macro body, which also uses the substitution operator
'&': one writes simply '&&'!

1.2.4 Nested Macro Definitions


A macro body may also contain further macro definitions. However, these nested macro
definitions aren't valid until the enclosing macro has been expanded! That means, the
enclosing macro must have been called, before the nested macros can be called.
Example 1:
A macro, which can be used to define macros with arbitrary names, may look as follows:
DEFINE MACRO MACNAME
MACNAME MACRO
DB 'I am the macro &MACNAME.'
ENDM
ENDM

1.2.5 Nested and Recursive Macro Calls


Macro bodies may also contain macro calls, and so may the bodies of those called macros,
and so forth. If a macro call is seen throughout the expansion of a macro, the assembler starts
immediately with the expansion of the called macro. For this, its its expanded body lines are
simply inserted into the expanded macro body of the calling macro, until the called macro is
completely expanded. Then the expansion of the calling macro is continued with the body
line following the nested macro call.
Dept of ISE, RVCE

2014-15

Page 5

Macro Processor

1.3. Block Diagram


The following is a block representation of the various actions that will take
place during macro expansion.

1.a Block representation of macro expansion

Chapter-2
Theory and Concepts of Macro Processor

Dept of ISE, RVCE

2014-15

Page 6

Macro Processor

2.1 Introduction
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. The design of a macro processor generally is machine independent.In this project,a
macro processor which analyses the given macro lines into tokens of label,opcode,operand
and stored seperately.It is processed to produce the expanded output.Our approach is to build
a single pass macro processor.

2.2 Basic concepts of Macro processor


A macro processor is a program that reads a file (or files) and scans them for certain
keywords. When a keyword is found, it is replaced by some text. The keyword/text
combination is called a macro.

Assembler directives are used in macro definition are


1)MACRO: identify the beginning of a macro definition.
2)MEND: identify the end of a macro definition.

Prototype for the macro


Each parameter begins with &
name MACRO parameters
:
body
:
MEND
Basic Macro Processor Functions
Macro definition: It defines how to expand a single language statement or computer
instruction into a number of instructions.
Macro Expansion: A macro call leads to macro expansion. During macro expansion, the
macro statement is replaced by sequence of assembly statements. Argumentsfrom the macro
invocation are substituted for the parametersin the macro prototype (according to their
positions).
Dept of ISE, RVCE

2014-15

Page 7

Macro Processor

2.3 Types of Macro Processor


There are two types of macroprocessor :-

1) Two

pass Macroprocessor.
2) One pass Macroprocessor
.

2.3.1 Two pass Macro processor


In this type,1 pass macro definations are taken care and in 2 pass they are expanded to
produce output But, it will not allow the body of one macro to contain definitions of other
macros. Nested definition of macros is useful some time.
Pass I
Examine every operation code
Save all macro definitions in a MDT( Macro Definition Table)
Save a copy of the input text, minus macro definitions on secondary storage for
use in pass II
Prepare a Macro Name table(MNT)
Pass II
Examine every string in program
Replace each macro name with the appropriate text from the macro definition.

2.3.2 One pass Macro processor


one-pass macro processor that alternate between macro definitionand macro
expansionin a recursive way is able to handle recursive macro definition.
Restriction
The definition of a macro must appear in the source program before any statements that
invoke that macro.

Flowchart:

Dept of ISE, RVCE

2014-15

Page 8

Macro Processor
Pass 1
MDTC<-1
Rea
M
MNTC<-1
d
A
next
C
Writ
sour
R
e
E
ce
O
cop
N
card
ps
y of
Rea
D
Enter
eu
sour
d
ps
macro
do
ce
next
eu
name
Sub
Rea
card
sour
do
Go
and
stitu
d
op
ce
to
current
te
next
?
card
op
Pas
value of
inde
sour
s?2
MDTC in
x
ce
MNT
nota
MNTC <- MNTC + 1
card
entry
tion
M
Enter
Prepare argument
number list array
Enter line
for
Einto MDT
macro
MNTC
argu
N
name
MDTC <- MDTC + 1
men
D
card into
MDTC <- MDTC + 1
ts
ps
MDT
eu
do
op
?

Fig 2.a flowchart of 1 pass macro processor

Data Structure
DEFTAB (definition table)
1) To Stores the macro definition includes prototype and macro body.
2) Comment lines are omitted.
3) References to the macro instruction parameters are converted to a positional
notation for efficiency in substituting arguments.

Dept of ISE, RVCE

2014-15

Page 9

Macro Processor
NAMTAB
1) Stores macro names.
2) Serves as an index to DEFTABby pointing to the beginning and the end of
the macro definition (DEFTAB)
ARGTAB(argument Table)
1) Stores the arguments of macro invocation.
2) As the macro is expanded, arguments from ARGTAB are substituted for the
corresponding parameters in the macro body.

Chapter-3
Software Requirements Specification
Dept of ISE, RVCE

2014-15

Page 10

Macro Processor

2.1 Hardware and Software Requirements


The hardware and software requirements for this project are limited to only the requirements
of a C compiler on a UNIX or Windows machine.
Hardware Requirements
1. Pentium 60 MHz or higher.
2. 256 MB RAM or higher.
3. 10 MB hard-disk space free (not including swap space required for the OS)
Software Requirements
1. DEV C++ IDE.
2. Windows 98 or later.
3. Implementable on UNIX OS with minor adjustments

2.2 Description of Tools used


Dev C++ : Bloodshed Dev-C++ is a full-featured Integrated Development
Environment (IDE) for the C/C++ programming language. It uses Mingw port of GCC (GNU
Compiler Collection) as its compiler. It creates native Win32 executables, either console or
GUI. Dev-C++ can also be used in combination with Cygwin.

Chapter-4

HIGH LEVEL DESIGN


Design Constraints
Dept of ISE, RVCE

2014-15

Page 11

Macro Processor
The design of macro processor doesnt depend on the architecture of the machine.
We will be studying some extended feature for this macro processor. These features are:
Concatenation of Macro Parameters
Generation of unique labels
Conditional Macro Expansion
Keyword Macro Parameters

4.1 Concatenation of unique labels:


Most macro processor allows parameters to be concatenated with other character
strings. Suppose that a program contains a series of variables named by the symbols
XA1, XA2, XA3,, another series of variables named XB1, XB2, XB3,, etc. If similar
processing is to be performed on each series of labels, the programmer might put this as a
macro instruction. The parameter to such a macro instruction could specify the series of
variables to be operated on (A, B, etc.). The macro processor would use this parameter to
construct the symbols required in the macro expansion (XA1, Xb1, etc.).

4.2 Generation of Unique Labels


It is not possible to use labels for the instructions in the macro definition, since every
expansion of macro would include the label repeatedly which is not allowed by the
assembler. This in turn forces us to use relative addressing in the jump instructions. Instead
we can use the technique of generating unique labels for every macro invocation and
expansion. During macro expansion each $ will be replaced with $XX, where xx is a twocharacter alphanumeric counter of the number of macro instructions expansion.

4.3 Conditional Macro


Conditional if and conditional while is used in macro expansion. This capability adds
greatly to the power and flexibility of amacro language.
Macro processor directives: IF-ELSE-ENDIF, WHILE-ENDW.
The testing of Boolean expression in IF statements occurs at the time macros are expanded.

Dept of ISE, RVCE

2014-15

Page 12

Macro Processor
By the time the program is assembled, all such decisions havebeen made. There is only one
sequence of source statements during program execution.

Macro time variable


It begins with &but is not a macro instruction parameter. Can be used to store
working values during the macro expansion

4.4 Keyword Macro Parameters


All the macro instruction definitions used positional parameters. Parameters and arguments
are matched according to their positions in the macro prototype and the macro invocation
statement. The programmer needs to be careful while specifying the arguments. If an
argument is to be omitted the macro invocation statement must contain anull argument
mentioned with two commas.
Positional parameters are suitable for the macro invocation. But if the macro
invocation has large number of parameters, and if only few of the values need to be used in a
typical invocation, a different type of parameter specification is required

Keyword parameters
Each argument value is written with a keyword that names the corresponding parameter.
Arguments may appear in any order. Null arguments no longer need to be used.It is easier to
read and much less error-prone than the positional method.

4.5 Language
C programming language is used.

4.6 Future plans


Macro processor has main application in these areas:
Language extension, Language translation, Text generation, Systematic editing.
It can be used as a translator, Given a language A and a language B, together with some rules
for translating from A to B, then an application for a macro processor might be to translate
from A to B. Clearly this goal can only be realized in a limited number of cases. In particular,
macros are not suitable for translating between natural languages, since this area requires
very specialised techniques. However, taking A to be a high-level programming language and

Dept of ISE, RVCE

2014-15

Page 13

Macro Processor
B to be the assembly language for some machine, then it might be possible for a macro
processor to perform the compilation from A to B.

4.7 Data flow diagram


Level 0:
It is initial level of data flow diagram, scource code is taken as input and expanded code is
produced which is passed as input to assembler.

Level 1 :
Here, source code is broken into tokens and passed into identify module.It checks whether
the opcode is Macro or it is defined in the code.and passed to substitute module,where it
expands or replaces macro as identified by previous module.

Level 2:

Dept of ISE, RVCE

2014-15

Page 14

Macro Processor

Source code is processed into word scanner,each word or a macro line is checked by
processline.It searches NAMTAB for opcode,or MACRO opcode else expand it.Define enters
defination and prototype into DEFTAB and NAMTAB respectively.GETLINE substitute
actual arguments with formal parameters.and EXPAND produces expanded output.

Chapter-5
DETAILED DESIGN
This chapter discuss about the data structure and control flow in the software with much more
details about software modules by clarifying the details about each function with
functionality, purpose, input and output. The decision taken are based on certain design

Dept of ISE, RVCE

2014-15

Page 15

Macro Processor
considerations, constraints and dependencies which will affect the subsequent functioning of
the product.

5.1 Data Structures for Macro processor


DEFTAB (definition table)
1) To Stores the macro definition includes prototype and macro body.
2) Comment lines are omitted.
3) References to the macro instruction parameters are converted to a positional
notation for efficiency in substituting arguments.
NAMTAB
1) Stores macro names.
2) Serves as an index to DEFTABby pointing to the beginning and the end of
the macro definition (DEFTAB)
ARGTAB(argument Table)
1) Stores the arguments of macro invocation.
2) As the macro is expanded, arguments from ARGTAB are substituted for the
corresponding parameters in the macro body.

5.2 Data Flow Diagram of an overall system

Dept of ISE, RVCE

2014-15

Page 16

Macro Processor

5.3 Description of each Module


5.3.1 Word Scan:

Functionality: This module is responsible to scan the macro line and process it.

Input: source code with defination before main code.

Output: Tokens produced are passed to processline .

Flow Chart: The Flow Chart for the search module is shown in figure 0.1.1

Dependency: It depends on source code data.

5.3.2 Process line:

Functionality: Searches in the NAMTAB for opcode if found control is transferred to


expand, else if opcode is Macro control is transferred to define,else the line is
transferred to expanded code file.

Input: Tokens.

Output: control is transferred to define, expand, getline.

Flow Chart: The Flow Chart for the search module is shown in figure 0.1.2

Dependency: It depends on word Scanner.


Algorithm:

Dept of ISE, RVCE

2014-15

Page 17

Macro Processor
procedure PROCESSLINE
begin
search NAMTAB for OPCODE
if found then
EXPAND
else if OPCODE = MACRO then
DEFINE
Else write source line to expanded file
End (PROCESSLINE)

5.3.3 Define:
.

Functionality: Enters the Macro name into NAMTAB and prototype into DEFTAB,
comment lines are neglected. Care is taken if a Macro is called within a Macro. Hence
prototype is stored in NAMTAB with beginning and end pointer

Input : Macro line which contain MACRO opcode.

Output: completed DEFTAB and NAMTAB.

Flow Chart: The Flow Chart for the search module is shown in figure 0.1.3

Dependency: It depends on processline , it checks opcode is MACRO and transfer


control to define.
Algorithm:
procedure DEFINE
begin
enter macro name into NAMTAB
enter macro prototype into DEFTAB
LEVEL := 1
While LEVEL > 0 do
Begin
GETLINE
If this not a comment line then
Begin
substitute positional notation for parameters

Dept of ISE, RVCE

2014-15

Page 18

Macro Processor
enter line into DEFTAB
if OPCODE = MACRO
LEVEL := LEVEL + 1
else if OPCODE =: MEND then
LEVEL := LEVEL - 1
end (if not comment)
end ( while )
store in NAMTAB pointers to beginning and end of definition
end (DEFINE)

5.3.4 Getline:

Functionality: If Expanding is true, line is taken from DEFTAB and arguments are
substituted for positional notation.

Input: Macro line from DEFTAB


Output: Argument Substituted code..

Flow Chart: The Flow Chart for the search module is shown in figure 0.2.1

Dependency: It depends

on define by taking macro line and substitute with

arguments.
Algorithm:
procedure GETLINE
begin
if EXPANDING then
begin
get next line of macro definition from DEFTAB
substitute arguments from ARGTAB for positional notation
end ( if )
else
read next line from input file
end {GETLINE}

5.3.5 Expand:
Dept of ISE, RVCE

2014-15

Page 19

Macro Processor

Functionality: Expanding is assigned true. Macro prototype is taken from DEFTAB


and macro invocation arguments are copied to ARGTAB. Control is transferred to
getline and processline until end of Macro definition.

Input: Argument Substituted code.

Output: Expanded code.

Flow Chart: The Flow Chart for the search module is shown in figure 0.2.2

Dependency: It depends on getline to get the actual argument substituted code.


Algorithm:
procedure EXPAND
begin
EXPAND := TRUE
Get first line of macro definition (prototype) from DEFTAB
Set up arguments from macro invocation in ARGTAB
Write macro invocation to expand file as comment
While not end of macro definition do
begin
GETLINE
PROCESSLINE
end ( while )
EXPANDING := FALSE
end ( expand )

DEFTAB:
Description: It is used to Stores the macro definition includes prototype and macro
body. Comment lines are omitted.References to the macro instruction parameters are
converted to a positional notation for efficiency in substituting arguments.

NAMTAB:
Dept of ISE, RVCE

2014-15

Page 20

Macro Processor
Description: Stores macro names, Serves as an index to DEFTAB by pointing to the
beginning and the end of the macro definition (DEFTAB)
ARGTAB:
Description:Stores the arguments of macro invocation. As the macro is expanded,
arguments from ARGTAB are substituted for the corresponding parameters in the macro
body.
Expanded code:
Description:Data stored after processed by macro processor. The code is brought to
the state to be processed by compiler or assembler.

Chapter-6
Dept of ISE, RVCE

2014-15

Page 21

Macro Processor

IMPLEMENTATION
6.1 Algorithm
The following functionalities are implemented in our C code for macro processor:
MACROPROCESSOR
EXPANDING=FALSE.
Read each line and call GETLINE() and PROCESSLINE() until END encounters.
PROCESSLINE ( )
If OPCODE is a macroname then EXPAND ( ).
Else if OPCODE is MACRO ,then DEFINE ( ).
Else write line to expanded file as such.
DEFINE( )
Enter Macro name into NMATAB.
Enter macro prototype into DEFTAB.
Set LEVEL=1.
Substitute parameters with positional notations and enter to DEFTAB.
If OPCODE=MACRO, LEVEL++;
If OPCODE=MEND, LEVEL--;
Continue this until LEVEL=0
Store beginning and end of definition as pointers within NAMTAB
EXPAND ( )
EXPANDING = TRUE
Set up arguments from macro invocation in ARGTAB.
Write macro invocation statement to expanded file as a comment line.
Call GETLINE() and PROCESSLINE() till macro definition ends.
Dept of ISE, RVCE

2014-15

Page 22

Macro Processor
Set EXPANDING=FALSE.
GETLINE ( )
If EXPANDING is TRUE, read from DEFTAB (data structure where macro body is
stored) and substitute arguments for positional notations.
If EXPANDING is FALSE , read next line from input file.
UINQUE LABEL GENERATION
a. Labels in the macro body may cause duplicate labels problem if the macro is
invocated and expanded multiple times.
b. Use of relative addressing at the source statement level is very inconvenient, errorprone,
and difficult to read.
c. It is highly desirable to
- let the programmer use label in the macro body
a. Labels used within the macro body begin with $.
- let the macro processor generate unique labels for each macro invocation and expansion.
b. During macro expansion, the $ will be replaced with $xx, where xx is a twocharacter
alphanumeric counter of the number of macro instructions expanded.
c. XX=AA, AB, AC,.

6.2 Language used in implementation


C programming language is used because:
Modularity: modularity is one of the important characteristics of C. we can split the C
program into no. of modules instead of repeating the same logic statements (sequentially).
It allows reusability of modules.
Middle level language: as a middl e level language C combines both the advantages
of low leve l and high leve l languag es. (arrays,pointers etc).

Dept of ISE, RVCE

2014-15

Page 23

Macro Processor
General purpose programming language: C can be used to implement any kind of
applications such as math s oriented, graphics, business oriented applications.
Portability:

we can compile or execute C pro gram in any operating system

(unix,dos,windows).
Powerful programming language: C is very efficient and powerful programming
language, it is best use d for data structures and designing system software.
C is case sensitive language.

6.3 Error Detection


The C programming language provides perror() and strerror() functions which can be used to
display the text message associated with errno.

Chapter-7
SOFTWARE TESTING
Dept of ISE, RVCE

2014-15

Page 24

Macro Processor

At testing stage of this project, defects or errors were discovered by testing individual
program components. Testing was focused on establishing functional requirements and on
system behavior in given scenario. The test cases are selected to ensure that the system
behavior can be examined in all possible combinations of conditions.
Accordingly, expected behavior of the system under different conditions is given. Therefore
test cases are selected which have inputs and the outputs are as expected.

7.1 Testing Process


Testing is an integral part of software development. Testing process, in a way certifies,
whether the product, that is developed, complies with the standards, that it was designed to.
Testing process involves building of test cases, against which the product has to be tested. In
some cases, one drives the test cases from the requirements of the product/software, which is
to be developed.

7.2 Levels of Testing


Different levels of testing are used in the testing process, each level of testing aims to test
different aspects of the system. The basic levels are unit testing, integration testing, system
testing and acceptance testing.

7.3 Unit Testing


The first level of testing is called unit testing. Unit testing focuses verification effort on the
smallest unit of software design module.
In this different modules are tested against the specifications produced during design for the
modules. Unit testing is essentially for verification of the code produces during the coding
phase, and hence the goal is to test the internal logic of the modules. It is typically done by
the parameter of the module. Due to its close association with coding, the coding phase is
frequently called coding and unit testing .The unit test can be conducted in parallel for
multiple modules.

7.4 Integration Testing

Dept of ISE, RVCE

2014-15

Page 25

Macro Processor
The second level of testing is called integration testing. Integration testing deals with finding
defects in the way individual parts work together. In this, many unit tested modules are
combined into subsystems, which are then tested. The goal here is to see if all the modules
can be integrated properly.

7.5 System Testing and Acceptance Testing


Here the entire software system is tested. The reference document for this process is the
requirements document and the goal is to see if the software meets its requirements.
Acceptance testing is sometimes performed with realistic data to demonstrate that the
software is working satisfactorily. Testing here focuses on the external behavior of the
system.

7.6 Unit Testing of Main Modules


Unit testing is the process of testing individual components in the system. This is a defect
testing process so its goal is to expose faults in these components. Individual functions or
methods are the sample type of component and tests are the set of calls to these routines with
different parameters. Here different modules are tested independently and their functionality
and usability is checked. There are 3 test cases for dictionary creation is tested as shown
below Table7.1 to Table 7.3.
Table 7.1 Expansion of MacroTest case 1

Sl. No. of test case : 1


Name of test : Macro expansion
Item / Feature being tested : Macro
Sample Input : Program with macros
Expected output : Program with macros expanded.
Actual output : Program with macros expanded.
Remarks : Test succeeded
Test case 1 as shown in table 7.1 tests the expansion of the macro in the given input file. After
successfully expanding , the program with expanded macros is given as output.
Table 7.2 Generation of unique labels test case 2

Sl. No. of test case : 2


Name of test : Generation of unique labels
Item / Feature being tested : Unique labels
Dept of ISE, RVCE

2014-15

Page 26

Macro Processor
Sample Input :
Expected output :
Actual output :
Remarks :

Macro program with labels


Program with unique labels generated.
Program with unique labels generated.
Test succeeded.

Test case 2 as shown in table 7.2 searches the labels in the program. And finally it generates
the unique labels in the out file
Table 7.3 nested macro unit test case 3

Sl. No. of test case :


Name of test :
Item / Feature being tested :
Sample Input :
Expected output :
Actual output :
Remarks :

3
Nested macro
Nested macro
Program with nested macros.
Program with nested macros expanded.
Program with nested macros expanded.
Test successful.

Test case 3 as shown in table 7.3 tests the nested macros in the program and expands them
accordingly and the output program with macros expanded is obtained.
Table 7.4 Time to expand a macro unit test case 4

Sl. No. of test case :


Name of test :
Item / Feature being tested :
Sample Input :
Expected output :
Actual output :
Remarks :

4
Time to expand a macro in program
Macro processor
Program with macro
Program with macro, is expanded in 0.019 seconds.
Program with macro, is expanded in 0.019 seconds.
Test successful.

Test case 4 as shown in table 7.4 checks the total time required to expand the macro by macro
processor.Timer is started, before expanding the macro starts and timer is stopped after the
macro processor has expanded. The duration is calculated and displayed as output on the
screen. Sample output test is shown in above table.

7.7 Summary
Implementation describes the important decisions taken for the development of the project.
Programming language selection which can help in the designing of the project efficiently is
Dept of ISE, RVCE

2014-15

Page 27

Macro Processor
chosen. The platform selection is described based on the requirement of the project. The
coding convention used in the project is explained in detail. Software testing deals with the
testing of each module to meet the functional requirements. Various stages of testing are
included. The test cases to test and their respective outcomes are tabulated.

CHAPTER 8
RESULTS, DISCUSSION AND INFERENCE
8.1 Advantages and Disadvantages
The following are the major advantages of the project
Effectively replaces code that was meant for replacement with the macro.
Used concepts such as file pointers, structures to carry out its tasks

Dept of ISE, RVCE

2014-15

Page 28

Macro Processor

Highlights errors caused by macros that the programmer can rectify before the
code is sent for compilation rather than to be intimated of errors during
compilation
Produces the output file, even if there are errors or warnings in the input file.
Maintains a separate input file and a separate output file for data verification
and rectification.
Maintains macro names and their definitions in structures, thereby reducing the
amount of resources involved with character arrays and is useful for debugging
purposes.
Tabs and whitespaces and original contents of the input file are maintained as
they are even in the final source code file.

The following are the major disadvantages of the project


Conditional macros are not taken care only simple macros, parameterized
macro, macro within macros are taken care off.
The input file has to be prepared as a text file, which means the programmer
will have to first save his program as a .txt file. Also, the output file is created
as a .txt file, which means the programmer will have to bring this output file
back to the IDE for compilation. Compilation errors would require the
programmer to start the process from scratch

8.2 Future Enhancements


Based on the development of the project, there is sufficient scope for further
enhancement. Enhancements can be made by way of improving on existing features,
addition of new features, removal of constraints and increasing flexibility to macros.
Some of the possible future enhancements are as follows:

Dept of ISE, RVCE

2014-15

Page 29

Macro Processor

Taking care of conditional macros


Conditional macros like the if, elseif, while,endwhile else are not taken into account
by using macro time variable to store value during execution of conditions.

Inclusion of a compiler
Steps can be taken to develop an IDE (Integrated Development Environment), so that
the output cpp file can be directly compiled and the results caused due to macro
expansions can be taken care of. However, the development of an IDE would require
also the development of an editor, a parser, linker and other systems software concepts
that are well beyond the boundaries of a simple macro processor. However, with the
combination of a compiler, the use of macros can be fully justified.

User-defined macros
User-defined macros can be setup to help programmers work with arrays and files
better. These macros can be used to perform regular tasks such as arranging elements
in ascending or descending order, or to open files with a given filename and file mode.
These specifics can be applied with more input from the programmer as to how he
would like to lessen the amount of typing and to help increase readability.

REFERENCES
1) ANDREW S. TANENBAUM, A General-Purpose Macro Processor as a Poor Man's
Compiler-Compiler, IEEE TRANSACTIONS ON SOFTWARE ENGINEERING,
VOL. SE-2, NO. 2, JUNE 1976
2) P. J. Brown, "The ML/I macro processor," Commun. Ass. Comput. Mach., vol. 10, pp.
618-623, Oct. 1967.

Dept of ISE, RVCE

2014-15

Page 30

Macro Processor
3) W. Wulf, D. Russell, and A. Habermann, "BLISS: A language for systems
programming," Commun. Ass. Comput. Mach., vol.14, pp. 780-790, Dec. 1971.
4) P. J. Brown, "Macro processors and software implementation," Comput. J., vol. 12,
pp. 327-331, Nov. 1969.

5)

Marshall, Martin (April 29, 1991). "Macro Assembler Update Adds High-Level
Features". InfoWorld 13 (17): p. 21. ISSN 0199-6649.

Appendices
Appendix A
Source code
Code.c
#include<stdio.h>
#include<string.h>
#include<stddef.h>.
Dept of ISE, RVCE

2014-15

Page 31

Macro Processor
#include str_rep.h
FILE *input_f,*output_f;
int main ()
{
int test;
int counter=99;
int j,check_endof_macro;
check_endof_macro=1;
j=0;
input_f = fopen("input.txt","r");
output_f = fopen("expanded.txt","w");
while(1){
j++;
if( feof(input_f) ){break ;
}
fscanf(input_f,"%s %s %s",label,opcode,operand);
counter=search(opcode);
if(counter!=99)
{
write_to_file(1,counter);
printf("\nok\n");
continue;

//Not to read same line again

}
write_to_file(0,0);
if(strcmp(opcode,"MACRO")==0){
strcat(arg[c_d], operand);
strcat(name[c_d], label);
check_endof_macro=1;
while(check_endof_macro)
{
fscanf(input_f,"%s %s %s",label,opcode,operand);
counter=search(opcode);
Dept of ISE, RVCE

2014-15

Page 32

Macro Processor
if(counter!=99)
{
nestmacro(counter,c_d);
printf("\nok\n");
continue;
}
write_to_file(0,0);
check_endof_macro=strcmp(opcode,"MEND");
if(check_endof_macro!=0)
{
write_to_deftab(c_d);
}
}
c_d++;
}
}
test=search("RBUFF");
fclose(input_f);
}
int countdef=0;
int c,expnd=0;
char label[20],opcode[20],operand[20];
char

name[10][10]={"","","","","","","","","",""},arg[10]

[100]={"","","","","","","","","",""},def[30][4096]={"","","","","","","","","",""};
char unique_Label='A';
int search(char *macro_name)
{
int i;

Dept of ISE, RVCE

2014-15

Page 33

Macro Processor
for(i=0;i<c_d;i++)
{
if(strcmp(macro_name,name[i])==0)
{
//

printf("\nccc%d\n",i);
return i;

}
}
return 99;
}
char *split_Argtab(char str[128],int count_Split)
{
char * pch=" ";
// printf ("Splitting string \"%s\" into tokens:\n",str);
if(count_Split>0)
{
pch = strtok (str,",");
while (pch != NULL && count_Split>1)
{
pch = strtok (NULL, ",");
count_Split--;
}
}
//printf("%s\n",pch );
return pch;
}
/*
int count_Argtab(char a[30])
{
Dept of ISE, RVCE

2014-15

Page 34

Macro Processor
int count_arg=0,i=0;
/*char *t;
t=st;
//*s='\0';
printf("%s\n",t);
while(*t!='a')
{
if(*t==',')
count_arg++;
printf("check\n");
t++;
}*/
/*
printf("%s\n",a );
int len=strlen(a)-1;
printf("%c\n",a[0] );
while(i!=3)
{
if(!strcmp(',',a[i]))
count_arg++;
i++;
}
return count_arg;
}*/
int write_to_file(check_Expand,counter)
{
char operands[10][128]={"","","","","","","","","",""};
char arg_operands[10][128]={"","","","","","","","","",""};
char * pch;
char *split_opnd=" ";
char *token;
int icount=0,len=0,ii=0,count_arg=0,i,count_Split;
char *opnd_Replace;
Dept of ISE, RVCE

2014-15

Page 35

Macro Processor
char dummy[128]={""};
char def_buffer[4096];
if(check_Expand==1)
{
fprintf(output_f, ";");
fprintf(output_f, label);
fprintf(output_f, " ");
fprintf(output_f, opcode);
fprintf(output_f, " ");
fprintf(output_f, operand);
fprintf(output_f, "\n");
//printf("%s",split_Argtab(arg[counter],2));
//Splitting Operand
len=strlen(operand)-1;
while(ii!=len)
{
if(operand[ii]==',')
count_arg++;
ii++;
}
/*for ( i = 0; i <count_arg ; i++)
{
count_Split=i+1;
if(count_Split>0)
{
split_opnd = strtok (operand,",");
while (split_opnd != NULL && count_Split>1)
{
split_opnd = strtok (NULL, ",");
count_Split--;
}
Dept of ISE, RVCE

2014-15

Page 36

Macro Processor
};
strcat(operands[i],split_opnd);
}*/
for (i =0; i <=count_arg ; i++)
{
/* code */
strcpy(dummy,operand);
//printf("#%s\n\n",split_Argtab(dummy,i+1));
strcat(operands[i],split_Argtab(dummy,i+1));
}
//Splitted Opnd
count_arg=0;
ii=0;
//Splitting Arguments
len=strlen(arg[counter])-1;
while(ii!=len)
{
if(arg[counter][ii]==',')
count_arg++;
ii++;
}
for (i =0; i <=count_arg ; i++)
{
/* code */
strcpy(dummy,arg[counter]);
//printf("#%s\n\n",split_Argtab(dummy,i+1));
strcat(arg_operands[i],split_Argtab(dummy,i+1));
}
//Splitting arg done
Dept of ISE, RVCE

2014-15

Page 37

Macro Processor
//Copying argtab
i=0;
for (i =0; i <=count_arg ; i++)
{
/* code */
//printf("%s\n",arg_operands[0] );
//opnd_Replace = strstr (def[counter],arg_operands[0] );
//strncpy (opnd_Replace,"sam",3);
strcat(def_buffer,replace_str2(def[counter],arg_operands[i],operands[i]));
def[counter][0]='\0';
strcat(def[counter],def_buffer);
def_buffer[0]='\0';
printf("%s\n",def[counter] );
}
//copying done
fprintf(output_f, def[counter]);
count_arg=0;//to count no. of again for different macro
return 2;
}
fprintf(output_f, label);
fprintf(output_f, " ");
fprintf(output_f, opcode);
fprintf(output_f, " ");
fprintf(output_f, operand);
fprintf(output_f, "\n");
return 1;
}
int nestmacro(int counter,int macroCounter)
{
strcat( def[macroCounter],def[counter]);
}
int write_to_deftab(int d_c)
Dept of ISE, RVCE

2014-15

Page 38

Macro Processor
{
char label[40]=" ";
if(strcmp("NULL",label)!=0)
{
//strcat(label,'$');
label[0]='$';
label[1]=unique_Label;
unique_Label = unique_Label + 1;
strcat(label,label);
strcat(def[d_c], label);
}
else
{
strcat(def[d_c], label);
}
strcat(def[d_c], " ");
strcat(def[d_c], opcode);
strcat(def[d_c], " ");
strcat(def[d_c], operand);
strcat(def[d_c], "

");

strcat(def[d_c], "\n");
}
str_rep.h
char *replace_str2(const char *str, const char *old, const char *new)
{
char *ret, *r;
const char *p, *q;
size_t oldlen = strlen(old);
size_t count, retlen, newlen = strlen(new);
int samesize = (oldlen == newlen);
if (!samesize) {
Dept of ISE, RVCE

2014-15

Page 39

Macro Processor
for (count = 0, p = str; (q = strstr(p, old)) != NULL; p = q + oldlen)
count++;
retlen = p - str + strlen(p) + count * (newlen - oldlen);
} else
retlen = strlen(str);
if ((ret = malloc(retlen + 1)) == NULL)
return NULL;
r = ret, p = str;
while (1) {
if (!samesize && !count--)
break;
if ((q = strstr(p, old)) == NULL)
break;
ptrdiff_t l = q - p;
memcpy(r, p, l);
r += l;
memcpy(r, new, newlen);
r += newlen;
p = q + oldlen;
}
strcpy(r, p);
return ret;
}

Dept of ISE, RVCE

2014-15

Page 40

Macro Processor

APPENDIX B
Output Screenshot
Input file

Dept of ISE, RVCE

2014-15

Page 41

Macro Processor

Dept of ISE, RVCE

2014-15

Page 42

Macro Processor

Output file

Dept of ISE, RVCE

2014-15

Page 43

Macro Processor

Dept of ISE, RVCE

2014-15

Page 44

S-ar putea să vă placă și