-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
58 lines (49 loc) · 1.18 KB
/
main.py
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
"""A very basic definition of BaseTypes using RE."""
from RE.Lexer import Lexer
from RE.Parser import Parser, Branch
from RE.RegularExpression.Literal import Literal
from RE.RegularExpression.One import One
expressions = {
"boolean": Literal("true") | Literal("false"),
"AND": Literal("and"),
"OR": Literal("or"),
"NOT": Literal("not"),
"L": Literal("("),
"R": Literal(")"),
"WHITESPACE": One(Literal(" "))
}
lexer = Lexer(**expressions)
sentences = {
"expression": [
["boolean"],
["NOT", "expression"],
["L", "expression", "R"],
["expression", "AND", "expression"],
["expression", "OR", "expression"]
]
}
parser = Parser(**sentences)
string = input("> ")
tokens = list(token for _, token in lexer.lex(string) if token.name != "WHITESPACE")
print("tokens:", tokens)
tree = parser.parse(tokens)
print("tree:", tree)
tree = [
Branch(
name='expression',
children=[
Branch(
name='boolean',
children='true'
)
]
),
Branch(
name='AND',
children='and'
),
Branch(
name='boolean',
children='false'
)
]