This repository contains my implementations for the Compilers Laboratory (CS39003) course at IIT Kharagpur, Autumn 2024.
The project involves building a compiler for TinyC, a carefully selected subset of the C programming language, following a multi-phase approach.
- Goal: Implement the lexical analyzer for TinyC.
- Tools: Flex
- Scope:
- Based on the ISO/IEC 9899:1999 (C99) standard, but reduced to a subset.
- Handles keywords, identifiers, constants, string literals, punctuators, and comments.
- Maintains a symbol table with token names and lexemes.
- Deliverables:
- Flex specification (
.lfile) - Test input file covering all lexical rules
- Output: Stream of tokens (
<tokenname, lexeme>) and symbol table.
- Flex specification (
- Goal: Implement the parser and construct the parse tree for TinyC.
- Tools: Bison + Flex
- Scope:
- Uses phase structure grammar (subset of C99).
- Supports expressions, declarations, statements, and external definitions.
- Parse tree is constructed and stored in a human-readable format.
- Deliverables:
- Bison specification (
.yfile) defining grammar and tokens - Main driver program to connect lexer and parser
- Makefile to build lexer and parser
- Test input program and corresponding parse tree output.
- Bison specification (
- Goal: Generate 3-address code (intermediate representation) for TinyC.
- Tools: Bison + Flex + C++
- Scope:
- Extends lexer and parser with semantic actions.
- Generates:
- 3-address quads
- Symbol tables (including nested scopes)
- Auxiliary data structures for translation
- Supports:
- Arithmetic, logical, relational, shift, and assignment expressions
- Simple variable, pointer, array, and function declarations
- Core statements (selection, iteration, return)
- Function definitions
- Excludes complex constructs like
switch,goto, compound assignment operators, etc.
- Design Highlights:
- Symbol Table: Nested, supports lookup, insert, update, and print.
- Quad Array: Stores intermediate 3-address instructions.
- Global Functions:
makelist,merge,backpatch,typecheck, and type conversion utilities.
- Deliverables:
- Complete translator (
.l,.y,.h,.cxx) - Test inputs and corresponding quad outputs
- Makefile and final tar archive.
- Complete translator (