How To Build a Yacc?(11)

分析完rule_file, 最后一个关键的步骤是生成DFA。

这是一个比较复杂的过程,首先我们要建立一个Item结构,这样才能构造状态(states)

item 应该是一个rule和一个相关的position(当前识别位置)组成。

class TestCompiler < Test::Unit::TestCase 
  def test_item
    rule = Rule.parse("function_decl := /
      function function_name ( argument_list ) ; : decl")
    assert(rule)
    item = Item.new(rule, 0)
    assert(item.current_token == "function_decl")
    assert(item.next_token == "function")

    item = item.step
    assert(item.current_token == "function")
    assert(item.next_token == "function_name")
    assert(item.is_end? == false)
   
    item.step!(5)   
    assert(item.is_end? == true)
  end
end




##################################
# 一个Item即NFA中一个状态集合中的成员
##################################
class Item
  def initialize(rule, pos)
    @rule, @pos = rule, pos
  end
 
  def current_token
    return token(@pos)
  end
 
  def next_token
    return token(@pos + 1)
  end
 
  def step(distance = 1)
    return Item.new(@rule, @pos + distance)
  end
 
  def step!(distance = 1)
    @pos = @pos + distance
  end 
 
  def is_end?
    return @pos >= @rule.rt.length
  end
 
  def token(pos)
    return nil if pos < 0 || pos > @rule.rt.length
    return @rule.lt if 0 == pos
    return @rule.rt.at(pos-1)
  end
 
  def to_s
    rta = rule.rt.dup
    #shift_pos = @pos-1 < 0 ? 0 : @pos - 1
    rta.insert(@pos, "#")
    "[#{rule.lt} = #{rta.join(" ")}]"
  end
 
  def eql?(other)
    #p "#{self.to_s} eql? #{other.to_s}, #{@rule.eql?(other.rule) && @pos.eql?(other.pos)}"
    return @rule.eql?(other.rule) && @pos.eql?(other.pos)
  end
 
  alias :== eql?
  attr_reader :rule, :pos
end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值