class Parser

Public Class Methods

new(istream) click to toggle source
# File lib/parser.rb, line 7
def initialize(istream)
  @scan = Scanner.new(istream)
end

Public Instance Methods

Expr() click to toggle source
# File lib/parser.rb, line 28
def Expr() 
  return RestExpr(Term())
end
Factor() click to toggle source
# File lib/parser.rb, line 111
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
    end
  end
  if t.ype == :lparen then
    expr = Expr()
    t = @scan.getToken
    if t.type == :rparen then
      return expr
    else
      raise ParseError.new
    end
  end
  raise ParseError.new
  
  # puts "Factor not implemented"
  # raise ParserError.new # "Parse Error"
end
Prog() click to toggle source

private public protected

# 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) click to toggle source
# 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) click to toggle source
# File lib/parser.rb, line 69
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
  
  # puts "RestTerm not implemented"
  # raise ParseError.new # "Parse Error"
end
Storable() click to toggle source
# File lib/parser.rb, line 88
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

  
  # puts "Storable not implemented"
  # raise ParseError.new # "Parse Error"
end
Term() click to toggle source
# 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
parse() click to toggle source
# File lib/parser.rb, line 11
def parse()
  return Prog()
end