DLR Language with Generated parser (Part 3)
Background
I wanted to produce a working DLR language. For this purpose I wanted to use C# Compiler tools to generate lexical analyzer and parser. I spent couple of hours tackling DLR with VS and debugger and finaly made it. There are several problems which you have to solve. First, implement a set of classes to host a your language (See Part 1), manage variables (See Part 2) and finaly produce a AST generator.
When almost everything was ready I have suprisingly strange problem with running loops and conditions. The following AST haven’t run as expected:
In fact, it does not produce any results… And again debugger had helped me. Everything was because of the following implementation of LanguageContext from DLR:
The limitation of the following solution is that it only works with Integer variables.
Solution
I have used C# Compiler Tools by Malcolm Crowe to produce Parser and Lexical Analyzer. The syntax of the grammar is similar to that of Yacc:
MyStatement: …
| ‘if’ Condition:c MyStatement:s {$$ = Ast.IfThen((Expression)c.yylval, (Statement)s.yylval);}
…
The complete grammar of the language and build tools may be found in the “Grammar and Lexer” folder of the solution.
The example of the MyL code: