您的位置:首页 > 产品设计 > UI/UE

How To Build a Yacc?(7)

2005-12-19 17:51 399 查看
代码,还是代码!

要完成一个这样相对复杂的功能,是需要写一些代码,不过我保证,他最终将比你想象的少的多。

我对Lex类还有些不尽满意,实际上,我更希望lex.get_token_string能取得当前符号流中的任何一个符号,而不仅仅是当前的一个符号。。

lex = Lex.new(src)
lex.get_next_token
assert ( lex.get_token_string(0) == current_token_string && lex.get_token_string(-1) == prev_token_string )

设计一个类ExtendLex, 在初始化时将source code文件全部分解成符号流读入,保存在成员里。然后建立一个内部迭代变量。

class ExtendLex
ERROR = 9
EOF = 0

def read_file
while true
t_id = @lex.get_next_token
if ERROR == t_id
raise "lex error: '#{super.get_token_string}' is unknown character"
end
@token_ids.push(t_id)
@token_defs.push(@@token_match[t_id])
@token_strs.push(@lex.get_token_string)
break if t_id == EOF
end
end

def initialize(file)
@lex = Lex.new(file)
@token_ids = Array.new
@token_defs = Array.new
@token_strs = Array.new
@current_pos = -1
read_file
end

@@token_match = {
1 => "(",
2 => ")",
3 => "function",
4 => ";",
5 => ",",
6 => "=",
7 => "id",
8 => "constant",
9 => "error",
0 => "$"
}

def get_next_token
@current_pos = @current_pos + 1
return @token_ids[@current_pos]
end

def get_next_token2
@current_pos = @current_pos + 1
return @token_defs[@current_pos]
end

def get_token_string(index)
return @token_strs[@current_pos+index]
end

attr_reader :token_ids, :token_defs, :token_strs
end

如上面的代码:read_file调用lex的get_next_token方法分析整个文件,将所有识别的符号存储在一个数组:
token_ids里面,而将所有的符号字符串存储在一个数组: token_strs里面。
get_token_string方法带了一个参数,如果对象拥有文件中所有的符号,那么可以根据index来取得任何一个位置的符号,符号字符串。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: