parsing string messages in c++
I'm asking for a suggestion regarding the parsing of text messages. I have a list of messages in text format, in the format:
dev1.comm1 param
开发者_JAVA百科
dev1.comm2 param
dev1.comm11 param
dev2.comm1 param
dev3.comm1 param
I need to parse each received line of text and find out the message contained. I did a parser for binary messages with lookup tables, i.e.:
first byte table =
{
0x01 -> table 0x01,
0x02 -> table 0x01,
0x03 -> message 0x03
}
where 0x01
and 0x02
are the start of messages with length greater than 1, while 0x03
is a message made up of only one byte. If the first byte of the received message is 0x01
I go on comparing the second byte with the content of the table 0x01
, while if the first byte is 0x03
I return the id of the message 0x03
and get it in some other function. Obviously I implemented this with structures and pointers, this is just some form of pseudocode.
I would like to have a similar solution for text messages, but the one I outlined above seems not that good to me, also because I have long text messages, and I would end up with too many tables. Is there a cleaner solution, in the case of text messages? A parse tree perhaps?
A parse tree of course.
Perhaps you need to write a FSM for long sentences.
精彩评论