Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interpreting custom language

I need to develop an application that will read and understand text file in which I'll find a custom language that describe a list of operations (ie cooking recipe). This language has not been defined yet, but it will probably take one of the following shape :

  • C++ like code

(This code is randomly generated, just for example purpose) :

begin
repeat(10)
{
    bar(toto, 10, 1999, xxx);
}
result = foo(xxxx, 10);
if(foo == ok)
{
    ...
}
else
{
    ...
}
end
  • XML code

(This code is randomly generated, just for example purpose) :

<recipe>
    <action name="foo" argument"bar, toto, xxx" repeat=10/>
    <action name="bar" argument"xxxxx;10" condition="foo == ok">
        <true>...</true>
        <false>...</false>
    </action>
</recipe>

No matter which language will be chosen, there will have to handle simple conditions, loops.

I never did such a thing but at first sight, it occurs to me that describing those operations into XML would be simplier yet less powerful.

After browsing StackOverFlow, I've found some chats on a tool called "ANTLR"... I started reading "The Definitive ANTLR Reference" but since I never done that kind of stuff, I find it hard to know if it's really the kind of tool I need...

In other words, what do I need to read a text file, interpret it properly and perform actions in my C# code. Those operations will interact between themselves by simple conditions like :

  • If operation1 failed, I do operation2 else operation3.
  • Repeat the operation4 10 times.

What would be the best language to do describe those text file (XML, my own) ? What are the key points during such developments ?

I hope I'm being clear :)

Thanks a lot for your help and advices !

like image 790
Andy M Avatar asked Jul 27 '26 14:07

Andy M


2 Answers

XML is great for storing relational data in a verbose way. I think it is a terrible candidate for writing logic such as a program, however.

Have you considered using an existing grammar/scripting language that you can embed, rather than writing your own? E.g:

LUA

Python

like image 132
Moo-Juice Avatar answered Jul 29 '26 04:07

Moo-Juice


In one of my projects I actually started with an XML like language as I already had an XML parser and parsed the XML structure into an expression tree in memory to be interpreted/run.

This works out very nicely to get passed the problem of figuring out tokenizing/parsing of text files and concentrate instead on your 'language' and the logic of the operations in your language. The down side is writing the text files is a little strange and very wordy. Its also very unnatural for a programmer use to C/C++ syntax.

Eventually you could easily replace your XML with a full blown scanner & lexer to parse a more 'natural C++' like text format into your expression tree.

As for writing a scanner & lexer, I found it easier to write these by hand using simple logic flow/loops for the scanner and recursive decent parser for the lexer.

That said, ANTLR is great at letting you write out rules for your language and generating your scanner & lexer for you. This allows for much more dynamic language which can easily change without having to refactor everything again when new things are added. So, it might be worth looking into as learning this as it would save you much time in rewrites as things change if you hand wrote your own.

like image 41
MerickOWA Avatar answered Jul 29 '26 04:07

MerickOWA



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!