Arcee
It is a Python parser generator, use EBNF-like syntax.
Install
$ pip install Arcee
Example
It's really readable.
grammar:
KEYWORDS : let, if, zero, -
NUMBER : \d+(\.\d*)?
ASSIGN : =
SUBTRACTION : -
RIGHT_BRACKET : (
COLON : ,
LETF_BRACKET : )
ID : [A-Za-z]+
SKIP : [ \\t]+
program : expression ;
expression : zeroexp
| diffexp
| ifexp
| varexp
| letexp
| constexp
;
constexp : $NUMBER ;
diffexp : '-' '(' expression ',' expression ')' ;
zeroexp : 'zero' '(' expression ')' ;
ifexp : 'if' expression 'then' expression 'else' expression ;
varexp : $ID ;
letexp : 'let' $ID '=' expression 'in' expression ;
$ arcee grammar > result.py
result.py has three parts:
Token
from collections import namedtuple
Token = namedtuple('Token', ['type', 'value', 'line', 'column'])
Program = namedtuple('Program', ['expression'])
# ...
Lexer
import re
def tokenize(code):
pass # ...
Parser
class Parser:
def __init__(self, token_list):
pass
# ...
def parse_expression(self):
if xxx:
self.parse_constexp()
elif yyy:
self.parse_diffexp()
#...
def parse_constexp(self):
pass
def parse_diffexp(self):
pass
def parse_zeroexp(self):
pass
def parse_ifexp(self):
pass
def parse_varexp(self):
pass
def parse_letexp(self):
pass
You can parse input such as:
input = '''let a = 0 in if zero(a) then -(a, 1) else -(a, 2)'''
tokens = list(tokenize(input))
parser = Parser(tokens)
parser.parse_program()
result is:
result = Program(
expression=Expression(
nonterminal=Letexp(
ID=Token(type='ID', value='a', line=2, column=4),
expression1=Expression(
nonterminal=Constexp(
NUMBER=Token(type='NUMBER', value='0', line=2, column=8))),
expression2=Expression(
nonterminal=Ifexp(
expression1=Expression(
nonterminal=Zeroexp(
expression=Expression(
nonterminal=Varexp(
ID=Token(type='ID', value='a', line=2, column=21))))),
expression2=Expression(
nonterminal=Diffexp(
expression1=Expression(
nonterminal=Varexp(
ID=Token(type='ID', value='a', line=2, column=31))),
expression2=Expression(
nonterminal=Constexp(
NUMBER=Token(type='NUMBER', value='1', line=2,
column=34))))),
expression3=Expression(
nonterminal=Diffexp(
expression1=Expression(
nonterminal=Varexp(
ID=Token(type='ID', value='a', line=2, column=44))),
expression2=Expression(
nonterminal=Constexp(
NUMBER=Token(type='NUMBER', value='2', line=2,
column=47))))))))))
Now, you can use this ast to do what you like.
You can also use api in your Python file.
from arcee import generate
grammar = '''KEYWORDS : let, if, zero, -
NUMBER : \d+(\.\d*)?
ASSIGN : =
SUBTRACTION : -
RIGHT_BRACKET : (
COLON : ,
LETF_BRACKET : )
ID : [A-Za-z]+
SKIP : [ \\t]+
program : expression ;
expression : zeroexp
| diffexp
| ifexp
| varexp
| letexp
| constexp
;
constexp : $NUMBER ;
diffexp : '-' '(' expression ',' expression ')' ;
zeroexp : 'zero' '(' expression ')' ;
ifexp : 'if' expression 'then' expression 'else' expression ;
varexp : $ID ;
letexp : 'let' $ID '=' expression 'in' expression ;'''
print(generate(grammar))
Metadata
Release files for arcee 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| arcee-0.1.1.tar.gz | 8.7 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| arcee-0.1.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 22.8 kB
Release files / arcee-0.1.1.tar.gz
| Download URL | arcee-0.1.1.tar.gz |
|---|---|
| Size | 8.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f7703eac60d50426f369bc89e8c612c153b2962f15e8482784ba07f03b557e56
|
|
BLAKE2b-256 checksum How to use checksums |
119361bd2b1c2c0731ab52db9782dc2175e35d587a5a9862133649b4c9f08bc0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.7.0
|
Release files / arcee-0.1.1-py3-none-any.whl
| Download URL | arcee-0.1.1-py3-none-any.whl |
|---|---|
| Size | 14.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7146b1c5ad723183b4bddca5c62b6e96d6784703d0b5b5a6994d704a383d8d7b
|
|
BLAKE2b-256 checksum How to use checksums |
4115ec6a216378600e397eec0e6c703fdfc8014f41960c8e326f8be621fa6cfb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.7.0
|
Security for arcee 0.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.