Q.1) What are the different phases of compiler with suitable example?
: The compilation
process is divided into several stages or phases, each performing a specific task. The output
of one phase becomes the input of the next phase. The following are the various phases of a
compiler: 1.Lexical Analysis: This is the first phase of the compiler. It reads the source
program character by character and groups them into meaningful sequences called lexemes.
For each lexeme, it produces a token. Example: In the statement int a = b + c;, it
recognizes tokens like int, a, =, b, +, c, ;. 2.Syntax Analysis: Also called parsing. It uses the
tokens generated by the lexical analyzer. It arranges tokens in a tree structure (parse tree) that
represents the grammatical structure of the source program. Example: It checks whether int
a = b + c; follows the correct grammar rules. 3.Semantic Analysis: Checks for semantic
errors in the program. Ensures that the parse tree follows the rules of the language. Example:
Verifies that variables b and c are declared before use. 4.Intermediate Code Generation:
Converts the parse tree or syntax tree into an intermediate representation. This code is easier
to optimize and is not machine-specific. Example: a = b + c may be represented as t1 = b
+ c; a = t1. 5.Code Optimization: Improves the intermediate code so that faster-running
machine code can be generated. Removes unnecessary code or combines operations.
Example: Eliminates common sub-expressions. 6.Code Generation: Converts the optimized
intermediate code into machine code. This phase maps instructions to the target machine’s
instruction set. Example: Machine-level instructions for a = b + c. 7.Code Linking and
Assembly: Translates the machine code into an executable form. Links different program
modules and libraries. Produces the final executable file.
Q.2) Syntax Directed Translation is a method used in compiler design where each grammar
rule is associated with a set of semantic rules. These semantic rules define how to compute
the attributes of the non-terminals, which are used to translate source code into
intermediate representations or machine code during compilation. It systematically
combines syntax and semantics to guide the translation process. In Syntax Directed
Translation, the parse tree is traversed, and attributes are evaluated based on the rules
attached to the grammar. It plays a crucial role in tasks like type checking, intermediate code
generation, and syntax tree construction by associating computations with syntactic
constructs.
Q.3) Parameterized macros are similar to functions. A parameterized macro is defined
using the #define directive and can take one or more arguments. These arguments are
placeholders that get substituted with actual values when the macro is invoked. This feature
allows code to be reused without using actual functions, which can save time during
compilation since macros are expanded by the preprocessor before compilation. Whenever
the macro is called in the code, the arguments are replaced, and the macro body is expanded
at that point. This expansion is textual and occurs exactly as written, so proper parentheses
are necessary to avoid unintended results due to operator precedence. Example:
#define SQUARE(x) ((x)*(x))
Here, SQUARE(4) will be replaced by ((4)*(4)).
Q.4)Single Pass Macro Processor: A single pass assembler scans the source code only once.
It simultaneously performs the tasks of both Pass I and Pass II of a two-pass assembler. It
generates the machine code directly without generating intermediate code. Working: •It
maintains a symbol table while reading each line. •If the symbol is not yet defined, it enters it
in the table with a “?” address (forward reference).•As soon as the symbol is defined later in
the code, its address is updated.•The location counter (LC) is updated after processing each
instruction.•Machine code is generated directly.
Q.5 Advanced macro facilities provide additional features in macro processors which
increase the power and flexibility of macros. These include: 1.Macro with
Parameters:Macros can have parameters that allow them to behave like functions.Example:
MACRO
INCR &ARG
ADD &ARG, =1
MEND 2.Conditional Macro Expansion:Allows conditional assembly of code based on
parameters using AIF (Assembler IF). Example:
MACRO
COMPARE &ARG1, &ARG2
AIF (&ARG1 EQ &ARG2) THEN
MOVER AREG, ='1'
THEN
MEND 3.Expansion Time Variables (SET): Used to declare variables which can be
modified during macro expansion using SET. Example:
LCL &COUNT
SET &COUNT, 1 4.Macro within Macro (Nested Macro): One macro can invoke another
macro. Example:
MACRO
OUTER
M1
MEND 5.Positional and Keyword Parameters: •Positional: Parameters based on
position.•Keyword: Parameters assigned using keywords. Example:
MACRO COPY &ARG1=DATA1, &ARG2=DATA2 MOVER AREG, &ARG1
MOVEM AREG, &ARG2 MEND