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()

# Declare Custom Operand/Operator(s)
op = Operator(symbol='sum', func=lambda *args: sum(args), priority=Priority.PRE, kind='pre', operands=4)

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

# Use it in Expressions
t.read('sum(0.1, 2.5, 30, 49)')

# Animate all the Processes
t.animate(seconds_per_frame=1.0)

# Display the Parse Tree
print(str(t))

# Evaluate the Expression
print(t.evaluate())

Current Support

  • Operands
    • Integers (-1, 0, 2345)
    • Floats (0.0, 2.5, -0.77)
    • Constants (pi, e)
  • Operators
    • Unary
      • Pre-fix (lg, -)
      • Post-fix (deg, !)
    • Binary
      • Pre-fix (log)
      • In-fix (+, -, *, /, ^)
    • Multi
      • Pre-fix (if)
  • Parentheses
    • Unlimited nesting

Other Features Supported

  • Implicit Operator (default is higher-priority multiplication)
  • Operator Overloading (maximum of 2: 1 pre-fix and 1 non-pre-fix)
  • Custom Word Operands/Operators (operands/operators whose symbols are lowercase alphabetical characters)
  • Custom Symbol Operands/Operators (operands/operators whose symbols are special characters)

See Also

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.12.tar.gz (21.0 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.12-py3-none-any.whl (26.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: animated-parse-tree-0.0.12.tar.gz
  • Upload date:
  • Size: 21.0 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.12.tar.gz
Algorithm Hash digest
SHA256 1f183d184eaf21f8d09b9a92de8eefa172e2a7063226975fab66acb024849475
MD5 b0e7ff4e3d84b10f954c4486370dce89
BLAKE2b-256 dffbb38d984a6c7d73551b3f1bce7cf84a6817676ec0ff56e594766cefedea89

See more details on using hashes here.

File details

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

File metadata

  • Download URL: animated_parse_tree-0.0.12-py3-none-any.whl
  • Upload date:
  • Size: 26.8 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.12-py3-none-any.whl
Algorithm Hash digest
SHA256 810c67fd1035816aea209e2dcadf453608e48f72b0286c0abf3d87ed08e2cc28
MD5 ae04216294a9b491e1dd0f8637589fca
BLAKE2b-256 4006e358ac1e0c033b2893fdbe4f783ee4db145c08f543d455ec4c306389297b

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