Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
171 views
in Technique[技术] by (71.8m points)

parsing - Tokenizing math expression with functions in C#

I figured this would be easy to find, but I haven't been successful.

I need to be able to tokenize the following expression

(4 + 5) + myfunc('two words', 3, 5)

into

(
4
+
5
+
myfunc
(
'two words'
,
3
,
5
)

It seems like this is probably a common need, however I haven't been able to find any good documentation on this out there. Is this something I could do using regex? Anybody know of an existing way to do this?

I'm using C#, but if you have the answer in another language, don't be shy.

Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you are looking into a robust and powerful solution, you should definitively look into a lexical analyzer (like Antlr). However if what you need is just tokenization of simple expressions like the one you provided, you can achieve this result pretty easily:

// TODO Refactor and optimize this function
        public IList<string> TokenizeExpression(string expr)
        {
            // TODO Add all your delimiters here
            var delimiters = new[] { '(', '+', ')', ',' };
            var buffer = string.Empty;
            var ret = new List<string>();
            expr = expr.Replace(" ", "");
            foreach (var c in expr)
            {
                if (delimiters.Contains(c))
                {
                    if (buffer.Length > 0) ret.Add(buffer);
                    ret.Add(c.ToString(CultureInfo.InvariantCulture));
                    buffer = string.Empty;
                }
                else
                {
                    buffer += c;
                }
            }
            return ret;
        }

Example:

TokenizeExpression("(4 + 5) + myfunc('two words', 3, 5)") Count = 14

[0]: "("
[1]: "4"
[2]: "+"
[3]: "5"
[4]: ")"
[5]: "+"
[6]: "myfunc"
[7]: "("
[8]: "'twowords'"
[9]: ","
[10]: "3"
[11]: ","
[12]: "5"
[13]: ")"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...