1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# note - use python3
# note - example from https://github.com/dabeaz/sly
# note - type ctrl-d as the end of input.
# notes on lexer
# - tokens are compiled with re.verbose flag
# (comments with #, gets rid of white space).
# If you need to match # use \#
# - tokens matched in order in your file.
# If you want to use == and =, put the rule for == first.
# - sometimes there is more than one way to do something in the
# lexer. for example, can have a separate rule for each keyword
# or catch them under a generic "ID" or "NAME" rule and use "token remapping"
# -----------------------------------------------------------------------------
# calc.py
# -----------------------------------------------------------------------------
# package isn't installed at the moment, so add the path so python can find it
import sys
slyPath = "/u1/h0/jkinne/public_html/cs420-s2019/code/sly-0.4"
sys.path.append(slyPath)
from sly import Lexer, Parser
class CalcLexer(Lexer):
tokens = { NAME, NUMBER, PLUS, TIMES, MINUS, DIVIDE, ASSIGN, LPAREN, RPAREN,
IF, THEN, WHILE, DO}
ignore = ' \t'
# Tokens
NAME = r'[a-zA-Z_][a-zA-Z0-9_]*'
NUMBER = r'\d+'
NAME['if'] = IF
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX