0% found this document useful (0 votes)
16 views3 pages

Esp (CD)

Uploaded by

Subham Garain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views3 pages

Esp (CD)

Uploaded by

Subham Garain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

ESP (CD)

What is a Compiler?

• A compiler is a program that translates high-level source code written by a programmer into
machine code that a computer can execute.

• Example: Translating a C program into assembly code.

Phases of a Compiler

A compiler processes a program in several phases, grouped into two parts:

1. Analysis Phase (Front-End): Understands the source code.

2. Synthesis Phase (Back-End): Produces the target code.

1. Analysis Phase

• Lexical Analysis:

o Converts source code into tokens.

o Tools: Lex.

o Example: int x = 5; → tokens: int, x, =, 5, ;.

• Syntax Analysis (Parsing):

o Checks the structure of the code according to grammar rules.

o Tools: Yacc.

o Example: Ensures int x = 5; follows the grammar.

• Semantic Analysis:

o Ensures meaningfulness and type checking.

o Example: Verifies if assigning a string to an integer variable is valid.

2. Synthesis Phase

• Intermediate Code Generation:

o Produces an abstract representation (e.g., three-address code).

o Example: x = y + z → T1 = y + z, x = T1.

• Code Optimization:

o Improves efficiency by reducing instructions.

o Example: Removes unnecessary calculations.

• Code Generation:

o Generates machine code.


o Example: ADD R1, R2, R3.

• Code Linking and Loading:

o Links various compiled files into an executable.

Types of Parsers

1. Top-Down Parsing:

o Starts from the start symbol and works down.

o Example: Predictive Parsing (uses a lookahead to make decisions).

2. Bottom-Up Parsing:

o Starts from the input and works up to the start symbol.

o Example: LR Parsing (uses parsing tables).

ey Concepts

1. Lexeme

• The smallest sequence of characters in the source code that matches a token.

• Example: In int x = 5;, int and x are lexemes.

2. Token

• A categorized unit of lexemes.

• Example: int → keyword, x → identifier.

3. Regular Expression

• Defines patterns for lexemes.

• Example: Identifier pattern → [a-zA-Z_][a-zA-Z_0-9]*.

4. Symbol Table

• A data structure storing information about variables, functions, and objects in the program.

5. Predictive Parsing

• Uses FIRST and FOLLOW sets for parsing without backtracking.

6. LR Parsing

• Efficient bottom-up parsing techniques.

o SLR: Simple LR parsing.

o LALR: Look-Ahead LR parsing.

o CLR: Canonical LR parsing.


Common Errors in Compilation

1. Lexical Errors:

o Example: Invalid tokens ($var = 5;).

2. Syntax Errors:

o Example: Missing semicolon (int x = 5).

3. Semantic Errors:

o Example: Type mismatch (x = "hello", where x is an integer).

4. Runtime Errors:

o Example: Division by zero.

Advantages of Compilers

1. Faster execution compared to interpreters.

2. Produces optimized machine code.

3. Detects syntax and semantic errors before execution.

2. Chomsky Hierarchy

The hierarchy categorizes grammars into 4 types:

1. Type 0 (Unrestricted Grammar): No restrictions.

2. Type 1 (Context-Sensitive Grammar): Rules depend on context.

3. Type 2 (Context-Free Grammar): Rules are independent of context.

4. Type 3 (Regular Grammar): Simplest form, used in lexical analysis.

3. FIRST and FOLLOW

• FIRST: The set of terminals that can appear as the first symbol of a string derived from a non-
terminal.

• FOLLOW: The set of terminals that can appear immediately after a non-terminal.

4. Peephole Optimization

• A local optimization technique that works on a small part of the code (a "peephole").

• Example: Removing redundant instructions.

You might also like