0% found this document useful (0 votes)
4 views5 pages

Compiler Phases Buffering DFA Detailed

The document outlines the phases of a compiler, including lexical analysis, syntax analysis, semantic analysis, intermediate code generation, code optimization, and target code generation, using the expression 'a := b + c * 50' as an example. It also discusses compiler construction tools, input buffering techniques, and the specification and recognition of tokens, highlighting the importance of efficiency and performance in compiler design. Additionally, it addresses an invalid expression example to illustrate semantic errors in the compilation process.

Uploaded by

rem895964
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)
4 views5 pages

Compiler Phases Buffering DFA Detailed

The document outlines the phases of a compiler, including lexical analysis, syntax analysis, semantic analysis, intermediate code generation, code optimization, and target code generation, using the expression 'a := b + c * 50' as an example. It also discusses compiler construction tools, input buffering techniques, and the specification and recognition of tokens, highlighting the importance of efficiency and performance in compiler design. Additionally, it addresses an invalid expression example to illustrate semantic errors in the compilation process.

Uploaded by

rem895964
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/ 5

1.

Phases of a Compiler and Output for Expression a := b + c * 50

The phases of a compiler are:

a) Lexical Analysis:

- Breaks source code into tokens.

- Input: a := b + c * 50

- Output: [ID(a), ASSIGN(:=), ID(b), PLUS(+), ID(c), MULT(*), NUM(50)]

b) Syntax Analysis:

- Checks grammar and constructs parse tree.

- Output: Parse Tree

:=

/ \

a +

/\

b *

/\

c 50

c) Semantic Analysis:

- Type checking and declaration validation.

- Output: Annotated syntax tree with symbol types and semantic errors (if any).

d) Intermediate Code Generation:

- Converts syntax tree to a three-address code (TAC).

- Output:
t1 = c * 50

t2 = b + t1

a = t2

e) Code Optimization:

- Enhances performance (e.g., constant folding).

- Output:

t1 = 50 * c

t2 = b + t1

a = t2

f) Target Code Generation:

- Converts TAC to assembly/machine code.

- Output (Pseudo Assembly):

MOV R1, c

MUL R1, 50

MOV R2, b

ADD R2, R1

MOV a, R2

g) Code Optimization (Machine-specific):

- Improves register usage, instruction scheduling.

2. Compiler Construction Tools

Tools that aid in compiler creation:

- Lex: Lexical analyzer generator


- Yacc: Parser generator

- Bison: GNU version of Yacc

- ANTLR: Parser generator for multiple languages

- LLVM: Framework for code generation and optimization

- GCC: Complete compiler suite

Benefits:

- Automates parsing and scanning

- Reduces manual coding

- Enhances maintainability

3. Input Buffering

Input buffering in lexical analyzer improves performance by minimizing I/O.

Double Buffering Scheme:

- Two buffers: while one is scanned, the other is filled.

- Sentinels (EOF markers) indicate buffer end.

Advantages:

- Efficient scanning

- Reduces boundary checks

4. Minimized DFA for (a|b)*abb

Regular expression: (a|b)*abb

NFA Construction:
- Loop for (a|b)*

- Sequence for abb

Convert to DFA using subset construction

Minimize the DFA by merging equivalent states

Final DFA:

- States: q0 (start), q1, q2, q3 (final)

- Transitions:

q0 --a/b--> q0

q0 --a--> q1

q1 --b--> q2

q2 --b--> q3 (accepting)

5. Specification and Recognition of Tokens

Token = <token-name, attribute-value>

Defined using regular expressions.

Example: identifier [a-zA-Z][a-zA-Z0-9]*

Recognized using DFA:

- DFA reads characters

- Accepts input if final state reached

- Follows longest match rule

6. Phases of Compiler for c = a + b = 4


Invalid expression: c = a + b = 4 is not semantically correct in most languages

Phases:

- Lexical: [ID(c), ASSIGN(=), ID(a), PLUS(+), ID(b), ASSIGN(=), NUM(4)]

- Syntax: Error (Invalid grammar)

- Semantic: Error (Assignment not a valid expression)

- Intermediate code: Not generated due to error

7. Input Buffering Scheme in Lexical Analyzer

Scheme:

- Two buffers of equal size

- Sentinel at end of each buffer

- Pointer moves through buffer, refills other buffer when needed

Benefits:

- Reduces I/O operations

- Increases lexical analyzer speed

- Detects token boundaries efficiently

You might also like