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



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

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

#FactorObject

Raises:



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

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 == :lparen 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



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

def Prog()
  result = Expr()
  t = @scan.getToken()
  
  if 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()
  
  return e
end

#RestTerm(e) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/parser.rb', line 52

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()

  return e 
  
end

#StorableObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/parser.rb', line 70

def Storable()
  factor = Factor()

  t = @scan.getToken()

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

  @scan.putBackToken()
  return factor
  
end

#TermObject



48
49
50
# File 'lib/parser.rb', line 48

def Term()
  return RestTerm(Storable())
end