开发者

C++: parsing with simple regular expression or shoud I use sscanf?

I need to parse a string like func1(arg1, arg2); func2(arg3, arg4);. It's not a very complex parsing problem, so I would prefer to avoid resorting to flex/bison or similar utilities.

My first approch was to try to use POSIX C regcomp/regexec or Boost implementation of C++ std::regex. I wrote the following regular expression, which does not work (I'll explain why further on).

 "^"
 "[ ;\t\n]*"
 "(" // (1) identifier
    "[a-zA-Z_][a-zA-Z0-9_]*"
 ")"
 "[ \t\n]*"
 "(" // (2) non-marking
    "\["
    "(" // (3) non-marking
       "[ \t]*"
       "(" // (4..n-1开发者_JS百科) argument
          "[a-zA-Z0-9_]+"
       ")"
       "[ \t\n]*"
       ","
    ")*"
    "[ \t\n]*"
    "(" // (n) last argument
       "[a-zA-Z0-9_]+"
    ")"
    "]"
 ")?"
 "[ \t\n]*"
 ";"

Note that the group 1 captures the identifier and groups 4..n-1 are intended to capture arguments except the last, which is captured by group n.

When I apply this regex to, say func(arg1, arg2, arg3) the result I get is an array {func, arg2, arg3}. This is wrong because arg1 is not in it!

The problem is that in the standard regex libraries, submarkings only capture the last match. In other words, if you have for instance the regex "((a*|b*))*" applied on "babb", the results of the inner match will be bb and all previous captures will have been forgotten.

Another thing that annoys me here is that in case of error there is no way to know which character was not recognized as these functions provide very little information about the state of the parser when the input is rejected.

So I don't know if I'm missing something here... In this case should I use sscanf or similar instead?

Note that I prefer to use C/C++ standard libraries (and maybe boost).


If you want to use Regex, would it be simpler to split it into 2 steps. In step 1 you find

func1(stuff);

and turn it into func1 and stuff

In the next step, you parse 'stuff' to find all the separate args for the function.


How about Boost Spirit?


If this were Ruby, I'd start by matching

%r{
   ([a-zA-Z_][a-zA-Z0-9_]*)   #identifier
   \s*                        #whitespace after the identifier
   \(                         #open paren
     ([^)]*)                  #all arguments as one string
   \)                         #close paren
}x

Then I'd use $2.split(/\s*,\s*/) to split the argments apart. I don't think there's anything equivalent to split in the C++ standard library, however I think boost::regex_split can do it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜