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 the Lexer.py or Lexer.h file, you need to pass a list of tokens to the Builder class. 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. Afterwards, 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
DIVIDE = Token("DIVIDE", "/")
PRINT = Token("PRINT", "print")
INPUT = Token("INPUT", "input")
tokens = [DIVIDE, PRINT, INPUT]
lexer = PyBuilder(tokens)
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(STRING, "Hello, world!")
Token(EOF, None)
C++ Example:
Generate Lexer:
from LexBuilder.Builder import CppBuilder, Token
DIVIDE = Token("DIVIDE", "/")
PRINT = Token("PRINT", "print")
INPUT = Token("INPUT", "input")
tokens = [DIVIDE, PRINT, INPUT]
lexer = CppBuilder(tokens)
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for LexBuilder-1.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0d7d1b588a680e90e12e1b09d1db72f2568f32ad0ce97375fafb0b38f6a0aef5 |
|
MD5 | ef1e044e53be5f176e06a1a3d414445b |
|
BLAKE2b-256 | 2f8f5e775b905c425d45b8a6a5749250145eb12e174a60d552cc70bf9cb11e2c |