Skip to content

jsinger67/pascal-py

Repository files navigation

Pascal Parser in Python

This project is an example on how to use parol-pygen in praxis. The used input grammar description pascal.par uses %grammar_type 'LALR(1)' directive to instruct parol to use an LR parser algorithm.

This project shows a scaffolded parser project generated by parol-pygen init.

Setup after fresh clone

This project requires Python 3.10+ and uses a local virtual environment in .venv.

Script variants are available for both platforms:

  • PowerShell: scripts/*.ps1
  • Bash: scripts/*.sh

Prerequisites

  1. Install Python >=3.10.

  2. Install uv (recommended).

  3. Verify tools are available:

    python --version
    uv --version

    On Windows a global python installation may also be accessible via py launcher utility which is sufficient for our purpose.

    py --version

Recommended workflow with uv

From the repository root:

If you are on Linux/macOS, make scripts executable once:

chmod +x ./scripts/*.sh
  1. Bootstrap the local environment:

    ./scripts/bootstrap.ps1
    ./scripts/bootstrap.sh

    This script:

    • creates .venv (if missing) via uv venv .venv
    • installs dependencies via uv sync
    • resolves parol-pygen from PyPI according to pyproject.toml
  2. Activate the virtual environment (optional but convenient for manual commands):

    .\.venv\Scripts\Activate.ps1
    source .venv/bin/activate
  3. Generate parser code:

    ./scripts/generate-parser.ps1
    ./scripts/generate-parser.sh
  4. Run the proof/example parser:

    ./scripts/run-proof.ps1 .\basics.pas
    ./scripts/run-proof.sh ./basics.pas

You can also run tools through uv without activating the environment, for example:

uv run python .\proof_runner.py .\basics.pas
uv run python ./proof_runner.py ./basics.pas

Alternative workflow with venv + pip

If you cannot use uv, you can still set up a working environment manually:

python -m venv .venv
.\.venv\Scripts\Activate.ps1
pip install -e .

How this was created

  1. Generate the export JSON pascal_export.json using Parol CLI:

    parol export -f .\pascal.par -o .\pascal_export.json --pretty

    To be able to use the Parol CLI, you should additionally install Parol, e.g. using cargo:

    cargo install parol

  2. Run ./scripts/bootstrap.ps1 or ./scripts/bootstrap.sh

  3. Run ./scripts/generate-parser.ps1 or ./scripts/generate-parser.sh

  4. Run ./scripts/run-proof.ps1 or ./scripts/run-proof.sh

Example:

PowerShell: ./scripts/run-proof.ps1 .\basics.pas

Bash: ./scripts/run-proof.sh ./basics.pas

Generated package

  • Package name: pascal_py
  • Entry parser class: pascal_py.Parser
  • User semantic action base: pascal_py.UserActions

About

A Pascal Parser in Python - example project on how to use `parol-pygen` in praxis

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors