Is using a finite state machine a good design for general text parsing?

旧巷老猫 提交于 2019-12-08 02:29:29

问题


I am reading a file that is filled with hex numbers. I have to identify a particular pattern, say "aaad" (without quotes) from it. Every time I see the pattern, I generate some data to some other file.

This would be a very common case in designing programs - parsing and looking for a particular pattern.

I have designed it as a Finite State Machine and structured structured it in C using switch-case to change states. This was the first implementation that occured to me.

  • DESIGN: Are there some better designs possible?
  • IMPLEMENTATION: Do you see some problems with using a switch case as I mentioned?

回答1:


A hand-rolled FSM can work well for simple situations, but they tend to get unwieldy as the number of states and inputs grows.

There is probably no reason to change what you have already designed/implemented, but if you are interested in general-purpose text parsing techniques, you should probably look at things like regular expressions, Flex, Bison, and ANTLR.




回答2:


For embarrassingly simple cases couple of if's or switch'es are sufficient. For parsing a string on POSIX systems, man regex (3). For fully featured parsing of whole files (e.g. complex configs) use Lex/Flex and Yacc/Bison.

When writing in C++, look at Boost Regex for the simpler case and Boost Spirit for more complex one. Flex & Bison work with C++ too.



来源:https://stackoverflow.com/questions/2776430/is-using-a-finite-state-machine-a-good-design-for-general-text-parsing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!