very powerful and optional parser framework for python
Project description
EBNFParser
Parse Many, Any, Every
Multi-Language-Versions
Install
Python
pip
pip installl -U EBNFParser
setup
git clone https://github.com/thautwarm/EBNFParser cd EBNFParser/Python python setup.py install
An Introduce to EBNFParser
Here is an example for you to get a knowledge of Ruikowa for parsing Java switch syntax.
Token Token.Java # use the token definition at source file `./Token/Java`.
newline ::= R'\n' # match by using regular expression
switch ::= 'switch' '(' expression ')' newline*
'{'
(case | newline )*
[default newline*]
'}' ;
case ::= 'case' ':' body ;
default ::= 'default' ':' body ;
body ::= block | statement ;
block ::= '{' (newline|statement)* '}' ;
...
Now I’m going to tell you how to use EBNFParser to write a parser for Lisp quickly.
Install
pip install -U EBNFParser
Write a file and name it as lispGrammar with following content.
Atom := R'[^\(\)\s\`]+'; # use Regex # define a literal parser. `Atom::= R'[^\(\)\s\']+'` is ok, but the ast parsed from the two is a little different with each other. Expr Throw ['\n'] ::= Atom | Quote | C'(' (NEWLINE* Expr* NEWLINE*)* C')' ; # C-prefix announces a character parser. Quote ::= C'`' Expr ; NEWLINE := C'\n' ; Stmt Throw ['\n'] ::= (NEWLINE* Expr* NEWLINE*)* ;
Generate your parser and tokenizer.
ruiko ./lispGrammar ./lispParser.py -comment True
Test your parser.
python testLang.py Stmt "(+ 1 2)" -o test Stmt[ Expr[ "(" Expr[ "+" ] Expr[ "1" ] Expr[ "2" ] ")" ] ]
Moreover, here is a result in JSON format at test.json.
Usage
Command Line Tools
ruiko.
ruiko <grammar File> <output Python File(endswith ".py")> [-comment <True/False>] # whether any comments in your grammar file.
Use command ruiko to generate parser and token files, and then you can use testLang.py to test your parser.
python testLang.py <AST Name> "<your codes>"
Source
Will support C# and Elixir sooner.
License
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 Distributions
Built Distribution
Hashes for EBNFParser-1.0.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 134d691fdb79ab0603889d8b3e858dc73e65c09fe9ebc4cf737e8b94ea0378a6 |
|
MD5 | 1addbf49583bdb01bde679d9b7c76205 |
|
BLAKE2b-256 | 0bc94c13e640bf8f09ef4dc2395fda5f308dd6a6b6791a2e3db751cb8d38b1fe |