Skip to content

qweeze/python-parser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

python-parser

A recursive descent parser generator

Build Status

Usage

To create a parser you should provide a sequence of tokens and a BNF-like grammar. Here's a simple example of parsing an expression with nested parentheses:

from python_parser import Parser, a, anyof, someof, maybe, skip, to_dot

tokens = (
    ('\(', 'L_PAR'),
    ('\)', 'R_PAR'),
    ('\,', 'SEP'),
    ('\d+', 'NUM'),
    ('"\w+"', 'STR')
)
grammar = {
    'EXPR': a(
        skip('L_PAR'),
        'VALUE', maybe(someof(skip('SEP'), 'VALUE')),
        skip('R_PAR')
    ),
    'VALUE': anyof('STR', 'NUM', 'EXPR')
}
string_to_parse = '(1, 2, ("test", ((3), 4)))'

parser = Parser(tokens, grammar)
ast = parser.parse('EXPR', string_to_parse)

with open('ast.dot', 'w') as f:
    f.write(to_dot(ast))

After running this code ast.dot should contain the following graph:

Ast graph

More examples

Calculator

> (((23 + -3.2)) / 34+(2*((3)) *(3.0 - .1)))
  17.98235294117647
> 1/0
  float division by zero
> a = 11
> b = -6
> a * b
  -66.0
> 

JSON parser

>>> from python_parser.examples.json_parser import load as json_loads
>>> json_string = '{"first": [1, 2, 3, {"5": 6}, true], "key": "value", "1": 2}'
>>> result = json_loads(json_string)
>>> type(result)
builtins.dict
>>> result['first'][3]['5']
6

About

A recursive descent parser

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages