Class: Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/parser.rb

Instance Method Summary collapse

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

#ExprObject



29
30
31
# File 'lib/parser.rb', line 29

def Expr() 
  return RestExpr(Term())
end

#FactorObject

Raises:



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
# File 'lib/parser.rb', line 106

def Factor() 
  
  # puts "Factor not implemented"
  # raise ParserError.new # "Parse Error"

  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

#parseObject



11
12
13
# File 'lib/parser.rb', line 11

def parse()
  return Prog()
end

#ProgObject

—–private public protected ————



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/parser.rb', line 17

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



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/parser.rb', line 33

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()
  
  return e
end

#RestTerm(e) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/parser.rb', line 66

def RestTerm(e)
  
  # puts "RestTerm not implemented"
  # raise ParseError.new # "Parse Error"

  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
  return e
  
end

#StorableObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/parser.rb', line 85

def Storable()
 
  # puts "Storable not implemented"
  # raise ParseError.new # "Parse Error"

  fact = Factor();
  t = @scan.getToken

  if t.type == :keyword then
    if t.lex == "S" then
      return StoreNode(fact)
    else
      raise ParseError.new
    end
  end

  @scan.putBackToken
  return fact
end

#TermObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/parser.rb', line 49

def Term()
  # 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
   RestTerm(Storable())
end