im using the python module ply.lex
to write a lexer. I got some of my tokens specified with regular expression but now im stuck. I've a list of Keywords
who should be a token
. data
is a list with about 1000 Keywords which should be all recognised as one sort of Keyword. This can be for example: _Function1 _UDFType2
and so on. All words in the list are separated by whitespaces thats it. I just want that lexer to recognise the words in this list, so that it would return a token of type `KEYWORD.
data = 'Keyword1 Keyword2 Keyword3 Keyword4'
def t_KEYWORD(t):
# ... r'\$' + data ??
return t
text = '''
Some test data
even more
$var = 2231
$[]Test this 2.31 + / &
'''
autoit = lex.lex()
autoit.input(text)
while True:
tok = autoit.token()
if not tok: break
print(tok)
So i was trying to add the variable to that regex, but it didnt work. I'm always gettin:
No regular expression defined for rule 't_KEYWORD'
.
Thank you in advance! John
data = 'Keyword1 Keyword2 Keyword3 Keyword4' def t_KEYWORD(t): r'\$' + data return t
– Cosbyt_KEYWORD
in the regex? – SusurrateERROR: /Users/John/Lexer/lexer.py:21: No regular expression defined for rule 't_KEYWORD' Traceback (most recent call last): File "/Users/John/Lexer/lexer.py", line 77, in <module> autoit = lex.lex() File "/Library/Frameworks/Python.framework/Versions/3.0/lib/python3.0/site-packages/ply-3.4-py3.0.egg/ply/lex.py", line 894, in lex raise SyntaxError("Can't build lexer") SyntaxError: Can't build lexer
– Cosbyautoit = lex.lex()
that is throwing the traceback? It's not in the code that's provided. The code you provide is just defining a function and never actually does anything with regular expressions orply.lex
– Susurrateply
already has a decorator --TOKEN
-- to do some of the docstring magic people are suggesting. See here, for example. But I'm not sure if you want to construct 4 separate tokens and have each of them recognized separately (which this wouldn't do anyway), or if you have one keyword with four variations, or what. Could you edit your post to be a little more specific? – Damico