Created
June 21, 2018 01:56
-
-
Save marcelogdeandrade/16cc93151c50037a8be51f3fbfa5ca96 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from rply import LexerGenerator | |
class Lexer(): | |
def __init__(self): | |
self.lexer = LexerGenerator() | |
def _add_tokens(self): | |
self.lexer.add('PRINT', r'print') | |
# Parenthesis | |
self.lexer.add('OPEN_PAREN', r'\(') | |
self.lexer.add('CLOSE_PAREN', r'\)') | |
# Semi Colon | |
self.lexer.add('SEMI_COLON', r'\;') | |
# Operators | |
self.lexer.add('SUM', r'\+') | |
self.lexer.add('SUB', r'\-') | |
# Number | |
self.lexer.add('NUMBER', r'\d+') | |
# Ignore spaces | |
self.lexer.ignore('\s+') | |
def get_lexer(self): | |
self._add_tokens() | |
return self.lexer.build() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment