Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/gregorybchris/markers
Boolean formula parsing
https://github.com/gregorybchris/markers
binary boolean equation formula interpreter parser parsing pratt sat
Last synced: 7 days ago
JSON representation
Boolean formula parsing
- Host: GitHub
- URL: https://github.com/gregorybchris/markers
- Owner: gregorybchris
- Created: 2024-08-14T18:20:27.000Z (5 months ago)
- Default Branch: main
- Last Pushed: 2024-12-24T09:38:09.000Z (11 days ago)
- Last Synced: 2024-12-24T11:08:28.193Z (11 days ago)
- Topics: binary, boolean, equation, formula, interpreter, parser, parsing, pratt, sat
- Language: Python
- Homepage:
- Size: 108 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
Markers
Simple parser for boolean formulas
## About
Markers is a parser for boolean formulas. You can provide it an expression like `a and not (b or c)` as well as variable assignments like `a = true, b = false, c = true` and Markers will evaluate `true and not (false or true)` => `false`.
Supported tokens are variables, `and`, `or`, `not`, and parentheses.
If you care about the parse tree and not the evaluation on a set of variable assignments you can also use Markers to just parse an expression and emit a recursive Python structure of boolean operators and variables.
## Installation
[Poetry](https://python-poetry.org/) is a requirement
```bash
poetry install
```## CLI usage
### Parsing a boolean formula
```bash
$ poetry run markers parse "not a or b"
> BinaryOp(
> kind=BinaryOpKind.OR,
> left=UnaryOp(
> kind=UnaryOpKind.NOT,
> arg=Var(name='a')
> ),
> right=Var(name='b')
> )
```### Evaluating a boolean formula with variable assignments
```bash
$ poetry run markers eval "not a or b" -t b -f a
> True
```## Running tests
```bash
pytest tests
```## Motivation
I started working on Markers when I realized that [pytest](https://docs.pytest.org) doesn't expose the internals of its [marks](https://docs.pytest.org/en/stable/reference/reference.html#custom-marks) feature to plugins. You can hook into the results of evaluating the boolean formula, but not the unevaluated parse tree. Markers per function or class can be accessed on `TestItem` objects, for example through the `handle_test_completion` hook.
The name "Markers" is a nod to the fact that pytest is inconsistent with its use of "mark" and "marker".
## Acknowledgements
This project borrowed heavily from some online resources and human minds about parsing in general and boolean formula parsing in particular.
- [Eoin Davey's Blog](https://vey.ie/2018/10/04/RecursiveDescent.html)
- [Scrapscript Interpreter](https://github.com/tekknolagi/scrapscript)
- [Typped Pratt Parsing](https://abarker.github.io/typped/pratt_parsing_intro.html)
- [Max Bernstein](https://bernsteinbear.com)