/fort500-compiler

A repository for implementing a compiler for FORT500, a high-level language similar to FORTRAN but with structured commands and support for record structures.

Primary LanguageCMIT LicenseMIT

FORT500 Compiler

This repository hosts the development of a compiler for FORT500, a high-level language resembling FORTRAN with structured commands and record structures, supporting recursive subprogram definition.

Overview

Alt text

The FORT500 compiler project is divided into two main components: the Lexical Analyzer and the Syntax Analyzer. These components work together to analyze and process FORT500 source code.

Lexical Analyzer

The Lexical Analyzer, also known as the lexer or scanner, is responsible for breaking down the input source code into tokens. These tokens are the smallest meaningful units of the FORT500 language, such as keywords, identifiers, constants, and operators.

Syntax Analyzer

The Syntax Analyzer, also known as the parser, analyzes the structure of the source code according to the rules of the FORT500 grammar. It checks whether the sequence of tokens generated by the Lexical Analyzer follows the syntax rules defined by the language's grammar. If the sequence of tokens forms valid syntactic constructs, the Syntax Analyzer produces a parse tree or syntax tree, which represents the hierarchical structure of the program.

Repository Structure

  • Lexical Analyzer/: Contains the source code for the Lexical Analyzer component.
  • Syntax Analyzer/: Contains the source code for the Syntax Analyzer component.
  • LICENSE.txt: The license under which this project is distributed.
  • README.md: This README file providing an overview of the project.
  • CONTRIBUTING.md: Guidelines for contributing to the project.

Usage

  1. Clone the repository: git clone https://github.com/your-username/fort500-compiler.git
  2. Navigate to the project directory: cd fort500-compiler
  3. Build the Lexical Analyzer:
    # Navigate to the Lexical Analyzer directory
    cd "Lexical Analyzer/"
    # Compile the Lexical Analyzer
    flex lexer.l
    gcc -o lexer lex.yy.c -lm
    # Execute the lexical analyzer
    ./lexer fort500test1.f
    ./lexer fort500test2.f
  4. Build the Syntax Analyzer:
    # Navigate to the Syntax Analyzer directory
    cd "Syntax Analyzer/"
    # Generate syntax.tab.c and syntax.tab.h using Bison
    bison -v -d syntax.y
    # Compile the Syntax Analyzer
    gcc -o parser syntax.tab.c lex.yy.c hashtbl.c -lm
    # Return to the main project directory
    cd ..
    

Usage

  1. Clone the repository: git clone https://github.com/your-username/fort500-compiler.git
  2. Navigate to the project directory: cd fort500-compiler
  3. Build the compiler.
  4. Execute the compiler with a FORT500 source file as input: ./fort500-compiler <input_file.f500>

Compiler Pipeline

The FORT500 compiler follows a typical compilation pipeline involving the Lexical Analyzer and Syntax Analyzer:

  1. Lexical Analysis: The Lexical Analyzer scans the input FORT500 source code, breaking it down into tokens representing keywords, identifiers, constants, and operators.

  2. Syntax Analysis: The Syntax Analyzer parses the sequence of tokens generated by the Lexical Analyzer according to the rules of the FORT500 grammar. It ensures that the tokens form valid syntactic constructs and may produce a parse tree or syntax tree representing the program's structure.

These components work together seamlessly to process FORT500 source code and generate executable programs.

Tools Used

The development of the FORT500 compiler leverages the following tools:

  • Bison: Used for generating the Syntax Analyzer from the FORT500 grammar specifications.
  • Flex: Utilized for creating the Lexical Analyzer to tokenize the input source code.

Contribution Guidelines

Contributions are welcome! Please refer to the contribution guidelines for more information on how to contribute to this project.

License

This project is licensed under the MIT License.

License: MIT