-
Notifications
You must be signed in to change notification settings - Fork 0
/
TinyParser.rb
156 lines (140 loc) · 3.54 KB
/
TinyParser.rb
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#
# Parser Class
#
load "TinyLexer.rb"
load "TinyToken.rb"
load "AST.rb"
class Parser < Lexer
def initialize(filename)
super(filename)
consume()
end
def consume()
@lookahead = nextToken()
while(@lookahead.type == Token::WS)
@lookahead = nextToken()
end
end
def match(dtype)
if (@lookahead.type != dtype)
puts "Expected #{dtype} found #{@lookahead.text}"
@errors_found+=1
end
consume()
end
def program()
@errors_found = 0
p = AST.new(Token.new("program","program"))
while( @lookahead.type != Token::EOF)
p.addChild(statement())
end
puts "There were #{@errors_found} parse errors found."
return p
end
def statement()
stmt = AST.new(Token.new("statement","statement"))
if (@lookahead.type == Token::PRINT)
stmt = AST.new(@lookahead)
match(Token::PRINT)
stmt.addChild(exp())
else
stmt = assign()
end
return stmt
end
def exp()
term = term()
etail = etail()
if (etail == nil)
return term
else
etail.addChild(term)
return etail
end
end
def term()
fctr = factor()
ttail = ttail()
if (ttail == nil)
return fctr
else
ttail.addChild(fctr)
return ttail
end
end
def factor()
fct = AST.new(Token.new("factor","factor"))
if (@lookahead.type == Token::LPAREN)
match(Token::LPAREN)
fct = exp()
if (@lookahead.type == Token::RPAREN)
match(Token::RPAREN)
else
match(Token::RPAREN)
end
elsif (@lookahead.type == Token::INT)
fct = AST.new(@lookahead)
match(Token::INT)
elsif (@lookahead.type == Token::ID)
fct = AST.new(@lookahead)
match(Token::ID)
else
puts "Expected ( or INT or ID found #{@lookahead.text}"
@errors_found+=1
consume()
end
return fct
end
def ttail()
if (@lookahead.type == Token::MULTOP)
multiply = AST.new(@lookahead)
match(Token::MULTOP)
multiply.addChild(factor())
multiply.addChild(ttail())
return multiply
elsif (@lookahead.type == Token::DIVOP)
divide = AST.new(@lookahead)
match(Token::DIVOP)
divide.addChild(factor())
divide.addChild(ttail())
return divide
else
return nil
end
end
def etail()
if (@lookahead.type == Token::ADDOP)
add = AST.new(@lookahead)
match(Token::ADDOP)
add.addChild(term())
add.addChild(etail())
return add
elsif (@lookahead.type == Token::SUBOP)
subtract = AST.new(@lookahead)
match(Token::SUBOP)
subtract.addChild(term())
subtract.addChild(etail())
return subtract
else
return nil
end
end
def assign()
assgn = AST.new(Token.new("assignment","assignment"))
if (@lookahead.type == Token::ID)
idtok = AST.new(@lookahead)
match(Token::ID)
if (@lookahead.type == Token::ASSGN)
assgn = AST.new(@lookahead)
assgn.addChild(idtok)
match(Token::ASSGN)
assgn.addChild(exp())
else
match(Token::ASSGN)
end
else
match(Token::ID)
end
return assgn
end
end