Skip to main content

LL(1) parser generator

Project description

Arcee

Build Status

It is a Python parser generator, use EBNF-like syntax.

Example

It's really readable.

from arcee import generate

syntax = '''
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(syntax))

It 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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

arcee-0.1.0.tar.gz (8.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

arcee-0.1.0-py3-none-any.whl (14.0 kB view details)

Uploaded Python 3

File details

Details for the file arcee-0.1.0.tar.gz.

File metadata

  • Download URL: arcee-0.1.0.tar.gz
  • Upload date:
  • Size: 8.6 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for arcee-0.1.0.tar.gz
Algorithm Hash digest
SHA256 98ae03d685bd277acbf2d8a1fcdb4c6210e1a377fdc6d05d5fb4d83278733fb1
MD5 2973727f113b45db7d9827ffb1d7bf2f
BLAKE2b-256 d60bc2f46d0501831a39fe65adb44e84b53565e26d4b81d0e1e380f1835f4a8b

See more details on using hashes here.

File details

Details for the file arcee-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: arcee-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 14.0 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for arcee-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8b2b3e82bd28f1e29fe52b990dbc4f3bf8714b4a70a15b5d5e5430468a3db318
MD5 892c22bae60286bd506e8c9c8cb3a95f
BLAKE2b-256 228ae27fb9c0914f7a2561fe610a98db8e834f28da6b461881ab934914d22bcd

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page