1. Prologue
A few years ago I developed an internal language for calculating reports, which is used in our company. The language turned out to be strange, it has neither cycles nor standard conditional statements (if), dynamic typing, but it coped with its functions. The idea of the language was to implement a simple expression evaluator. Over time, the requirements became more and more, as a result, the language began to expand, it was necessary to develop a simple script for adding new functions.
At the moment, the internal language for calculating reports has become a monster, not a twisting mutant. This creation gave birth to the lack of documentation, ease of expansion and laziness of developers (it is easier to write something new than to figure out what is and find the right one). The language began to be able to do a lot, too much of what it was not intended for (except that the pancakes did not teach him to bake the oven, which is a pity).
2. Rethinking
The department began to expand, newcomers appeared who need to be taught this language, and this is not easy. It was decided to transfer the reports to a new system, devoid of the shortcomings of the current one; the task is complicated by the emerging department of analytics. The interaction is about analytics -> calculations -> development -> script -> analytics. The task is complicated by the fact that the code of the internal language must be read by analysts (not programmers) and send for revision if errors are detected. Sounds awesome - do it! This was the verdict, the task got to me, for the atonement of previous sins. When the task was voiced and the voices of critics were posed somewhere “Where are the cycles? The developer apparently did not know about them ”,“ Isn’t it easier to write all this in c #? C = a + b ”. Sharpe language constructs will be very relevant in the analytics department (where words like functional, ternary operator sound like a checkmate).
3. Search for a solution
The current version of the language was developed on the knees of neither knowledge nor experience at that time was not needed, it was necessary to develop something quickly that would satisfy current needs. At the moment, there is an experience that says “No, sit down to do this nonsense! No knowledge! It is necessary to use someone else’s experience and knowledge. ”. The first thing that comes to mind is the development of a compiler (quite an idea).
Found it . After viewing, I realized that it was very interesting, very difficult, and the campaign too I swung at the compiler. Sometimes in the project we used configuration files of various services (usually xml), proudly calling it DSL! I decided to get acquainted with the abbreviation in more detail - the campaign
is what is needed . I am already familiar with this uncle, he has not a weak weight in the field of design - it is necessary to acquaint him with his works. In
this book, he constantly mentions ANTLR.
')
4. Installing ANTLR4 in VS
I did not find adequate articles on the use of ANTLR in VS, it made me write this article (in fact, everything is simple, when you know, when you know, everything is simple).
Here, in principle, an example is described (with manual editing of the project file). Not okay. Everything you need can be found in the studio extensions and nuget.
ANTLR Language Support:

What it gives:

Further two libraries with nuget:

Then create a grammar, add the file Calculator.g4 to the antlr4 Combined Grammar project. With something like this:
grammar Calculator; @parser::members { protected const int EOF = Eof; } @lexer::members { protected const int EOF = Eof; protected const int HIDDEN = Hidden; } prog: expr+ ; expr : left = expr op=('*'|'/') right = expr # MulDiv | left = expr op=('+'|'-') right = expr # AddSub | INT # int | '(' expr ')' # parens ; /* * Lexer Rules */ INT : [0-9]+; MUL : '*'; DIV : '/'; ADD : '+'; SUB : '-'; EQU : '='; WS : (' ' | '\r' | '\n') -> channel(HIDDEN) ;
Taken almost unchanged from the site mentioned above. In the file properties you need to set:

Next F5 ... And nothing, no errors, no promised CalculatorBaseVisitor.cs. Oh yes, how could I forget, I don’t have an environment for running programs on JAVA (epic, that for developing in C # you need JAVA).
Repeated attempt to launch the project issued:
Error 5 '@' came as a complete surprise to me
Error 2 '╗' came as a complete surprise to me
Error 3 missing SEMI at '┐grammar'
Etc. There is no despair in our hearts or Google to help, in general, the case in the encoding: utf8 (BOM) must be utf8 (without BOM).
MS Word in shock when opening a file:

I converted to windows - 1251 and the curved characters are immediately visible at the beginning of the file (funny, but there was no such problem on another machine).
The project has gathered, but (there is always something lately, but) the CalculatorBaseVisitor.cs has not found the promised one in the project. Yandex revealed all the maps (and no matter what search engine) ... \ obj \ Debug - the generated analyzer files live here, it is possible that these are correctly generated files, cannot be modified, wpf drops the .g files into the same folder, but resets the visitor there I was inconvenienced when Parser and Lexer were added to the project, but the visitor was not.
Then you can use the link from where I took an example of grammar to complete the lesson.
FurtherPS
Perhaps all the problems are due to the fact that the ANTLR native language is JAVA, and the generator is not so popular in C #, but after overcoming all the connection problems (and this is not 1 hour), the system turned out to be quite good, although the examples for C # are very few.
Pps
Based on real events. Some names and events have been distorted in order to respect copyright and safety.