Skip to main content

This module will help you write simple tokenizers and parsers for your toy languages.

Project description

dlangtools

This module will help you write tokenizers and parsers for your toy languages.

Lex

Usage of CharIterator:

from dlangtools.lex import CharIterator

src = "Hello, world!"
it = CharIterator(src)

while not it.end():
  if it.check(str.isspace):
    it.skip(str.isspace)
  elif it.check(str.isalnum):
    print(it.location(), 'word:', it.consume(str.isalnum))
  elif it.check(',!'):
    print(it.location(), 'symbol', it.next())

Output:

(1, 1) word: Hello
(1, 6) symbol ,
(1, 8) word: world
(1, 13) symbol !

Parser

The module has only (for now) an LL(1) parser, which is sufficient for many simple cases such as arithmetic expressions.

Using of Parser:

from dlangtools.parsing import Parser
from dataclasses import dataclasses

@dataclass
class Token:
    type_: str
    value: str
    location: tuple # position in source code (line, column)

tokens = [
    Token(type_='number',    value='1', location=(1, 1)),
    Token(type_='operation', value='+', location=(1, 3)),
    Token(type_='number',    value='2', location=(1, 5)),
    Token(type_='operation', value='*', location=(1, 7)),
    Token(type_='number',    value='3', location=(1, 9)),
]

# BNF form for parser below:
# <additive> ::= <primary> | <primary> '+' <additive>
#  <primary> ::= <value> | <value> '*' <primary>
#    <value> ::= <number> | '(' <additive> ')'

def parse(tokens):
    p = Parser(tokens)

    def value():
        if p.match(('symbol', '(')):
            expr = additive()
            p.consume(('symbol', ')'))
            return expr
        elif p.check('number'): 
            return int(p.next().value)
        else:
            p.error('"(" or number expected instead of "%s"')

    def primary():
        first = value()
        if p.match(('operation', '*')):
            second = primary()
            return ('mul', first, second)
        return first
    
    def additive():
        first = primary()
        if p.match(('operation', '+')):
            second = additive()
            return ('sum', first, second)
        return first
    
    return additive()

tree = parse(tokens)
print(tree) # ('sum', 1, ('mul', 2, 3))

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

dlangtools-0.0.1.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

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

dlangtools-0.0.1-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file dlangtools-0.0.1.tar.gz.

File metadata

  • Download URL: dlangtools-0.0.1.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.5

File hashes

Hashes for dlangtools-0.0.1.tar.gz
Algorithm Hash digest
SHA256 7b77f718df9a541e135b6d63579c63d7ed69182a96d9a6dcc934e4e354b9ff53
MD5 989b4ef08d82352c8eaf8582df3d7298
BLAKE2b-256 3dd42471ae5efc1839f1ec4a3bb7460323fcaadb0ecd32034b225a8ac76dec2b

See more details on using hashes here.

File details

Details for the file dlangtools-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: dlangtools-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 4.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.5

File hashes

Hashes for dlangtools-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d472ce47b7d04550542e345129473cdd8862ca79ace6d2e230221f22511d8bf3
MD5 13117e2a08a627f6f8321ed1b3e57f4d
BLAKE2b-256 e9627ad48d2eed82ee2aaacd98e6116833ec441763e65f3d4dd1f190d40ab2dd

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