1

I am just curious. Like in interpreted languages or even statement calculators how do people convert the strings given by input or files to actual expressions? e.g "Enter Calculation: " and you write "2*7/4" which is a string. How does the program convert the string into an actual expression? It is easy to convert a string to an int but how do you convert operators like + , - , /, etc.? I understand that these kind of things are usually implemented in C/C++ but is it possible to make such a thing in a high-level language like C#? And If yes then how?

ApprenticeHacker
  • 20,229
  • 26
  • 99
  • 152

3 Answers3

3

Here's an article you might check out. There are also tools like Flee. Or yet another technique which allows you to evaluate C# expressions using a CodeDom provider.

Darin Dimitrov
  • 994,864
  • 265
  • 3,241
  • 2,902
1

I believe this can be achieved using expression trees, which is how LINQ is implemented.

http://msdn.microsoft.com/en-us/library/bb397951.aspx

Daniel Powell
  • 7,957
  • 10
  • 59
  • 107
1

Google "parse tree". One can be written in any Turing-complete language.

jcomeau_ictx
  • 36,402
  • 6
  • 91
  • 101