Skip to content

Aryan0reve/MicroC-Compiler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

microC 3-Address Code Translator

Overview

This project implements a 3-address code translator for microC, a simplified subset of the C programming language. The translator takes microC source code as input and generates an intermediate representation in the form of 3-address quads, along with a symbol table. This intermediate representation serves as a machine-independent translation that can later be targeted to specific architectures like x86 or x86-64.

Project Components

The project consists of several key files:

  1. a9_220101019.l: The lexical analyzer specification (Flex file) that defines tokens for the microC language, including keywords, identifiers, constants, operators, and punctuators.

  2. a9_220101019.y: The parser specification (Bison file) that defines the grammar rules for microC and includes semantic actions to generate 3-address code. This file also contains data structures for the symbol table and quad array.

  3. a9_220101019.h: A header file containing shared type definitions and forward declarations used by both the lexer and parser.

  4. Makefile: Automates the compilation process, generating the lexer, parser, and final executable.

  5. 220101019_test1.mc: A sample microC program used to test the translator.

Features Supported

The translator supports the following microC language features:

Expression Phase

  • Arithmetic, shift, relational, bit, logical, and assignment expressions
  • Simple assignment operator (=)

Declarations Phase

  • Simple variable, pointer, array, and function declarations
  • Type specifiers: void, char, int, and float
  • Variable initialization

Statement Phase

  • Compound statements (blocks)
  • Expression statements
  • Selection statements (if-else)
  • Iteration statements (while, do-while, for)
  • Jump statements (return)

External Definitions Phase

  • Function definitions

How to Run

Prerequisites

  • GCC compiler
  • Flex (Fast Lexical Analyzer Generator)
  • Bison (Parser Generator)
  • Make utility

Compilation

To compile the project, run:

make clean
dos2unix a9_220101019.l a9_220101019.y( Ensure all files have Unix-style line endings (LF, not CRLF):)
make

Running the Translator

To run the translator on a test file:

./a9_220101019 220101019_test1.mc

This will process the input file and generate the 3-address code in a file named 220101019_quads1.out.

Examining the Output

To view the generated 3-address code:

cat 220101019_quads1.out

Output Format

The output file contains two main sections:

  1. Symbol Table: Lists all variables, functions, and temporary variables with their types, sizes, and memory offsets.

    Symbol Table: global
    ----------------------------------------
    Name            Type       Base Type  Size       Offset     Kind
    ----------------------------------------
    global_float    float      void       8          0          global
    i               int        void       4          8          global
    a               array      int        40         12         global
    ...
    
  2. Quad Array: Shows the 3-address code instructions generated from the input program.

    Quad Array:
    ----------------------------------------
    Index Op         Arg1       Arg2       Result
    ----------------------------------------
    L100  =          3.140000              global_float
    L101  =          10                    i
    L102  =          5                     v
    L103  +          i          1          t1
    ...
    

Implementation Details

Lexical Analysis

The lexical analyzer (a9_220101019.l) recognizes tokens in the microC language and passes them to the parser. It handles:

  • Keywords (int, float, if, while, etc.)
  • Identifiers
  • Constants (integer, floating-point, character)
  • String literals
  • Operators and punctuators
  • Comments (single-line and multi-line)

Syntax Analysis

The parser (a9_220101019.y) defines the grammar rules for microC and includes semantic actions to generate 3-address code. It builds:

  • A symbol table to track variables, functions, and their properties
  • A quad array to store the generated 3-address code

Semantic Analysis and Code Generation

During parsing, semantic actions:

  • Create and manage symbol tables for different scopes
  • Generate temporary variables for intermediate results
  • Emit 3-address quads for expressions, statements, and control flow
  • Handle type checking and conversions

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published