xml – 如何编写简单的Ragel标记器(无回溯)?
更新2
原始问题:我可以避免使用Ragel的| ** |如果我不需要回溯? 更新的答案:是的,如果您不需要回溯,可以使用()*编写一个简单的标记器. 更新1 我意识到询问XML标记化是一个红色的鲱鱼,因为我所做的并不是特定于XML. 结束更新 我有一个Ragel扫描仪/标记器,只需在文件中查找FooBarEntity元素,如: <ABC > <XYZ > <FooBarEntity> <Example >Hello world</Example > </FooBarEntity> </XYZ > <XYZ > <FooBarEntity> <Example >sdrastvui</Example > </FooBarEntity> </XYZ > </ABC > 扫描仪版本: %%{ machine simple_scanner; action Emit { emit data[(ts+14)..(te-15)].pack('c*') } foo = '<FooBarEntity>' any+ :>> '</FooBarEntity>'; main := |* foo => Emit; any; *|; }%% 非扫描仪版本(即使用()*而不是| ** |) %%{ machine simple_tokenizer; action MyTs { my_ts = p } action MyTe { my_te = p } action Emit { emit data[my_ts...my_te].pack('c*') my_ts = nil my_te = nil } foo = '<FooBarEntity>' any+ >MyTs :>> '</FooBarEntity>' >MyTe %Emit; main := ( foo | any+ )*; }%% 我想出了这个并在https://github.com/seamusabshere/ruby_ragel_examples为它编写了测试 您可以在https://github.com/seamusabshere/ruby_ragel_examples/blob/master/lib/simple_scanner.rl和https://github.com/seamusabshere/ruby_ragel_examples/blob/master/lib/simple_tokenizer.rl看到读/缓冲代码 解决方法
您不必使用扫描程序来解析XML.我已经实现了
simple XML parser in Ragel,没有扫描仪.
Here是一篇博客文章,其中包含一些时间和更多信息.
编辑:你可以做很多事.你可以使用扫描仪.您可以解析单词,如果您看到STARTANIMAL,则会开始收集单词,直到您看到STOPANIMAL. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |