I need to write a translator for vbscript to c#. What would be the basic steps invloved to translate using ANTLR? I am not very clear about whether to use grammar (lexer/parser? file or stringtemplate or AST or all.
Suggestions?
Thanks in advance.
Is this really possible?
I'm "translating" (read: rewriting) a MS Access/VBA application since two years to C# and found out that even the online available converters (like this one which is more VB.NET, but anyway) fails at most basic conversions.
So my assumption until now is that there are way too much kind of constructs that are simply not translatable from VBScript to C#.
See What kinds of patterns could I enforce on the code to make it easier to translate to another programming language?
The easiest way to translate VBScript to C# would be using VB.NET as an intermediate step. VBScript is very similar to VB.NET (there are some differences though), so all you need to do is copy-paste your code in a new solution in Visual Studio. After you make a few changes, the code will be able to compile.
You can leave it at that (you can call the resulting DLL from your C# code), but even if you don't want to do that, converting VB.NET to C# is trivial. You can even decompile your resulting DLL/EXE (although this will lead to some loss of information) or use any online VB.NET to C# converter.
Related
I may be starting a new job which requires VB.NET but I am a C# developer and even though I may be able to understand the code, writing it from scratch seems to be a hassle for me for a while.
There are C#>VB.NET converter out there (online) and where you paste your C# code and it converts it into VB.NET code. My question is whether there is any person who experienced this and whether it is a good temp solution or I am gonna experience so much difficulties with that? Do they convert good?
And probably I am gonna run the codes on Asp.net.
An example converter: http://www.developerfusion.com/tools/convert/csharp-to-vb/
Thanks in advance.
Don't use converters - learn VB.NET and the differences between its syntax and C#.
There is a very good comparison cheat sheet here to get you started.
In practice, you will find that most of the time you are interacting with familiar .NET objects in the same way and you only have some syntax differences (though generics and delegate syntax are such a pain that one tends to shy away from them).
Microsoft has stated that they are trying to bring both language to feature parity, so anything you can do in C#, you should be able to do with VB.NET (with minor differences normally).
Update - don't forget that compiled code (in assemblies) should work identically in both languages (assuming CLS compliance), so you could write a library in C# for use with VB.NET and vice versa.
You can try the Telerik Code Converter
That being said, it would be a valuable exercise to convert the code manually. You'll gain a good amount of experience by doing a manual conversion and you'll learn some of the key differences between the 2 languages that may help you going forward.
I'd say your code will work, but you'll miss some special features for which there is no C# equivalent and which would make your code fit better with the language. Some examples:
In C#, you assign event handlers with +=, which will be translated to AddHandler. In VB, however, it's much more common to use WithEvents instance variables. This is especially relevant for ASP.NET, where C# often uses the AutoEventWireup feature, which done in VB through WithEvents instead.
In C#, you access XML through LINQ to XML method calls, which will be translated to the matching method calls in VB. However, in VB, it's more natural to use the integrated language support for XML.
This is good for converting the bulk of your code, but it's not a total solution. One thing you will have issues with is the converter knowing what to do with C#'s indexer brackets ([]) vs. method parentheses (()). VB uses parenthesis for indexers and methods and there's no way for it to know which to use.
I've gone through conversion hell with these things and finally decided that they were just too much trouble and that it was much easier to just convert it by hand. I come from a VB background, so this wasn't a huge deal for me.
For what you want to do, though, you need to learn VB.NET syntax. Writing everything in C# and converting it to VB.NET is not a good, long-term solution. You will eventually have to learn VB.NET. Your manager(s) will not be keen to the fact that you're not learning the core fundamentals of the language you were hire to program in.
Don't do it... There are converters, but you will find that once you learn the key differences that you will be fine. You will interact with the .NET libraries in the same way so much of the programming will be the same.
I just changed jobs recently and went the opposite direction. I'm glad I took the time to learn C#. Major dividends in the end and you'll be more versatile.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Convert VB.NET code to C#
Hey,
I'm looking for a powerfull tool who can convert & C# code to VB.NET or vice-versa.
I've tried some websites but they are not very good.
Any ideas ?
Thanks
I have used .Net Reflector. Just load the DLL and select the language C#, VB etc.
See http://reflector.red-gate.com/download.aspx
The two most popular ones are developerFusions and teleriks:
http://www.developerfusion.com/tools/convert/vb-to-csharp/
http://converter.telerik.com/
Sometimes you will have to hand convert certain parts of the converted code that the converters have trouble understanding but on the whole they do a large part of the heavy lifting for you.
One example is that vb.net uses array indexes as () whereas c# uses []. For some reason I have seen the developerFusion get confused and leave these as () in the c# code which confuses the compiler.
I mostly use this for quick translations of code when I am answering forum questions and the op has specifically requested a vb.net answer. I just find it easier to code it in c# and then convert it.
If you have a very large project then you might find it tedious to convert each of the individual pages. In this case you might (I haven't actually tested this) find a way to convert the code more quickly by using the #develop IDE. I only say this as the developerFusion online converter is actually powered by code from this tool.
http://www.icsharpcode.net/OpenSource/SD/
Another thing to remember, as has been mentioned elsewhere in this thread, is that its entirely possible to mix and match languages within a single project. The only restriction to my knowledge is that you can only have one language per folder. This is to do with the way that the .net code is compiled. By default each folder is compiled into its own assembly.
If you really need to mix and match on the same page I think you could make a usercontrol to contain the code for one language and put it onto a different languages page.
You've both online- and offline solutions at your disposal.
A very popular online converter: Developer Fusion's Converter
Reflector has for long been the most popular offline source inspector. It allows you to view any assembly in the language of your choice. However, recently they changed and it is now no longer free.
I'm making a Genetic Program, but I'm hitting a limitation with C# where I want to present new functions to the algorithm but I can't do it without recompiling the program. In essence I want the user of the program to provide the allowed functions and the GP will automatically use them. It would be great if the user is required to know as little about programming as possible.
I want to plug in the new functions without compiling them into the program. In Python this is easy, since it's all interpreted, but I have no clue how to do it with C#. Does anybody know how to achieve this in C#? Are there any libraries, techniques, etc?
It depends on how you want the user of the program to "provide the allowed functions."
If the user is choosing functions that you've already implemented, you can pass these around as delegates or expression trees.
If the user is going to write their own methods in C# or another .NET language, and compile them into an assembly, you can load them using Reflection.
If you want the user to be able to type C# source code into your program, you can compile that using CodeDom, then call the resulting assembly using Reflection.
If you want to provide a custom expression language for the user, e.g. a simple mathematical language, then (assuming you can parse the language) you can use Reflection.Emit to generate a dynamic assembly and call that using -- you guessed it -- Reflection. Or you can construct an expression tree from the user code and compile that using LINQ -- depends on how much flexibility you need. (And if you can afford to wait, expression trees in .NET 4.0 remove many of the limitations that were in 3.5, so you may be able to avoid Reflection.Emit altogether.)
If you are happy for the user to enter expressions using Python, Ruby or another DLR language, you can host the Dynamic Language Runtime, which will interpret the user's code for you.
Hosting the DLR (and IronPython or IronRuby) could be a good choice here because you get a well tested environment and all the optimisations the DLR provides. Here's a how-to using IronPython.
Added in response to your performance question: The DLR is reasonably smart about optimisation. It doesn't blindly re-interpret the source code every time: once it has transformed the source code (or, specifically, a given function or class) to MSIL, it will keep reusing that compiled representation until the source code changes (e.g. the function is redefined). So if the user keeps using the same function but over different data sets, then as long as you can keep the same ScriptScope around, you should get decent perf; ditto if your concern is just that you're going to run the same function zillions of times during the genetic algorithm. Hosting the DLR is pretty easy to do, so it shouldn't be hard to do a proof of concept and measure to see if it's up to your needs.
You can try to create and manipulate Expression Trees. Use Linq to evaluate expression trees.
You can also use CodeDom to compile and run a function.
For sure you can google to see some examples that might fit your needs.
It seems that this article "How to dynamically compile C# code" and this article "Dynamically executing code in .Net" could help you.
You have access to the Compiler from within code, you can then create instances of the compiled code and use them without restarting the application. There are examples of it around
Here
and
Here
The second one is a javascript evaluator but could be adapted easily enough.
You can take a look at System.Reflection.Emit to generate code at the IL level.
Or generate C#, compile into a library and load that dynamically. Not nearly as flexible.
It is in fact very easy to generate IL. See this tutorial: http://www.meta-alternative.net/calc.pdf
Is there an equivalent of PHP's highlight_string function in C#?
It is not necessary to be a built-in function.
Edit: If not is there a good library for that?
Edit2: I really need a server side solution for that like PHP does.
There is nothing in the framework that will give you the coloured output. You will have to use a library to do this.
C# Code Format was one of the first .Net ones to come up in Google, although it only supports highlighting of C#, VB, HTML, XML, T-SQL or Monad. It is capable of running server side though as it is written in C#. If you need it to format PHP, then it is probably quite simple to extend it to do so - just make a custom PHPFormat class.
You can build yourself a basic syntax highlighter easy using RegExp.
Here's an example:
Syntax Highlight in C#
Or if you want a html C# highlighter written in C#
C# Syntax Highlighter 2.0
Or a JS highlighter:
highlight.js
Nope, certainly not in the Framework itself. You'd have to look for libraries that support this.
If you want it for webpages (which I assume, since the PHP highlight function formats it as HTML) you can use Googles Code prettify. It's however a javascript, which on the other hand makes it code-behind language independent.
I'm using it at my site with great success :-)
You can find it here: http://code.google.com/p/google-code-prettify/
I recently wrote a SQL formatting library (Poor Man's T-SQL Formatter library) in C# (.Net 2.0) which, among the available options, supports just coloring the output (with html span tags).
It's available online for testing at http://poorsql.com, and here's a link that already sets the options to "just colorize" so you can test/play:
http://poorsql.com/?reFormat=false
I have a few modules of block of code in VBA to run on few Access databases. I would like to know how I should proceed if I want to convert the coding to C# environment. And is it possible to implement and obtain the same results as I am getting now with Access and VBA?
I am completely new to C# at this point.
Automatic conversion isn't possible at the moment, but doing it manually will also help improve your C# skills. There's a Top 10 article here that takes you through the common differences:
http://msdn.microsoft.com/en-us/library/aa164018%28office.10%29.aspx
You may also find the following links useful:
The MSDN page for developing Office solutions with C#:
http://msdn.microsoft.com/en-us/library/ms228286.aspx
The MSDN Visual C# application development page (for starting out in C# development):
http://msdn.microsoft.com/en-us/library/aezdt881.aspx
Good luck and I hope this helps.
One thing to be aware of is that some object name spaces and library references are included automatically when you are coding in VBA. These need to be explicitly added when working in C#. For example,
Selection.TypeText("foo")
in VBA becomes
using Microsoft.Office.Interop.Word;
Application word = new Application();
word.Selection.TypeText("foo");
in C#. Library references can be added by right-clicking the References folder in the Solution Explorer and choosing "Add Reference".
Generally, you should be able go convert the code manually. You won't find any automated code converters that will do it.
That being said, the frameworks which you use to access data are quite different in the C# and VBA worlds (they're even quite different betwween VB.NET and VBA!). So you're going to want to read up on ADO.NET before you get started.
Given that VBA is VERY similar to vb.net, then I would suggest you converting to VB.net. In fact you can often cut + paste in code, especially if such code is from a code module. If your code has DAO.Recordsets, then I suggest creating a class in VB.net that “mimics” the dao.recordset. As such most VBA code can near 100% go into a vb.net code module. So this suggests that you don’t need a conversion, but simply move the VBA code to VB.net. In fact 99% of the commands in VBA exist with the SAME syntax in VB.net.