-2

Ive got a token class, and a lexer class. I want to have a get_token_value method in the lexer class and I can't seem to get it. The Token and Lexer classes are below with only relevant methods.

This is the code used to try to get the values of all tokens in the @tokens array of the lexer

 while lex.has_next
    puts (lex.get_token_value) //error here # yeah, I'm a java port
    lex.get_next
  end

Interpeter says value is undefined. The @tokens array is filled properly with tokens by the lexer's tokenize method(not shown). What am I not understanding here? i've tried atrrib_reader as well to the same effect. When I do

puts lex.get_next

I get the to_s of the object, but that is not the desired behavior, just a kludge.

Class Token
  def initialize( l, v)
    @label = l
    @value = v
  end

  def value
    @value
  end

  def label
    @label
  end
  def to_s
    " #{label} #{value} "
  end
end



  class Lexer

      def initialize
        @tokens = Array.new
      end
     def get_token_value
      @tokens[0].value
     end
     def get_token_label
        @tokens[0].label
      end
      # consumes the current token.
      def get_next
        return @tokens.delete_at 0
      end

      def has_next
        if @current < @tokens.size
          return true
        else
          return false
        end
      end
    end
ol bassud
  • 1
  • 1
  • The code you're provided doesn't show `get_token_value` - is this all of it? What error are you getting? – Kristján Feb 23 '16 at 06:45

1 Answers1

0

What am I not understanding here?

In order to be able to call a method named get_token_value you need to define a method named get_token_value. There is no such method in your Lexer class.

Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653
  • Sorry about that. Loop that generates error is: while lex.has_next lex.get_token_value puts lex.get_next end – ol bassud Feb 23 '16 at 21:47
  • Left out get_token_value from copy paste. Not all of code, Have 3 files 200+lines. Thanks for patience – ol bassud Feb 23 '16 at 21:55
  • The get_token_value method was left out of my problem definition. It is in the code I'm trying to run. C:/Users/..../Lexer.rb:95:in `has_next': undefined method `<' for nil:NilClass (NoMethodError) is the error message – ol bassud Feb 24 '16 at 22:01