-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.rb
executable file
·51 lines (44 loc) · 1.17 KB
/
parser.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
require './scanner'
## Grammar is
## A' -> Aeof
## A -> AA
## A -> []
## A -> [T]
def parseA(fileName)
listOfTrees = []
while( (thisToken = getToken(fileName)).typeName != "eof" ) ## HANDLES AA, ALSO EOF at END ##
if(thisToken.typeName == "LeftBracket")
listOfTrees << parseT(fileName)
else ## WE MUST START WITH A LEFTBRACKET
error = "Line " + @@lineNumber.to_s + ": " + "Found " + thisToken.value.to_s
error += " Expected: [ or eof"
@@errorList << error
end
end
return listOfTrees
end
## T -> TT
## T -> []
## T -> [T]
## T -> Atom
def parseT(fileName)
addTree = Node.new(nil)
thisToken = getToken(fileName)
if(thisToken == "eof")
@@errorList << "Line " + @@lineNumber.to_s + ": " + "Mid File EOF"
end
while( thisToken.typeName != "RightBracket" )
if(thisToken.typeName == "LeftBracket")
addTree.addNode(parseT(fileName))
else
thisNode = Node.new(thisToken)
addTree.addNode(thisNode)
end
thisToken = getToken(fileName)
if(thisToken.typeName == "eof")
@@errorList << "Line " + @@lineNumber.to_s + ": " + "Expected ]"
return addTree
end
end
return addTree
end