Skip to content

Latest commit

 

History

History
70 lines (48 loc) · 2.05 KB

File metadata and controls

70 lines (48 loc) · 2.05 KB

TinyJava Language Compiler

Course: CS340 – Programming Languages
Instructor: Professor Pani Chakrapani


Project Overview

This project implements a syntax analyzer and intermediate code generator for a simplified programming language called TinyJava. The design and structure of the compiler follow the architecture described in Figure 6.29 of the course textbook, originally presented for TinyAda and adapted here for a subset of Java.

The compiler processes a TinyJava source program through the following stages:

  1. Character-level input handling
  2. Lexical analysis (tokenization)
  3. Recursive descent parsing
  4. Symbol table management
  5. Intermediate (three-address) code generation

TinyJava Language Specification

The TinyJava language supported by this compiler includes the following features:

  • Variable types: int, double
  • Statements:
    • Variable declarations
    • Assignment statements
    • if / else conditional statements
  • Operators:
    • Arithmetic: +, -, *, /, %
    • Relational: ==, !=, <, <=, >, >=
  • Block statements using { }
  • Single-line comments using //

The language enforces basic type safety, including prevention of illegal double to int assignments.


Compiler Architecture

The compiler is structured according to the modular pipeline shown in Figure 6.29:

  • Chario: Reads the source file character-by-character and reports errors.
  • Scanner: Converts characters into a stream of tokens.
  • Parser: Uses recursive descent parsing to validate syntax and generate intermediate code.
  • SymbolTable: Tracks declared identifiers and their types.
  • IR: Stores and outputs the intermediate three-address code.

Each component is implemented as a separate Java class to ensure modularity and clarity.


Input Format

The compiler accepts a TinyJava source file as input. These source programs use the .tj file extension.

Example input file (test1.tj):

int x;
double y;

x = 3 + 4 * 2;

if (x > 5) {
    y = x / 2.0;
} else {
    y = 0.0;
}