Skip to main content

A simple parse tree utility package, with animated evaluations and configurable tree displays

Project description

Animated Parse Tree

Author Ethan Tan
Date 11/12/2021
Language Python (py)

Description

This package is meant to provide a high-level API for programmers to visualise parse trees. The eventual goal is to animate the tokenization (extracting symbols from a string), lexing (formatting the tokens), parsing (building the parse tree) and evaluation (reducing the parse tree).

Setup

It is extremely easy to integrate this package in your existing projects.

In the command line, run:

pip install animated_parse_tree

Sample Usage

From Command Line

Then in your terminal/shell, enter:

python -m animated_parse_tree

The output should look similar to the following:

Greetings...

                "This is a utility program which aims
                    to show the beauty of parse trees
                           in a fun and engaging way"

Don't be intimidated :)
It was designed to be easy to use, yet extensible.

                                               Enjoy!
?>

Enter 'help' to display the help menu or 'mode' to change your current mode

From Source Files

Then in your Python source file / Jupyter Notebook, insert:

from animated_parse_tree import ParseTree

# Instantiate Parse Tree Object
t = ParseTree()

# Read a Mathematical String Expression (separated by singular whitespace characters)
t.read('1 + 2 * 3')

# Retrieve the Result
print('<<< Equation >>>')
print(t.expression, '=', t.evaluate(), end='\n\n')

# Display the Parse Tree
print('<<< Parse Tree >>>')
print(str(t))

The output in the terminal will look something like this:

<<< Equation >>>
1 + 2 * 3 = 7

<<< Parse Tree >>>
 +
/  \
1  *
  / \
  2 3

Declaring Custom Operands/Operators

A Bundle is simply a list of Operands and/or Operators.

To extend functionality, one simply has to register their own custom operands/operators like so:

# Instantiate Parse Tree Object
t = ParseTree()

# Declaring Custom Operator
op = Operator(symbol='if', func=lambda a,b,c: b if a == 1 else c, priority=7, kind='pre', operands=3)

# Register the Custom Operator(s)
t.register(bundle=[op])

# Use it in Expressions
t.read('if(1, 8, -1)')
print(t.evaluate())

Current Support

  • Operands
    • Integers
    • Floats
    • Constants (like pi, e, etc.)
  • Operators
    • Unary
      • Pre-fix
      • Post-fix
    • Binary
      • Pre-fix
      • In-fix
    • Multi
      • Pre-fix
  • Parentheses
    • Unlimited nesting permitted
    • Not encouraged for animation currently

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

animated-parse-tree-0.0.11.tar.gz (18.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

animated_parse_tree-0.0.11-py3-none-any.whl (24.6 kB view details)

Uploaded Python 3

File details

Details for the file animated-parse-tree-0.0.11.tar.gz.

File metadata

  • Download URL: animated-parse-tree-0.0.11.tar.gz
  • Upload date:
  • Size: 18.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.5

File hashes

Hashes for animated-parse-tree-0.0.11.tar.gz
Algorithm Hash digest
SHA256 2a478f4b14b706260db14eb72f39d6f2246fe5da3da3e146a448a4427bf5f138
MD5 50f1abd7fe888f1be8fb5cb1e69d3455
BLAKE2b-256 d9b62968c5813bc8fdcf2ad058be921cb86ae9f436b291a1a8a8b323e81e4047

See more details on using hashes here.

File details

Details for the file animated_parse_tree-0.0.11-py3-none-any.whl.

File metadata

  • Download URL: animated_parse_tree-0.0.11-py3-none-any.whl
  • Upload date:
  • Size: 24.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.5

File hashes

Hashes for animated_parse_tree-0.0.11-py3-none-any.whl
Algorithm Hash digest
SHA256 1a269ae8237638368f04b36eb693773a293a40fb4833b4c84a6db335a95e2a43
MD5 ca5c50e0964dc1754c433dbabe7afba8
BLAKE2b-256 dba731582f0aa3440c970339aed7a302b358665ba2b1dc876cf764e71834b519

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page