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来取得任何一个位置的符号,符号字符串。
要完成一个这样相对复杂的功能,是需要写一些代码,不过我保证,他最终将比你想象的少的多。
我对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来取得任何一个位置的符号,符号字符串。
相关文章推荐
- How To Build a Yacc?(13)
- How To Build a Yacc?(14)
- How To Build a Yacc?(1)
- How To Build a Yacc Series
- How To Build a Yacc?(2)
- How To Build a Yacc?(9)
- How To Build a Yacc
- How To Build a Yacc?(3)
- How To Build a Yacc?(10)
- How To Build a Yacc?(4)
- How To Build a Yacc?(5)
- How To Build a Yacc?(11)
- How To Build a Yacc?(6)
- How To Build a Yacc?(8)
- How To Build a Yacc?(12)
- How to build Android adb for ARM
- How to Build CyanogenMod for One X (codename: endeavoru)
- How_to_build_an_ARM/DSP_Hello_World_program_on_the_DaVinci_EVM
- How to Build FFmpeg for Android
- XPages Example: Building a Custom Control - How to build a Custom Multi-FileUpload Control that you can drop on any XPag