0% found this document useful (0 votes)
11 views6 pages

Compiler Construction Final

Uploaded by

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

Compiler Construction Final

Uploaded by

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

1) Parser in Compiler Construction

A parser is a component of a compiler that processes the source code to check its structure according to
the grammar of the programming language. It ensures the code follows the rules and produces a
structured representation, usually in the form of a parse tree or syntax tree.

What does a Parser do?


1. Checks Syntax: Verifies if the code is written correctly (no missing semicolons, brackets, etc.).
2. Builds Parse Tree: Converts the source code into a tree-like structure representing the
grammatical structure of the code.
3. Detects Errors: If there are syntax errors, the parser identifies them and provides feedback.

Types of Parsers
1. Top-Down Parsers: Start analyzing from the root of the parse tree and move toward the leaves.
2. Bottom-Up Parsers: Start analyzing from the leaves of the parse tree and move toward the root.

Simple Example
Consider the grammar for a simple arithmetic expression:

E→E+T|T

T→T*F|F

F → a|b|c | id

Now, let’s parse the expression: a + b * c

1. Input Code: a + b * c

2. Output Parse Tree:

/|\

E + T

| /\

T T F

| | |

F F c

| |

a b

This shows the code is valid and conforms to the grammar.


Error Handling Example
 If the input is a + * c:
 The parser will detect an error because + cannot be followed directly by *.

2) Tokens in Compiler Construction


A token is the smallest unit of a program's source code that has a meaningful interpretation. The lexer
(lexical analyzer) breaks the code into tokens, which are then processed by the parser.

Types of Tokens
1. Keywords: Reserved words in a language (e.g., if, else, while).
2. Identifiers: Names for variables, functions, etc. (e.g., x, sum).
3. Operators: Symbols for operations (e.g., +, -, *).
4. Literals: Constant values (e.g., 10, 'hello').
5. Punctuation: Symbols like ;, ,, (), {}.

Example
For the code:

 int x = 10 + 20;

Tokens:

1. int → Keyword
2. x → Identifier
3. = → Operator
4. 10 → Literal
5. + → Operator
6. 20 → Literal
7. ; → Punctuation

3) Semantic Analysis and Parser Rules


Semantic Analysis ensures the logical correctness of a program by checking the parser's output (parse
tree or AST). It validates the program's operations, ensuring they follow the language's meaning rules.

Role of Parser in Semantic Analysis


1. Parser Output: Generates a parse tree or abstract syntax tree (AST) based on grammar rules.
2. Semantic Analyzer Tasks:
1. Type Compatibility: Verifies that variables and expressions have compatible types.
2. Scope Checking: Ensures variables/functions are declared before use.
3. Function Argument Matching: Checks the number and type of arguments in function calls.
4. Operator Validation: Confirms operators are used with valid data types.

Semantic Rules in Parser Phase


1. Type Checking Rule:
Verify types in operations or assignments.

Example:

 Assignment → Identifier = Expression


 Semantic Check: Ensure Identifier and Expression types match or are convertible.

2. Scope Rule:
Ensure variables/functions are declared before use.

Example:

 Identifier → VariableName
 Semantic Check: Confirm VariableName is in the current scope.

3. Function Argument Rule:


Validate the number and type of arguments in function calls.

Example:

 FunctionCall → Identifier (ArgumentList)


 Semantic Check: Match ArgumentList types with the function definition.

4. Operator Rule:
Ensure operators are applied to valid types.

Example:

 Expression → Expression + Term


 Semantic Check: Both Expression and Term must be numeric.

Example of Semantic Analysis


 Code:
 int x = 10;
 float y = x + 5.5;
 z = x + y;

Steps
1. Parser Rules (Syntax Check):
Match declarations:

 Declaration → Type Identifier


 Valid: int x; float y;

Match assignments:

 Assignment → Identifier = Expression


 Valid: x = 10; y = x + 5.5; z = x + y;

2. Semantic Rules (Meaning Check):


Type Checking:

 x (int) + 5.5 (float) → Implicit conversion for y is allowed.


 z is undeclared → Semantic error: Undeclared variable.

Scope Check: Ensure x and y are declared before use.

Operator Check: Ensure + is applied to compatible types.

4) Code Generation
Code generation is the final phase in a compiler where the intermediate representation (IR) of a
program is translated into machine code or assembly code that can be executed by a computer.

Key Tasks in Code Generation:


1. Register Allocation:
Decides which variables or values should be stored in the CPU's registers for faster access during
execution.
2. Instruction Selection:
Selects the correct machine-level instructions (such as ADD, LOAD, STORE) to perform
operations described in the intermediate code.
3. Instruction Scheduling:
Arranges the machine instructions in the most efficient order, minimizing delays and optimizing
performance by reducing dependencies between instructions.
4. Memory Management:
Determines how data will be stored in memory, including whether it will be stored in registers,
the stack, or the heap.

Example:
For the statement: x = a + b;

The code generation might produce assembly code like:

 LOAD a ; Load the value of 'a' into a register


 ADD b ; Add the value of 'b' to the register
 STORE x ; Store the result into 'x'

5) Symbol Table
In compiler construction, a symbol table is a data structure that stores information about variables,
functions, and other symbols used in the source code during compilation.

Purpose:
1. Track identifiers: Keeps track of variables, functions, and other symbols in the program.
2. Store information: Stores details like type, scope, memory location, and usage of each symbol.

Static Binding
Occurs at compile-time: The memory address and type of variables or methods are determined during
compilation.

Advantages of Static Binding:


1. Faster execution: Memory is fixed, so the program runs faster.
2. Simple to manage: The memory locations are known, so the compiler can optimize easily.

Disadvantages of Static Binding:


1. Less flexibility: Memory is reserved even if not needed.
2. Wasted memory: If not all variables are used, they still take up space.

Dynamic Binding
Occurs at runtime: Memory address and type are determined during execution.

Advantages of Dynamic Binding:


1. More flexible: Memory is allocated only when needed.
2. Saves memory: Only used memory is allocated.

Disadvantages of Dynamic Binding:


1. Slower execution: Memory allocation takes time during program execution.
2. More complex: Managing memory is harder, which can lead to errors like memory leaks.

6) Three-Address Code (TAC)


A common form of intermediate code where each instruction involves at most three addresses
(operands). Each statement can have:

1. At most three operands (variables or constants).


2. One operator besides the assignment operator.

Example:
Expression: id1 = id2 + id3 * 60

TAC Steps:
1. Convert constant:

 Temp1 = inttoreal(60)

2. Multiply:

 Temp2 = id3 * Temp1

3. Add:

 Temp3 = id2 + Temp2

4. Assign:

 id1 = Temp3

Final TAC:

 Temp1 = inttoreal(60)
 Temp2 = id3 * Temp1
 Temp3 = id2 + Temp2
 id1 = Temp3

You might also like