The program resulting from this project parses Brainfuck, compiling to C#.
Much of the code is what's left from an assignment at the Compiler's course I received at college, circa 2013. At the time what everyone in my class used was Antlr-3.4, but since Antlr-4 is available as a Nuget package, it is much easier to run, so I converted it to the latter.
-
Clone the repository or download the github-generated zip to a directory, uncompress it, and open the solution file with Visual Studio.
-
In Visual Studio, to be able to resolve headers and dependencies, go to View > Other Windows > Package Manager Console. Once the Package Manager opens, clic the Restore button. If that doesn't work, enter the following:
Update-Package Antlr4.Runtime.Standard -reinstall
- If that doesn't work, then try:
Uninstall-Package Antlr4.Runtime.Standard; Install-Package Antlr4.Runtime.Standard
I think so. All of the code C# generation is written in form of actions on the grammar file (bf.g4).
The following code shows the rule listOfPlus
of the grammar (which contains some action
):
listOfPlus
@init {
int n = 0;
}
@after {
push_back($"data[cursor] += (char){n};");
}
: PLUS {n++;} (PLUS {n++;})*
;
This rule is in almost every way equivalent to the Extended Backus Naur Form (EBNF) rule with Antlr actions:
listOfPlus
: PLUS * { push_back("data[cursor]++;"); }
;
But you don't need a rule for that!
Yeap, but then you don't produce C#-pseudo-optimized code.
If you are asking, probably it is not, at least not for you.
ANTLR is a powerful tool to try compilers' course various concepts, make domain-specific languages (DSLs) or for some cool project you might be doing, and this grammar can serve as an intro to this amazing lexer and parser generator.
If you are looking for an example in Antlr 3.x, or just a more complete example, look no further than Yet Another Tiger Compiler, this one a semester-long assignment and final project, circa 2014.
Yes, all commands adjacent, repeated in source that share the same token from {+
, -
, >
, <
} are "merged" together.
Esolang is a good starting point.
Nice of you to ask. All complaints should go to /dev/null
.
Just kidding, just send those comments to me.