| 07-10-2008, 09:32 AM | #1 |
So, I decided to play around with the idea of Grimoire extensions. I'm not much of a delphi guy, though, so first I needed to put together an interpreter for Jass. I decided to use ANTLR to these ends, and the results are very good. Attached are: -A .g file which can be opened in ANTLR. The grammar was developed in ANTLRWorks 1.2b5. Any post-3.1 version of ANTLR should be able to run it, but if you're a programming languages noob like me, I'd recommend using ANTLRWorks. Using ANTLR, you can use this .g file to export source files for a Jass interpreter to about half a dozen different languages. I used C#, but you can use C, Java, Objective C, ActionScript, etc. I know that C# works for a fact, though, and I cannot vouch for the usefulness of the exporters for the other languages. -A C# project showing off the Jass interpreter exported with ANTLR. It's a console project that takes a Jass file in as a single argument. It will attempt to parse the Jass file, displaying parse errors. There is a known bug, located in the C# ANTLR runtimes that causes it to get confused by UNIX/Mac newlines, so the line of a syntax error will be displayed incorrectly in some cases. This doesn't stop it from parsing correctly, though, because my grammar is done right. Included in the project are Program.cs, written by me, and JassLexer.cs/JassParser.cs, exported by ANTLR. Also the necessary dll's of the ANTLR runtimes are included. The only changes made to JassLexer.cs/JassParser.cs after exporting is that I changed the text of the token name strings at the top of JassParser.cs, to make syntax error messages more informative. -The binaries exported by compiling the C# project, so you can test it without pulling out visual studio. Let me know if you enjoy this stuff- I'll be using it to write my obfuscator! Update: I decided to go ahead and clean up/comment the .G file, so it's a little more legible now, hopefully. |
| 07-10-2008, 10:50 AM | #2 |
Your attachment is invalid. Thought you wanna know. |
| 07-10-2008, 10:54 AM | #3 | |
Quote:
You mighta caught it while I was replacing it with the redone version. It's working for me now. |
| 07-10-2008, 11:20 AM | #4 |
Yub. It works now. |
| 07-10-2008, 12:29 PM | #5 | |
Quote:
-- I hope your obfuscator does more than stripping whitespace cause you wouldn't have needed a grammar then. |
| 07-10-2008, 12:49 PM | #6 | |
Quote:
Naw, I'm gonna go in and replace ID's. Dunno how much use it'll see, but I'm having fun either way. |
| 07-10-2008, 01:29 PM | #7 | |
Quote:
I uploaded a new version that corrected a bug in my grammar where it wouldn't consider a // directly followed by a newline to be a comment. |
| 07-10-2008, 04:10 PM | #8 |
you don't need grammar to replace ID's (with integers) either. just look for all '...' strings and replace them. anyway, can you explain what this is for to someone who doesnt know what ANTLR is? |
| 07-10-2008, 05:46 PM | #9 | |
Quote:
JASS:function grace takes nothing returns nothing call BJDebugMsg("Bob is 'fine'") endfunction Nevertheless it is still simple to ignore strings. I think PJASS has a grammar in its source? |
| 07-10-2008, 06:43 PM | #10 | |
Quote:
Sorry, I should have been clearer on what I meant by IDs. I mean variable & (non-native) function names. ANTLR is a tool written in Java that lets you build programming language grammars in its own proprietary language (it's like a weird bastard child of EBNF, a notation for representing programming languages on paper) and export the code for a parser for that language in any number of languages. The package is a grammar written for antlr, a C# project with the output, and the C# project compiled. I'm not sure how overly useful it would be to someone else, since I'm not sure a lot of people are building stuff for Grimoire that don't already have a parser source that they work with, but it lets you build Jass parsers in languages other than Delphi with a few clicks, so I figured that it was potentially useful enough to other people to warrant posting. As for a grammar with pjass- I checked out JassHelper, I didn't think to check pjass, I'll have to. |
| 07-11-2008, 06:51 AM | #11 |
Of course my tool does ignore strings. But it doesnt convert character codes to integers, instead it does it the other way around (after checking that the result will look like an actual code). If you mean variable/function names, I do that too without parsing the grammar. You don't need to parse all those if's/loop's to do that, just search for function and variable declarations and add then into a dictionary, then when you hit the name thats already in the dictionary, replace it (there are a few exceptions but thats not hard to implement). I also inline one-line functions that take nothing and return a value (because if you convert GUI to JASS you will get a LOT of these). |
| 07-11-2008, 08:53 AM | #12 |
Well, I don't get why you are critizizing the fact that he does it the clean and easy way by using grammar. This is also much more efficient than pattern matching. Since jass needs to declare everything before its usage you can do all the work in one pass. (well except for ExecuteFunc and those variable events of course) On topic: Yeah, the pjass source includes an LL(1) jass grammar. |
