Class: Parser
- Inherits:
-
Object
- Object
- Parser
- Defined in:
- lib/parser.rb
Instance Method Summary collapse
- #Expr ⇒ Object
- #Factor ⇒ Object
-
#initialize(istream) ⇒ Parser
constructor
A new instance of Parser.
- #parse ⇒ Object
-
#Prog ⇒ Object
private.
- #RestExpr(e) ⇒ Object
- #RestTerm(e) ⇒ Object
- #Storable ⇒ Object
- #Term ⇒ Object
Constructor Details
#initialize(istream) ⇒ Parser
Returns a new instance of Parser.
7 8 9 |
# File 'lib/parser.rb', line 7 def initialize(istream) @scan = Scanner.new(istream) end |
Instance Method Details
#Expr ⇒ Object
28 29 30 |
# File 'lib/parser.rb', line 28 def Expr() return RestExpr(Term()) end |
#Factor ⇒ Object
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 |
# File 'lib/parser.rb', line 95 def Factor() t = @scan.getToken if t.type == :number then return NumNode.new(t.lex.to_i) end if t.type == :keyword then if t.lex == "R" then return RecallNode.new else raise ParseError.new end end if t.type == :lparen then expr = Expr() t = @scan.getToken if t.type == :rparen then return expr else raise ParseError.new end end raise ParseError.new end |
#parse ⇒ Object
11 12 13 |
# File 'lib/parser.rb', line 11 def parse() return Prog() end |
#Prog ⇒ Object
private
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/parser.rb', line 16 def Prog() result = Expr() t = @scan.getToken() unless t.type == :eof then print "Expected EOF. Found ", t.type, ".\n" raise ParseError.new end return result end |
#RestExpr(e) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/parser.rb', line 32 def RestExpr(e) t = @scan.getToken() if t.type == :add then return RestExpr(AddNode.new(e,Term())) end if t.type == :sub then return RestExpr(SubNode.new(e,Term())) end @scan.putBackToken() e end |
#RestTerm(e) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/parser.rb', line 65 def RestTerm(e) t = @scan.getToken if t.type == :times then return RestTerm(TimesNode.new(e, Storable())) end if t.type == :divide then return RestTerm(DivideNode.new(e, Storable())) end @scan.putBackToken e end |
#Storable ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/parser.rb', line 79 def Storable() fact = Factor() t = @scan.getToken if t.type == :keyword then if t.lex == "S" then return StoreNode.new(fact) else raise ParseError.new end end @scan.putBackToken fact end |
#Term ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/parser.rb', line 48 def Term() RestTerm(Storable()) # Write your Term() code here. This code is just temporary # so you can try the calculator out before finishing it. # t = @scan.getToken() # if t.type == :number then # val = t.lex.to_i # return NumNode.new(val) # end # puts "Term not implemented\n" # raise ParseError.new end |