• Stars
    star
    804
  • Rank 56,681 (Top 2 %)
  • Language
    Python
  • License
    Other
  • Created about 8 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Sly Lex Yacc

SLY (Sly Lex-Yacc)

SLY is a 100% Python implementation of the lex and yacc tools commonly used to write parsers and compilers. Parsing is based on the same LALR(1) algorithm used by many yacc tools. Here are a few notable features:

  • SLY provides very extensive error reporting and diagnostic information to assist in parser construction. The original implementation was developed for instructional purposes. As a result, the system tries to identify the most common types of errors made by novice users.
  • SLY provides full support for empty productions, error recovery, precedence specifiers, and moderately ambiguous grammars.
  • SLY uses various Python metaprogramming features to specify lexers and parsers. There are no generated files or extra steps involved. You simply write Python code and run it.
  • SLY can be used to build parsers for "real" programming languages. Although it is not ultra-fast due to its Python implementation, SLY can be used to parse grammars consisting of several hundred rules (as might be found for a language like C).

SLY originates from the PLY project. However, it's been modernized a bit. In fact, don't expect any code previously written for PLY to work. That said, most of the things that were possible in PLY are also possible in SLY.

SLY is a modern library for performing lexing and parsing. It implements the LALR(1) parsing algorithm, commonly used for parsing and compiling various programming languages.

Important Notice : October 11, 2022

The SLY project is no longer making package-installable releases. It's fully functional, but if choose to use it, you should vendor the code into your application. SLY has zero-dependencies. Although I am semi-retiring the project, I will respond to bug reports and still may decide to make future changes to it depending on my mood. I'd like to thank everyone who has contributed to it over the years. --Dave

Requirements

SLY requires the use of Python 3.6 or greater. Older versions of Python are not supported.

An Example

SLY is probably best illustrated by an example. Here's what it looks like to write a parser that can evaluate simple arithmetic expressions and store variables:

# -----------------------------------------------------------------------------
# calc.py
# -----------------------------------------------------------------------------

from sly import Lexer, Parser

class CalcLexer(Lexer):
    tokens = { NAME, NUMBER, PLUS, TIMES, MINUS, DIVIDE, ASSIGN, LPAREN, RPAREN }
    ignore = ' \t'

    # Tokens
    NAME = r'[a-zA-Z_][a-zA-Z0-9_]*'
    NUMBER = r'\d+'

    # Special symbols
    PLUS = r'\+'
    MINUS = r'-'
    TIMES = r'\*'
    DIVIDE = r'/'
    ASSIGN = r'='
    LPAREN = r'\('
    RPAREN = r'\)'

    # Ignored pattern
    ignore_newline = r'\n+'

    # Extra action for newlines
    def ignore_newline(self, t):
        self.lineno += t.value.count('\n')

    def error(self, t):
        print("Illegal character '%s'" % t.value[0])
        self.index += 1

class CalcParser(Parser):
    tokens = CalcLexer.tokens

    precedence = (
        ('left', PLUS, MINUS),
        ('left', TIMES, DIVIDE),
        ('right', UMINUS),
        )

    def __init__(self):
        self.names = { }

    @_('NAME ASSIGN expr')
    def statement(self, p):
        self.names[p.NAME] = p.expr

    @_('expr')
    def statement(self, p):
        print(p.expr)

    @_('expr PLUS expr')
    def expr(self, p):
        return p.expr0 + p.expr1

    @_('expr MINUS expr')
    def expr(self, p):
        return p.expr0 - p.expr1

    @_('expr TIMES expr')
    def expr(self, p):
        return p.expr0 * p.expr1

    @_('expr DIVIDE expr')
    def expr(self, p):
        return p.expr0 / p.expr1

    @_('MINUS expr %prec UMINUS')
    def expr(self, p):
        return -p.expr

    @_('LPAREN expr RPAREN')
    def expr(self, p):
        return p.expr

    @_('NUMBER')
    def expr(self, p):
        return int(p.NUMBER)

    @_('NAME')
    def expr(self, p):
        try:
            return self.names[p.NAME]
        except LookupError:
            print(f'Undefined name {p.NAME!r}')
            return 0

if __name__ == '__main__':
    lexer = CalcLexer()
    parser = CalcParser()
    while True:
        try:
            text = input('calc > ')
        except EOFError:
            break
        if text:
            parser.parse(lexer.tokenize(text))

Documentation

Further documentation can be found at https://sly.readthedocs.io/en/latest.

Talks

Resources

For a detailed overview of parsing theory, consult the excellent book "Compilers : Principles, Techniques, and Tools" by Aho, Sethi, and Ullman. The topics found in "Lex & Yacc" by Levine, Mason, and Brown may also be useful.

The GitHub page for SLY can be found at:

https://github.com/dabeaz/sly

Please direct bug reports and pull requests to the GitHub page. To contact me directly, send email to [email protected] or contact me on Twitter (@dabeaz).

-- Dave

P.S.

You should come take a course!

More Repositories

1

curio

Good Curio!
Python
4,001
star
2

python-cookbook

Code samples from the "Python Cookbook, 3rd Edition", published by O'Reilly & Associates, May, 2013.
Python
3,864
star
3

ply

Python Lex-Yacc
Python
2,717
star
4

dataklasses

A different spin on dataclasses.
Python
782
star
5

bitey

Python
606
star
6

generators

Generator Tricks for Systems Programmers (Tutorial)
422
star
7

cluegen

Get a clue, get some code
Python
355
star
8

thredo

Thredo was an experiment - It's dead. Feel free to look around.
Python
338
star
9

blog

David Beazley's blog.
261
star
10

concurrencylive

Code from Concurrency Live - PyCon 2015
Python
157
star
11

python-distilled

Resources for Python Distilled (Book)
85
star
12

modulepackage

Materials for PyCon2015 Tutorial "Modules and Packages : Live and Let Die"
Python
74
star
13

wadze

Web Assembly Decoder - Zero Extras
Python
70
star
14

me-al

Meแบ—al - The Decorator
Python
64
star
15

pythonprog

Python
62
star
16

typemap

Typemap - The Annotator (TM)
Python
55
star
17

pylox

Python implementation of the Lox language from Robert Nystrom's Crafting Interpreters
Python
47
star
18

flail

Ball and Chain Decorators
Python
41
star
19

asm6502

A small 6502 assembler written in Python
Python
21
star
20

ranet

An Implementation of Raft in Janet
18
star
21

hoppy

9
star
22

raft_jun_2019

Raft Course 2019
Python
9
star
23

colonel

C Curio Kernel
Python
8
star
24

raft_dec19

Raft Project December 2019
Python
8
star
25

theater

Code for "The Problem with the Problem" talk, February 22, 2022.
Python
7
star
26

dabeaz.github.io

HTML
4
star
27

frothy

Frothy example from CSCI1730 Fall 2023
Racket
3
star
28

archive

Software Archive
3
star