-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
881 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
|
||
# ------------------------------------------------------------ | ||
# calclex.py | ||
# | ||
# tokenizer for a simple expression evaluator for | ||
# numbers and +,-,*,/ | ||
# ------------------------------------------------------------ | ||
import ply.lex as lex | ||
|
||
# List of token names. This is always required | ||
tokens = ( | ||
'NUMBER', | ||
'PLUS', | ||
'MINUS', | ||
'TIMES', | ||
'DIVIDE', | ||
'LPAREN', | ||
'RPAREN', | ||
) | ||
|
||
# Regular expression rules for simple tokens | ||
t_PLUS = r'\+' | ||
t_MINUS = r'-' | ||
t_TIMES = r'\*' | ||
t_DIVIDE = r'/' | ||
t_LPAREN = r'\(' | ||
t_RPAREN = r'\)' | ||
|
||
# A regular expression rule with some action code | ||
def t_NUMBER(t): | ||
r'\d+' | ||
t.value = int(t.value) | ||
return t | ||
|
||
# Define a rule so we can track line numbers | ||
def t_newline(t): | ||
r'\n+' | ||
t.lexer.lineno += len(t.value) | ||
|
||
# A string containing ignored characters (spaces and tabs) | ||
t_ignore = ' \t' | ||
|
||
# Error handling rule | ||
def t_error(t): | ||
print "Illegal character '%s'" % t.value[0] | ||
t.lexer.skip(1) | ||
|
||
# Build the lexer | ||
lexer = lex.lex() |
Oops, something went wrong.