add lexer skeleton
This commit is contained in:
commit
2b4de69ff8
0
parser/__init__.py
Normal file
0
parser/__init__.py
Normal file
38
parser/lexer.py
Normal file
38
parser/lexer.py
Normal file
@ -0,0 +1,38 @@
|
||||
|
||||
import ply.lex as lex
|
||||
|
||||
reserved = {
|
||||
'C': 'C',
|
||||
}
|
||||
|
||||
tokens = [
|
||||
'LPAREN',
|
||||
'RPAREN',
|
||||
'SEMICOLON',
|
||||
'STRING',
|
||||
'ID',
|
||||
] + list(reserved.values())
|
||||
|
||||
t_LPAREN = r'\('
|
||||
t_RPAREN = r'\)'
|
||||
t_SEMICOLON = r';'
|
||||
t_STRING = r'"([^"])*"'
|
||||
|
||||
t_ignore = ' \t\r'
|
||||
|
||||
def t_ID(t):
|
||||
r'[a-zA-Z_][a-zA-Z_0-9]*'
|
||||
t.type = reserved.get(t.value, 'ID')
|
||||
return t
|
||||
|
||||
def t_newline(t):
|
||||
r'\n+'
|
||||
t.lexer.lineno += len(t.value)
|
||||
return None
|
||||
|
||||
def t_error(t):
|
||||
print 'Illegal character "%s"' % t.value[0]
|
||||
t.lexer.skip(1)
|
||||
|
||||
def get_lexer():
|
||||
return lex.lex()
|
Loading…
x
Reference in New Issue
Block a user