Compiler Design
Compiler Design
Compilers:
Structure of a compiler :
Lexical Analysis – Role of Lexical Analyzer
input buffering
In compiler design, input buffering is an
optimization technique used to improve the
efficiency of the lexical analysis stage. The
lexical analyzer, or scanner, is responsible for
reading the source code and breaking it down
into tokens (keywords, identifiers, operators,
etc.).
Lex
Lex is a tool or a computer program that generates
Lexical Analyzers (converts the stream of characters
into tokens). The Lex tool itself is a compiler. The
Lex compiler takes the input and transforms that
input into input patterns. It is commonly used with
YACC(Yet Another Compiler Compiler)
A Lex program has three parts:
Components:
How it Works:
– Peephole optimization:
1. To improve performance
2. To reduce memory footprint
Initial code:
y = x + 5;
i = y;
z = i;
w = z * 3;
Optimized code:
y = x + 5;
w = y * 3; //* there is no i now
Initial code:
x = 2 * 3;
Optimized code:
x = 6;
Strength Reduction: The operators that consume higher
execution time are replaced by the operators consuming
less execution time.
Initial code:
y = x * 2;
Optimized code:
y = x + x; or y = x << 1;
blocks.
sub-expressions.
Structure:
Components:
● Vertices (or nodes): Represent entities or data points
in the graph.
Challenges of GDFA:
Storage Organization
In the context of Compiler Design (CD), storage
organization refers to how data is allocated
and managed within the program's memory
space during its execution. This primarily
focuses on the memory layout of the compiled
code and its associated data structures. Here's
a breakdown of the key aspects:
Memory Regions:
Memory Allocation:
● Programs use functions like malloc (allocate) and
calloc (allocate and initialize to zero) to request
memory from the heap.
● This pointer is used by the program to access and
manipulate the allocated memory.
Evaluation Approach:
Example:
E -> E + T | E - T | T
T -> int
Example:
S -> var = E
E -> T
T -> int
Syntax Tree:
Advantages:
Disadvantages:
Advantages:
1. Translation of Expressions:
type Checking:
parser
● Top-down parser: Starts with the start symbol and ends on the
terminals
● Bottom-up parser: Also known as Shift Reduce Parsers
2. Grammars:
3. Error Handling: