-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample03.py
More file actions
55 lines (45 loc) · 2 KB
/
example03.py
File metadata and controls
55 lines (45 loc) · 2 KB
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
from tools.shift_reduce import LR1Parser, evaluate_reverse_parse
from tools.lexer import Lexer
from tools.pycompiler import Grammar, Terminal, NonTerminal, Token
from tools.ast import Node, BinaryNode, get_printer
class Language03:
def __init__(self) -> None:
########################################################
# GRAMATICA 03 LR(1) #
########################################################
G = Grammar()
E = G.NonTerminal('E', True)
A = G.NonTerminal('A')
equal, plus, num = G.Terminals('= + int')
E %= A + equal + A | num
A %= num + plus + A | num
########################################################
# ==================================================== #
########################################################
########################################################
# LEXER #
########################################################
lexer = Lexer(
[
(num, '(1|2|3|4|5|6|7|8|9)(0|1|2|3|4|5|6|7|8|9)*'),
('space', '( |\t|\n)( |\t|\n)*'),
(equal, '='),
(plus, '\+'),
], G.EOF
)
self.lexer = lexer
########################################################
# PARSER LR(1) #
########################################################
parser = LR1Parser(G)
self.parser = parser
####################################
# VALID #
####################################
def is_Valid(self, text, verbose=False):
all_tokens = self.lexer(text)
tokens = list(filter(lambda token: token.token_type != 'space', all_tokens))
right_parse, operations = self.parser(tokens)
return True
def Print(self, ast):
print(self.printer(ast))