Skip to content

jainsujan11/Compilers

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 

Repository files navigation

TinyC — A Compiler for a Subset of C (Compilers Laboratory, IIT KGP, Autumn 2024)

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.


📂 List of Assignments

Part 1 — Lexer for TinyC

  • 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 (.l file)
    • Test input file covering all lexical rules
    • Output: Stream of tokens (<tokenname, lexeme>) and symbol table.

Part 2 — Parser for TinyC

  • 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 (.y file) 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.

Part 3 — Machine-Independent Code Generator

  • 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.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •