Comp Final
Comp Final
11111
Ans: The process of compiler design involves several distinct
phases, each designed to systematically translate a high-level
program into machine code while ensuring correctness and
efficiency.
Phases of a Compiler
The compilation process can be broken down into two main stages:
Analysis (front-end) and Synthesis (back-end). Each stage has
specific tasks.
1. Lexical Analysis (Scanner)
This is the first phase of compilation, where the source code is
scanned to break it into tokens (smallest units like keywords,
variables, operators, etc.). The lexical analyzer also removes
whitespaces and comments.
Example Code: int x = y + 10;
2. Syntax Analysis (Parser)
The parser checks if the sequence of tokens follows the grammar
rules of the programming language. It builds a parse tree (or syntax
tree).
Output (Parse Tree):
For the code int x = y + 10;, the tree looks like this:
If the syntax is incorrect (e.g., missing a semicolon), this phase
reports an error.
3. Semantic Analysis
In this phase, the compiler checks for semantic consistency, like
type checking, undeclared variables, etc.
Example Checks:
• Is y declared and initialized before use?
• Is the addition operation valid for y and 10 (e.g., no type
mismatch like adding a string to an integer)?
If an undeclared variable y is used, this phase generates an error
like:
Error: 'y' undeclared in the scope.
4. Intermediate Code Generation
The compiler generates an Intermediate Representation (IR) that is
machine-independent. The IR is easier to optimize and serves as a
bridge to machine code
Example IR (Three-Address Code) t1 = y + 10, x = t1
7. Code Linking and Loading
In this final phase, the machine code is linked with necessary
libraries and modules to produce the final executable file.
Output:
An executable file (e.g., a.out on Linux or program.exe on Windows).
5. Code Optimization
The IR is optimized to improve performance and reduce resource
usage. Optimization may include eliminating redundant
calculations or simplifying expressions.
Optimized IR:
If y is constant and equals 5, the optimized IR could be: x = 15
Q. Compare and contrast between a compiler and an interpreter
222222
•
Q. What is left recursive grammer give exampole?
4444
4. Example
5. Before Constant Folding:
int a = 10 * 5; | After Constant Folding: | int a = 50;
Here, 10 * 5 is evaluated at compile time and replaced by 50.
Advantages
1. Improves Performance: Reduces runtime computations by
performing them during compilation.
2. Reduces Code Size: Simplifies the code by removing
unnecessary calculations.
3. Optimizes Execution: Leads to faster execution since fewer
instructions are executed at runtime.
x = x + 5;
return x;
potentially used.
o Register Interference: Two variables interfere if their live
optimal performance.
o Correctness: Instructions must accurately implement the
machine instructions.
o Table-Driven: Uses a precomputed table to look up the best
2. Union (Alternation):
o Represents the choice between two or more expressions.
expression.
o Example: a* matches "", "a", "aa", "aaa", and so on.
4. Kleene Plus:
o Matches one or more occurrences of the preceding expression.
Example
Let's say you want to find all email addresses in a text document.
You could use a regular expression like this:
[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}
Explain various storage allocation strategies with
examples.
.Q. Storage Allocation Strategies
Storage allocation strategies determine how memory is assigned to
variables and data structures during program execution. Here are some
common approaches:
1. Static Allocation
2. Stack Allocation