get types of function

时间:2016-10-20 19:03:46

标签: c# string parsing types

I'm trying to parse this expression in C# like so:

fun(p1, p2, 33,"lina", g(x,5), ...)

How can I get the "type" of everything in the expression, given these examples:

fun    ---> function
p1, p2 ---> variables
33     ---> constant
"lina" ---> constant 
g      ---> function 
x      ---> variable
5      ---> constant 

1 个答案:

答案 0 :(得分:2)

In general case you can't parse such string with regular expression: regular expression can operate with regular grammar only (with some minor extensions), when you need context free one. You want a parser (e.g. Antlr or Irony). See

Regular vs Context Free Grammars

for details. To show the difficulties you can face, let's just play with strings and comments:

 fun (1 + p1 + 1);                  // p1 is an argument
 fun (/*1 + p1 + 1*/);              // p1 is NOT an argument
 fun ("/*1" + p1 + "1*/");          // p1 is an argument 
 fun (/*"/*1" + p1 + "1*+/"*/);     // p1 is NOT an argument 
 fun ("/*""/*1" + p1 + "1*+/""*/"); // p1 is an argument  
 ...