Skip to main content

Library for automatic construction of lexers

Project description

About LexBuilder:

LexBuilder is a library for automatically building a lexer in Python and C++. In the future, the library will be able to build lexers in major programming languages such as Golang, Java/Kotlin, etc.

About Syntax:

In order for the library to generate a Lexer.py or Lexer.h file, you need to pass a list of tokens to the Builder class or use its .add_token() method. To declare a token, you need to import the Token() class from the Builder() class. You need to pass the token name and its value to the Token() class. After that, add all the tokens we created to a list and pass it as an argument to the PyBuilder() or CppBuilder class. By default, the lexer contains the tokens:

INT_NUMBER = "INT_NUMBER"
FLOAT_NUMBER = "FLOAT_NUMBER"
STRING = "STRING"
PLUS = "PLUS"
MINUS = "MINUS"
VAR = "VAR"
EOF = "EOF"

Example of creating a list of tokens:

from LexBuilder.Builder import Token

DIVIDE = Token("DIVIDE", "/")
PRINT = Token("PRINT", "print")
INPUT = Token("INPUT", "input")

tokens = [DIVIDE, PRINT, INPUT]

Python Example:

Generate Lexer:

from LexBuilder.Builder import PyBuilder, Token


MULTIPLY = Token("MULTIPLY", "*")
DIVIDE = Token("DIVIDE", "/")
PRINT = Token("PRINT", "print")

tokens = [MULTIPLY, DIVIDE, PRINT]

lexer = PyBuilder(tokens)

lexer.add_token(Token("INPUT", "input"))
lexer.add_token(Token("LPAREN", "("))
lexer.add_token(Token("RPAREN", ")"))

lexer.build()

Use Lexer:

from Lexer import *


code = 'print("Hello, world!")'
lexer = Lexer(code)

token = lexer.get_next_token()
print(token)

while token.type != EOF:
    token = lexer.get_next_token()
    print(token)
Token(PRINT, "print")
Token(LPAREN, "(")
Token(STRING, "Hello, world!")
Token(RPAREN, ")")
Token(EOF, None)

C++ Example:

Generate Lexer:

from LexBuilder.Builder import CppBuilder, Token


MULTIPLY = Token("MULTIPLY", "*")
DIVIDE = Token("DIVIDE", "/")
PRINT = Token("PRINT", "print")

tokens = [MULTIPLY, DIVIDE, PRINT]

lexer = CppBuilder(tokens)

lexer.add_token(Token("INPUT", "input"))
lexer.add_token(Token("LPAREN", "("))
lexer.add_token(Token("RPAREN", ")"))

lexer.build()

Use Lexer:

#include "Lexer.h"

using namespace std;


int main() {
    string code = "5 + 5";
    Lexer lexer(code);
    Token current_token = lexer.get_next_token();
    cout << current_token << endl;
    while (current_token.type != EOF_TOKEN) {
        current_token = lexer.get_next_token();
        cout << current_token << endl;
    }
}
Token(INT_NUMBER, 5)
Token(PLUS, +)
Token(INT_NUMBER, 5)
Token(EOF, )

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

LexBuilder-1.2.0.tar.gz (5.1 kB view hashes)

Uploaded Source

Built Distribution

LexBuilder-1.2.0-py3-none-any.whl (6.1 kB view hashes)

Uploaded Python 3

Supported by

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