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
- Unary
- 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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file animated-parse-tree-0.0.10.tar.gz.
File metadata
- Download URL: animated-parse-tree-0.0.10.tar.gz
- Upload date:
- Size: 18.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.8.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ae81e60fd5e35ee466e08a418bced6e54a223bef2a895a32b6acd8595566e187
|
|
| MD5 |
873b0cdc688b00c6070e9c1f1acaa962
|
|
| BLAKE2b-256 |
696859bcaf17d19529f0a8fc72f6dab4289a26f6fbbe117c4ee59ef7f9f9388a
|
File details
Details for the file animated_parse_tree-0.0.10-py3-none-any.whl.
File metadata
- Download URL: animated_parse_tree-0.0.10-py3-none-any.whl
- Upload date:
- Size: 24.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.8.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
342ee2e2a19dbb879210cb0aa6f7a6f39a84b9a0f491e77aa49f197e06089e82
|
|
| MD5 |
6a46f75842616e3394eacf4681a89b01
|
|
| BLAKE2b-256 |
462266ec3fdd7f0c9221efe77094869a9e4fa650ef459fd22bf28f5972d7c82f
|