Skip to content

Compiler -translates Pascal syntax into MIPS using Bison and Flex and writing in Lex, Yacc and C.

License

Notifications You must be signed in to change notification settings

TarzEH/Mips_Compiler

Repository files navigation

Pascal-MIPS Compiler

A compiler that translates Pascal syntax into MIPS assembly language using Bison and Flex, implemented in Lex, Yacc, and C.

Features

  • Pascal to MIPS assembly translation
  • Lexical analysis using Flex
  • Syntax parsing using Bison
  • Error detection and reporting
  • MIPS code generation

Building the Compiler

Prerequisites

  • GCC compiler
  • Flex (lexical analyzer generator)
  • Bison (parser generator)
  • Make (optional, for automated building)

Build Instructions

Using Make (Recommended)

make

Manual Build

bison -d CPM.y
flex CPM.lex
gcc -o CPM.exe lex.yy.c y.tab.c

How to Use

Prerequisites

  • Built compiler executable (CPM.exe)
  • Pascal source files to compile

Steps

  1. Open Command Prompt and navigate to the directory where you extracted the compiler files

  2. Select Input Files - Follow the interface shown below to choose your Pascal source files:

    File Selection Interface

  3. Execute Compilation - Press Enter to start the compilation process

    • Success: If no errors occur, a MIPS assembly file will be generated
    • Error: If compilation fails, an error text file will be created with details
  4. Compilation Process:

    Compilation in Progress

  5. Successful Output:

    Generated MIPS File

Error Handling

The compiler provides comprehensive error reporting. Below are examples of different error scenarios:

Syntax Errors

Syntax Error Example

Compilation Errors

Compilation Error Details

Error File Generation

Error File Created

Error Log Content

Error Log Example

Output

  • Success: .mips file containing the translated assembly code
  • Error: .txt file containing detailed error messages and line numbers

Project Structure

Mips_Compiler/
├── CPM.lex          # Lexical analyzer specification
├── CPM.y            # Grammar specification (Bison)
├── CPM.exe          # Compiled executable
├── examples/        # Test cases
├── Makefile         # Build automation
├── README.md        # This file
├── WorkingExample.txt    # Valid Pascal code
└── NotWorkingExample.txt # Invalid Pascal code (for testing)

Example Programs

The examples/ directory contains sample Pascal programs:

  • basic_arithmetic.txt - Arithmetic operations
  • control_flow.txt - If/else and while loops

Technologies Used

  • Flex: Lexical analyzer generator
  • Bison: Parser generator (successor to Yacc)
  • C: Implementation language
  • Pascal: Source language
  • MIPS: Target assembly language

Development

Clean Build

make clean
make

Testing

make test        # Test with working example
make test-error  # Test with error example

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

Compiler -translates Pascal syntax into MIPS using Bison and Flex and writing in Lex, Yacc and C.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published