A parser for simple logical expressions of identifiers
Project description
Lexpr: A simple logical expressions parser
Lexpr is a simple package containing a logical expressions parser developed using a Lark grammar.
The expressions may contain:
- entity identifiers
- the binary operators
|
(or),&
(and) - the unary operator
!
(not). - balanced pairs of round parentheses
Installation
The package can be installed using pip install lexpr
.
Usage
A parser is created and used for parsing the text, as in the following example:
import lexpr
lp = lexpr.Parser()
lp.parse("(G1 & G2) | !G3")
#
# output:
#
# Tree(
# Token('RULE', 'start'),
# [Tree(Token('RULE', 'entity'),
# [Tree(Token('RULE', 'or_expr'),
# [Tree(Token('RULE', 'entity'),
# [Tree(Token('RULE', 'enclosed_expr'),
# [Tree(Token('RULE', 'entity'),
# [Tree(Token('RULE', 'and_expr'),
# [Tree(Token('RULE', 'entity'), [Token('IDENTIFIER', 'G1')]),
# Tree(Token('RULE', 'entity'), [Token('IDENTIFIER', 'G2')])])]
# )]
# )]
# ), Tree(Token('RULE', 'entity'),
# [Tree(Token('RULE', 'not_expr'),
# [Tree(Token('RULE', 'entity'), [Token('IDENTIFIER', 'G3')])]
# )]
# )]
# )]
# )]
# )
In case of an invalid string is passed to the parser, an exception is raised:
import lexpr
lp = lexpr.Parser()
lp.parse("G1 &")
# raises LexprParserError, unbalanced expression
lp.parse("G1 & G$")
# raises LexprParserError, invalid character in identifier
Implementation
The grammar is contained in the file lexpr/data/lexpr.g
.
The parser is in the module lexpr/parser.py
.
Errors raised by the module are defined in lexpr/error.py
and are instances of the class LexprError
or its
subclasses.
History
The package has been developed to support the parsing of the EGC format, for expressing expectations about the contents of prokaryotic genomes. In this format, groups of organisms can be combined using logical expressions of the form parsed by this package. The main implementation of the format is based on TextFormats, which, however does not support non-regular, indefinetly nested expressions, such as the logical expressions parsed here. Thus the parsing of this expressions has been developed separately in this package.
Acknowledgements
This package has been created in context of the DFG project GO 3192/1-1 “Automated characterization of microbial genomes and metagenomes by collection and verification of association rules”. The funders had no role in study design, data collection and analysis.
Project details
Release history Release notifications | RSS feed
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
File details
Details for the file lexpr-0.1.tar.gz
.
File metadata
- Download URL: lexpr-0.1.tar.gz
- Upload date:
- Size: 4.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4a2cf447634bd9f27c03b0c45352d132a020854ffa481d3a465f36cfae0717d3 |
|
MD5 | dbc5ff383f2b6fa7be83aabba9cdb50d |
|
BLAKE2b-256 | e17034a6a0736d5233b164922f8e9f78d11d8b4f433e30c4124bfd849e0dedd7 |
File details
Details for the file lexpr-0.1-py3-none-any.whl
.
File metadata
- Download URL: lexpr-0.1-py3-none-any.whl
- Upload date:
- Size: 4.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0941a73301fb808b3b41718b4ecd4a9cd258e36766795a26f831e869a189157b |
|
MD5 | dd23f2e0c603799767a188be2c911501 |
|
BLAKE2b-256 | d72c90dc3c45c423a20f5af3f6b3415b0d1b087bfc00698c82a96f605ad2754c |