0

I need an algorithm that parse an expression and translate into C# expression. For example an input string looks like:

MSG[CORRECTION] AND (NOT TIMERRUN[CYCLE] OR TIME[SHIFT]>120)

and after translate:

message.Tag =="CORRECTION" && (!timers["CYCLE"].IsRunning || timers["SHIFT"].value>120)

Currently I use Regex replace to translate, but I'd like a more elegant and more stable solution. I don't want evaluate the expressions, only translate them. The best solution would be a library. Thanks.

tlaci
  • 9
  • 1
  • A library will take up a lot of memory and take time to look up if you have a lot of different error messages. So there is a trade off. To make a library either create create a Dictionary or read messages from a file. – jdweng Jul 02 '16 at 18:15
  • 1
    This seems underspecified. What makes `MSG[CORRECTION]` become `message.Tag == "CORRECTION"` but `TIMERRUN[CYCLE]` become `timers["CYCLE"].IsRunning`? `TIME[SHIFT]` becomes something different, too. Unless you're already familiar with a parsing library I doubt using one will help. I'd suggest sticking with regex or learning how to write a parser. – 31eee384 Jul 02 '16 at 18:34
  • There are tons of parser frameworks to choose and you just need to google with correct keywords. – Lex Li Jul 02 '16 at 23:39
  • See http://stackoverflow.com/questions/2245962/is-there-an-alternative-for-flex-bison-that-is-usable-on-8-bit-embedded-systems/2336769#2336769 for how to write recursive descent parsers for expressions (this is actually relatively easy). If you follow that thread, it leads to other SO answers that describe how to build and process ASTs; you can probably fit you conversion into that framework. – Ira Baxter Jul 03 '16 at 00:11
  • If OP doesn't want to hand-code a parser, see this SR answer that discusses a methodical approach to OP's problem: http://softwarerecs.stackexchange.com/a/31379/101 – Ira Baxter Jul 03 '16 at 01:51

0 Answers0