Professional Documents
Culture Documents
SPCC Ia 2
SPCC Ia 2
2. What are the different ways of intermediate code representation? Explain with example.
Postfix Notation:
• Also known as reverse Polish notation or suffix notation.
• In the infix notation, the operator is placed between operands, e.g., a +
b. Postfix notation positions the operator at the right end, as in ab +.
• For any postfix expressions e1 and e2 with a binary operator (+) , applying
the operator yields e1e2+.
• Postfix notation eliminates the need for parentheses, as the operator’s
position and arity allow unambiguous expression decoding.
• In postfix notation, the operator consistently follows the operand.
Example 1: The postfix representation of the expression (a + b) * c is : ab +
c*
Example 2: The postfix representation of the expression (a – b) * (c + d) +
(a – b) is : ab – cd + *ab -+
2. Three-Address Code:
• A three address statement involves a maximum of three
references, consisting of two for operands and one for
the result.
• A sequence of three address statements collectively
forms a three address code.
• The typical form of a three address statement is
expressed as x = y op z, where x, y, and z represent
memory addresses.
• Each variable (x, y, z) in a three address statement is
associated with a specific memory location.
• While a standard three address statement includes
three references, there are instances where a statement
may contain fewer than three references, yet it is still
categorized as a three address statement.
Example: The three address code for the expression a +
b * c + d : T1 = b * c T2 = a + T1 T3 = T2 + d; T 1 , T2 ,
T3 are temporary variables.
There are 3 ways to represent a Three-Address Code in
compiler design:
i) Quadruples
ii) Triples
iii) Indirect Triples
Read more: Three-address code
3. Syntax Tree:
• A syntax tree serves as a condensed representation of a
parse tree.
• The operator and keyword nodes present in the parse
tree undergo a relocation process to become part of
their respective parent nodes in the syntax tree. the
internal nodes are operators and child nodes are
operands.
• Creating a syntax tree involves strategically placing
parentheses within the expression. This technique
contributes to a more intuitive representation, making it
easier to discern the sequence in which operands should
be processed.
• The syntax tree not only condenses the parse tree but
also offers an improved visual representation of the
program’s syntactic structure,
Example: x = (a + b * c) / (a – b * c)
3. Construct LR(0) parsing table for the following grammar and analyze the contents of stack and
input buffer and action taken after each step while parsing the input ‘abbcbcde’.
S→aCDe
C→Cbc
C→b
D→d
4. Discuss different databases used in direct linking loader.
Loader is the system program which is responsible for preparing the object program for
execution and initiate the execution.
• The loader does the job of coordinating with the OS to get initial loading address for the
.EXE file and load it into the memory.
Function of Loader
Allocation:
• Allocates the space in the memory where the object program would be loaded for
Execution.
Linking:
• It links two or more object codes and provides the information needed to allow references
between them.
Loading:
• It brings the object program into the memory for execution.
Format of Databases:
The assembler provides following types of record in the object file as follows
• ESD record combine information about all the symbol that are defined in this program.
• But may be referenced in the program but defined elsewhere.
• Text card record control the actual object code translated version of the source program.
1. END CARD:
• The END card records indicates the end of the object File and specifies the start address for
Execution.
ESD
IT consist of three types of definition
• SD- Segment Definition
• LD – Local Definition
• ER – External Reference
Note:
• Since all the address cannot be resolved in the starting.
• We required two pass Dynamic linking Loader.
• In pass 1 , it performs defining of a segment and local definition
• In pass 2, we work on with the text and RLD.
END
• Specifies the END of the program. • And total address is generated by PLA + SLength.
5. Generate 3-address code for the following C program and construct flow graph with the help of
basic blocks:
I=1; j=1; x=5;
While(i<3)
{
Switch(i)
{
Case 1:
A[j++]= i+x;
Break;
Case 2:
A[j++]=i-x;
Break;
}
i++;
}
6. What the phases of compiler? Give working of each for the following:
a) P = Q +R-S*3 b) a=b*c+10 where a,b,c are of type real
The compilation process contains the sequence of various phases. Each phase takes
source program in one representation and produces output in another
representation. Each phase takes input from its previous stage.
Lexical Analysis:
Lexical analyzer phase is the first phase of compilation process. It takes source code
as input. It reads the source program one character at a time and converts it into
meaningful lexemes. Lexical analyzer represents these lexemes in the form of
tokens.
Syntax Analysis
Syntax analysis is the second phase of compilation process. It takes tokens as input
and generates a parse tree as output. In syntax analysis phase, the parser checks
that the expression made by the tokens is syntactically correct or not.
Semantic Analysis
Semantic analysis is the third phase of compilation process. It checks whether the
parse tree follows the rules of language. Semantic analyzer keeps track of
identifiers, their types and expressions. The output of semantic analysis phase is
the annotated tree syntax.
Code Optimization
Code optimization is an optional phase. It is used to improve the intermediate code
so that the output of the program could run faster and take less space. It removes
the unnecessary lines of the code and arranges the sequence of statements in
order to speed up the program execution.
Code Generation
Code generation is the final stage of the compilation process. It takes the optimized
intermediate code as input and maps it to the target machine language. Code
generator translates the intermediate code into the machine code of the specified
computer.
Advantages of Loader
Disadvantages of Loader
11. Construct SLR parser for the grammar and parse the input ‘()()’
S→(S)S|€
12. Explain the basic blocks and flow graph with example the 3-address code.
Basic block is a set of statements that always executes in a sequence one after the other.
Here,
• All the statements execute in a sequence one after the other.
• Thus, they form a basic block.
Three Address Code for the expression If A<B then 1 else 0 is-
Here,
• The statements do not execute in a sequence one after the other.
• Thus, they do not form a basic block.
Any given code can be partitioned into basic blocks using the following rules-
• All the statements that follow the leader (including the leader) till the next leader
appears form one basic block.
• The first statement of the code is called as the first leader.
• The block containing the first leader is called as Initial block.
Flow Graphs-
A flow graph is a directed graph with flow control information added to the basic blocks.
15. Test whether following grammar is LL(1) or not. If it is LL(1) ,construct parse table for the
grammar:
a)
S→1A|B|€
A→1AC|0C
B→0S
C→1
b)
S→Ad
A→aB|BC
B→b
C→e|€
c)
S→(A) | 0
S→AB
B→,SB|€
b)
S→aBDh
B→ cC
C→ bC|€
D→ EF
E→ g|€
LL Parser LR Parser