We had been using version 3.2 from 2009, which does not have support for t_eof(). Change-Id: Id5610a272fe2cecd586991f4c59f3ec77184164e Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/56342 Reviewed-by: Jason Lowe-Power <power.jg@gmail.com> Reviewed-by: Bobby Bruce <bbruce@ucdavis.edu> Maintainer: Jason Lowe-Power <power.jg@gmail.com> Tested-by: kokoro <noreply+kokoro@google.com>
48 lines
913 B
Python
48 lines
913 B
Python
# -----------------------------------------------------------------------------
|
|
# calclex.py
|
|
# -----------------------------------------------------------------------------
|
|
|
|
import ply.lex as lex
|
|
|
|
tokens = (
|
|
'NAME','NUMBER',
|
|
'PLUS','MINUS','TIMES','DIVIDE','EQUALS',
|
|
'LPAREN','RPAREN',
|
|
)
|
|
|
|
# Tokens
|
|
|
|
t_PLUS = r'\+'
|
|
t_MINUS = r'-'
|
|
t_TIMES = r'\*'
|
|
t_DIVIDE = r'/'
|
|
t_EQUALS = r'='
|
|
t_LPAREN = r'\('
|
|
t_RPAREN = r'\)'
|
|
t_NAME = r'[a-zA-Z_][a-zA-Z0-9_]*'
|
|
|
|
def t_NUMBER(t):
|
|
r'\d+'
|
|
try:
|
|
t.value = int(t.value)
|
|
except ValueError:
|
|
print("Integer value too large %s" % t.value)
|
|
t.value = 0
|
|
return t
|
|
|
|
t_ignore = " \t"
|
|
|
|
def t_newline(t):
|
|
r'\n+'
|
|
t.lexer.lineno += t.value.count("\n")
|
|
|
|
def t_error(t):
|
|
print("Illegal character '%s'" % t.value[0])
|
|
t.lexer.skip(1)
|
|
|
|
# Build the lexer
|
|
lexer = lex.lex(optimize=True, lextab='calclextab')
|
|
|
|
|
|
|