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

CC Lab Project Mini Compiler

The document outlines a lab project for constructing a mini compiler using languages such as C, C++, Java, or Python. It details objectives, chapters on various compiler phases including lexical analysis, syntax analysis, semantic analysis, intermediate code generation, and code optimization, along with experiment tasks and evaluation criteria. Deliverables include the complete compiler source code, a final project report, and a demonstration.

Uploaded by

Ali Khan
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 views4 pages

CC Lab Project Mini Compiler

The document outlines a lab project for constructing a mini compiler using languages such as C, C++, Java, or Python. It details objectives, chapters on various compiler phases including lexical analysis, syntax analysis, semantic analysis, intermediate code generation, and code optimization, along with experiment tasks and evaluation criteria. Deliverables include the complete compiler source code, a final project report, and a demonstration.

Uploaded by

Ali Khan
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

Compiler Construction (Lab)

Anosh Fatima
Lecturer (SCS-NFC IEFR)

Lab Project- Mini Compiler


Languages: C/C++/Java/Python (any one of them)

Objectives
- Integrate all compiler phases into a working mini-compiler.
- Demonstrate all phases:

Lexical Analysis using FLEX/LEX


Syntax Analysis using YACC/BISON
Parsing Techniques
Semantic Analysis
Intermediate Code Generation
Code Optimization Basics
Final Project: Mini Compiler

Write a Report along source code of Project:

Chapter 1: Introduction to Compiler Construction


- Understand compiler phases and toolchain.
- Study the phases of a compiler.
- Identify source → lexical analysis → syntax analysis → semantic analysis → code generation →
optimization → target code.
- Write a short note on differences between compiler and interpreter.

Chapter 2: Lexical Analysis using FLEX/LEX


- Implement a lexical analyzer.
- Install & configure FLEX/LE.
- Write rules to identify identifiers, numbers, operators, and keywords.
- Test with a sample C program.
- Include Source code + screenshots

Chapter 3: Syntax Analysis using YACC/BISON


- Construct a parser using YACC.
- Define grammar for arithmetic expressions.
- Use YACC to generate a parser.
- Test expressions like (a+b)*c-d.
- Include Source code + test outputs.

Page 1
Chapter 4: Parsing Techniques
- Explore parsing methods.
- Implement recursive descent parser in C.
- Compare LL(1) vs LR parsing.
- Discuss parser error handling.
- Include Code

Chapter 5: Semantic Analysis


- Implement semantic checks.
- Extend parser to perform type checking.
- Report mismatched types in arithmetic operations.
- Handle variable declarations & scope.
- Include Source code + test cases.

Chapter 6: Intermediate Code Generation


- Generate intermediate representation (IR).
- Implement three-address code (TAC).
- Write IR for arithmetic and conditional statements.
- Test with small C snippets.
- Include TAC output + explanation.

Chapter 7: Code Optimization Basics


- Apply basic optimizations.
- Implement constant folding and dead code elimination.
- Optimize TAC generated earlier.
- Compare pre-optimization vs post-optimization code.
- Include Optimized TAC

Chapter 8: Final Project – Mini Compiler


- Integrate all phases into a mini compiler.
- Combine lexical, syntax, semantic, and IR generation.
- Input: Simple C/C++/Java/Python-like program.
- Output: Intermediate code with basic optimizations.
- Deliverable: Complete compiler source code + project report + viva.

Experiment Tasks
- Develop a mini-compiler for a subset of C/C++/Java/Python-like language.
- Implement modules: Lexical Analyzer, Syntax Analyzer, Semantic Analyzer, Intermediate
Code Generator, and Optimizer.
- Input: simple program (loops, conditionals, arithmetic).
- Output: optimized intermediate code.

Deliverables
- Complete compiler source code.
- Final project report.
- Demonstration + viva.

Evaluation Criteria
- Compiler functionality: 50%
- Report + documentation: 20%

Page 2
- Viva / demo: 20%
- Participation & project management: 10%

Page 3
Sample Code Examples

Lexical Analysis (Flex)


digit [0-9]
id [a-zA-Z_][a-zA-Z0-9_]*

%%
{digit}+ { printf("NUMBER"); }
{id} { printf("IDENTIFIER"); }
"+" { printf("PLUS"); }
. { /* ignore */ }
%%

Parsing (YACC)
%token NUMBER IDENTIFIER PLUS
%%
expr: expr PLUS term | term ;
term: NUMBER | IDENTIFIER ;
%%

Intermediate Code Example


t1 = a + b
t2 = t1 * c

Optimized Code Example


t1 = a + b
t2 = t1 * c ; (if a and b are constants, folded)

Page 4

You might also like