/LR-0-Parser

An LR(0) parser implementation in python. This repository includes the source code, documentation and examples for building and understanding LR(0) parsers.

Primary LanguagePython

LR(0) Parser

This repository contains a Python implementation of an LR(0) parser. The LR(0) parser is a type of bottom-up parser for analyzing context-free grammars used in compilers and interpreters.

Features

  • Computes the closure of a production
  • Handles state transitions with the goto function
  • Generates parsing tables for terminals and non-terminals
  • Parses and validates input strings

Installation

Prerequisites

  • Python 3.6 or higher (3.9.12 recommended)
  • termtables library for displaying parsing tables
  • collections library for counting occurences

Steps

  1. Clone the repository:

    git clone https://github.com/yourusername/LR0-parser.git
    cd LR0-parser
  2. Install required packages:

    pip install termtables
    pip install collections

Usage

  1. Run the script:

    python lr0_parser.py
  2. Enter productions: You will be prompted to enter grammar productions one by one. Type end when you are done.

    Productions

  3. View augmented grammar and states: The script will display the augmented grammar and the generated states.

    Productions

  4. Parsing table: The parsing table will be displayed.

    Parsing Table

  5. Enter the string to be parsed:

    Enter the string to be parsed: aabb
    
  6. Parsing result: The script will display whether the string is parsable and provide the parsing table.

    Parsing Result