Can Coco/R turn a parsed file into bytecode? - delphi

I want to write a simple compiler for educational purposes in Delphi. I have read about Coco/R and found this implementation for Delphi: http://code.google.com/p/dcocor/ . From what I have read, this is a parser for the Delphi 2009 syntax.
What would I have to do to turn the parsed file into a bytecode? Can Coco/R do this?
I know about scripting languages like FastScript or DWS, but I'd like to try and write my own for my own purposes.
Please give me some advice or clarify things a little.

What would I have to do to turn the parsed file into a bytecode?
First, decide what kind of bytecode would you like to have: JVM? LLVM? MSIL? Inventing your own?
Can Coco/R do this?
Coco/R is a parser generator framework, it only generates lexer-parser for a language you define and feed to the generator. Steps after that is your responsibility. Though there might be projects out there that can help generating ast/target code (but I haven't found any for Coco/R ).

Related

Autocomplete punctuation using grammar

I'm working on a tool that is able to autocomplete the necessary literals defined in a grammar. For example: in C# if a programmer enters: for with a space after it, then it's entirely possible to parse the code, determine that the programmer has started a for statement and autocomplete the necessary punctuation: ( ; ; ).
The more I think about the problem, the more I think there must already be a solution for it, because it's such a common use case, but I can't find anything.
Is there a tool that can do this using a given grammar?
If you don't mind using ANTLR v3 instead of v4 you can use Xtext in order to generate an editor that features auto-complete and error-highlighting. This will happen in form of a plugin for eclipse and apparently also for IntelliJ IDEA.
If you want to use a different IDE or simply want to make use of ANTLR v4's powerfull features you could still have a look at the Xtext Sources as they have to do what you are searching for in order to provide the above mentioned features properly...
This package looks very promising at first glance... You might find the respective code in there.
Be aware though that Xtext is mainly written with Xtend so you either have to do so as well or you have to rewrite it a bit

Lua Bytecode to Lua human "readable"

I just got an script that I want to make some changes and I'm looking for someone to develop me a freelance job to make the loadstring that I will give readable for editing.
The Lua code is like this:
------------------------- ENGINE -----------------------------
code='\27\76\117\97\81\0\1\4\4\4\8\0\56\0\0\0\64\67\58\92\85\115\101\114\115\92\74\101\
I want it to be turned into a human code. I already searched about the subject and found that there are some tools like Chuckspy, Luadec51 and Unluac that can do this job. Anyway, I never programmed before in lua and got no compiler knowledge to perform that.
I'm looking for someone to help me, I have no idea how I'll do it.
Thanks anyway
The link of two archive:
http://www.4shared.com/file/uQguRL4D/Avani_Dice_Script_1.html
http://www.4shared.com/file/FSLbD9tA/Avani_Dice_Script_2.html
luac -l will print out the Lua bytecode in human-readable form. With a basic understanding of Lua's instruction format, this is fairly easy to manually turn into source code.
As with other languages, automatic decompilers will rarely produce source code which is useful for understanding or editing the code.

Supply method bodies in Tools API

Years ago, I wrote a code template that took a few simple parameters (points) and produced a class skeleton. Since some of the method bodies had code in them, I couldn't call InvokeClassCompletion and just placed the implementations with the declarations (user had to move them).
It has occurred to me that I can write a code template script engine to insert these methods in the correct position, but I don't see how I'm supposed to navigate the file. There are declarations for IOTAEditReader and IOTAEditWriter in ToolsAPI.pas, but I'm guessing there is a step missing - certainly I don't need to manually parse the Delphi code just to find the implementation section?
Does anyone have experience with it?
The IDE has plenty of parsers in it, but not one is made available for IDE plugins (ToolsAPI). So you have to write your own parser or use an existing one like http://delphiblog.twodesk.com/using-the-castalia-delphi-parser
You might find that GExperts or CNPack contains almost all the code you need, in one of its editor wizards.
If not, then, those two are the best reference I know for writing IDE plugins. As far as writing parsers, see Andreas' answer.
If you just want better code templates, you could consider buying Castalia, as it has an improved code template feature over the builtin delphi IDE plugin features. Castalia internally uses the Castalia delphi parser mentioned by andreas. It's quite good.

llvm-clang: incremental or online parser?

Is there anyway to use the llvm-clang parser in an incremental/online manner?
Say I'm writing an editor and I want to be able to parse the C++ code I have in front of me.
I don't want to write my own hacked up parser.
I'd like to use something full featured, like llvm-clang.
Is there an easy way to hijack the llvm-clang parser? (And is it fast enough to run it continuously in the background)?
Thanks!
I don't think clang can incrementally parse C++ files, but it's one of this project goals: http://clang.llvm.org/features.html
I've written something similar for my final year project. It wasn't C++ editor, but a Visual Studio plugin, which main task was improving C++ intellisense (like Visual Assist X).
When I was writing this project I've been also thinking about C++ incremental parser, but I haven't found any suitable solution. To solve the C++ intellisense problem I used normal C++ parser from GCC. However it was to slow, to parse file after each code completion request (ctrl+space), just try including boost::spirit. To make this project work properly I parsed files in the background and after each code completion request I compared current file with it's previous version (via diff) to detect changes made from last parsing. Having those changes I updated syntax tree, mostly by adding or removing variables.
Except incremental parsing, there is also another problem with projects like this. Mostly you'll be parsing C++ code which is being edited so it's invalid code. Given the complex C++ grammar, sometimes parser won't be able to recover from syntax errors, so it won't detect correctly some symbols in code.
Another issue are C++ parsers / compilers differences. Let's say I'm using working in Visual Studio and I have used some VC++ compiler specific contruction in my code. Clang parser won't be able to parse it correctly.
For writing something similair to IntelliSense, I would advise you to write your own parser using the LALR parsing algorithm. Since you can save its state in each line so you don't have to reparse the whole file when a file has been editted, which is very fast!
Note that C++ can't be fully expressed in BNF, but I think you could get pretty far with some adjustments. It's ofcourse a lot more work than using Clang's frontend, but you could still use Clang for analysing header files in coöperation with you own written parser.

Parser generator for Delphi?

Can anyone recommend a parser generator that will produce win32 Delphi code? What I'm trying to do is create a simple Domain-Specific Language.
How complex is your DSL?
I created a parser (in Delphi) for the new Delphi RIDL language to support some in-house COM generation tools we use.
My approach was to use ANTLR to play around with the syntax rules until I had something that parsed the various test files I had. I then hand-coded a recursive descent parser (based on the Java generated by ANTLR). I was also using the Castalia Delphi Parser in the project, so I based my lexical analyser on that.
Recursive descent parsers are actually really simple (but tedious :-) ) to write manually.
The Delphi versions of Coco/R are quite ok:
http://www.ssw.uni-linz.ac.at/Research/Projects/Coco/
most notably Pat Terry's one:
http://www.scifac.ru.ac.za/coco/
Try this:
http://www.grendelproject.nl/dyacclex/
I have not tried this at all and I just noticed the information while I was reading through the documentation, but FastScript, in their documentation which you can get here, says that you can use their parser to create your own language which you specify in an XML file. This might give you something you can use.
I found Antlr For Delphi 3.1.1.
SharpPlus Antlr For Delphi Target
3.1.1, a language tool that provides a framework for constructing
recognizers, interpreters, compilers
with Delphi!
It is a commercial product. I have no idea what it is like.

Resources