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