class Parser

Public Class Methods

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

Public Instance Methods

parse() click to toggle source
# File lib/parser.rb, line 12
def parse()
  Prog() #No requiere return.
end

Private Instance Methods

Expr() click to toggle source
# File lib/parser.rb, line 29
def Expr() 
  return RestExpr(Term())
end
Factor() click to toggle source
# File lib/parser.rb, line 100
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
    end

    puts "Parse Error"
    raise ParseError.new
  end

  if t.type == :lparen then
    result = Expr()
    t = @scan.getToken
    if t.type == :rparen then
      return result
    end

    puts "Error"
    raise ParseError.new
  end

  puts "Error"
  raise ParseError.new
end
Prog() click to toggle source
# File lib/parser.rb, line 17
def Prog()
  result = Expr()
  t = @scan.getToken #Si una función no requiere parámetros no es necesario ().
  
  if t.type != :eof then
    print "Expected EOF. Found ", t.type, ".\n"
    raise ParseError.new
  end
  
  result #Está retornando el resultado.
end
RestExpr(e) click to toggle source
# 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
  
  e
end
RestTerm(e) click to toggle source
# File lib/parser.rb, line 66
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() click to toggle source
# File lib/parser.rb, line 82
def Storable
  result = Factor()
  t = @scan.getToken

  if t.type == :keyword then
    if t.lex == "S" then
      return StoreNode.new(result)
    end

    print "Expected S found #{t.lex} "
    puts " at line: #{t.line} col: #{t.col}"
    raise ParseError.new
  end

  @scan.putBackToken
  result
end
Term() click to toggle source
# 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