1# -----------------------------------------------------------------------------
2# lex_opt_alias.py
3#
4# Tests ability to match up functions with states, aliases, and
5# lexing tables.
6# -----------------------------------------------------------------------------
7
8import sys
9if ".." not in sys.path: sys.path.insert(0,"..")
10
11tokens = (
12    'NAME','NUMBER',
13    )
14
15states = (('instdef','inclusive'),('spam','exclusive'))
16
17literals = ['=','+','-','*','/', '(',')']
18
19# Tokens
20
21def t_instdef_spam_BITS(t):
22    r'[01-]+'
23    return t
24
25t_NAME    = r'[a-zA-Z_][a-zA-Z0-9_]*'
26
27def NUMBER(t):
28    r'\d+'
29    try:
30        t.value = int(t.value)
31    except ValueError:
32        print("Integer value too large %s" % t.value)
33        t.value = 0
34    return t
35
36t_ANY_NUMBER = NUMBER
37
38t_ignore = " \t"
39t_spam_ignore = t_ignore
40
41def t_newline(t):
42    r'\n+'
43    t.lexer.lineno += t.value.count("\n")
44
45def t_error(t):
46    print("Illegal character '%s'" % t.value[0])
47    t.lexer.skip(1)
48
49t_spam_error = t_error
50
51# Build the lexer
52import ply.lex as lex
53lex.lex(optimize=1,lextab="aliastab")
54lex.runmain(data="3+4")
55