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.2.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.2-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: dlangtools-0.0.2.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.2.tar.gz
Algorithm Hash digest
SHA256 887de1835e0253940ae10580a2971fc78f35e15c9b73331236674d130e25636a
MD5 60f6b330eebf24dd378b0787a50e9a3f
BLAKE2b-256 7a38892fd8f56ec989a6fc72fd8ebbd398d2ad8b5b3ece35774f54495cf290ca

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dlangtools-0.0.2-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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 e895161c4065397cd5bdcab02432986a60887b8ba7e8ae6b7f85d6e62572079a
MD5 9ecdd47dfd4498a56a49450999142534
BLAKE2b-256 8927f9ef8e4f1de956d3919a3de18e825b21e99f6653a7ef7f101c2f5156a1c6

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