I'm planning to write a C# 3.0 compiler in C#. Where can I get the grammar for parser generation?
Preferably one that works with ANTLR v3 without modification.
Take a look at C# Language Specification. In the chapter B. Grammar you'll find the grammar.
I ran into ANTLR C# Grammar on CodePlex. It's a relatively new project and uses ANTLR 3.2. It says it supports C# 4.0 and is licensed under the Eclipse Public License (EPL).
I played with it a little. It has a bunch of test files containing expressions. It supports lambdas, unsafe context, ... as you'd naturally expect. It parses a C# file and hands you an abstract syntax tree. You can do whatever you want with it.
Are you looking for something like this or this?
Please also refer to C# ANLTR grammar question.
Take a look at COCO/R it seems that they have the language specification for C# 3.0.
Related
Question:
I am looking to develop a Scripting Language like Lua, but you use
if (paramters) {
codeToDo()
}
rather than
if parameters do
code_to_do()
end
but have yet to find a good tutorial out their on the internet.
Parameters:
I want it to be interpreted, and I also want to make it in C#, so that it integrates with it the easiest.
Writing a language and an interpreter is not an easy task, you have to define the complete syntax and after write a parser for it, finally you have to implement a interpreter capable of executing your language.
There are already answers about creating programming languages here on SO:
Suggestions for writing a programming language?
How would you go about writing a simple programming language?
Some books that could guide you in the right direction:
Language Implementation Patterns
" Written by Terrence Parr creator of ANTLR a parse generation tool in Java, teaches the basics of language definition and the implementation of a bytecode vm."
Writing Compilers and Interpreters: A Software Engineering Approach
Couldnt find a practical book with code in C#, most of books about compilers comes with code in C, only some with Java.
I'm currently in the process of writing a programming language which extends C# mainly by adding custom operators and keywords.
I have found the grammar file for c# 4 in http://antlrcsharp.codeplex.com/ but cannot find any examples on how to import this file in my own grammar file.
Can anyone provide some examples or point me to other projects which archive the same thing?
If you want to extend c# that means you're doing a DSL?
Check this one out Irony from codeplex, Hanselman explains it better
Starting with ANTLR 3.1 there is a grammar composition feature.
You can import other grammars to yours and then use and/or override rules from the imported ones.
Another approach would be to modify the existing C# grammar to your needs.
I've been looking for a free (as in "free beer") library that can parse C# 3.0 code (actually, C# 2.0 with lambdas would do it) and return some sort of AST/code DOM for a personal project. However, most alternatives I've seen up to now are inadequate:
Microsoft.CSharp.CSharpCodeProvider.Parse throws a NotImpementedException;
Visual Studio's "exposed" code parser sucks;
The ways of Mono's C# compiler are impenetrable (and next to not documented);
The C# Parser project at CodePlex only parses C# 2.0 code and thus we forget lambdas;
Another project on SourceForge claims to parse C# to CodeDOM, but it's 2000 days old so I guess it's not C# 3.0;
... and it goes on for several pages of Google results.
So... is there a free, working C# 3.0 parser out there, that can be plugged into another project?
Take a look at http://antlrcsharp.codeplex.com/
It is a C# 4 grammar for the Antlr parser generator.
I've had good luck with NRefactory, written in C# and used for the SharpDevelop IDE written for Mono:
http://wiki.sharpdevelop.net/NRefactory.ashx
I want to enhance an application with scripting support like many other applications have, e.g. MS Office using VBA or UltraEdit using JavaScript.
Which libraries do exist for C#/.NET (and which language(s) do they support)?
Please check CS Scripting library
Here is an article about scripting Photoshop CS with C#
This one discusses using LUA as scripting lib with C#.
IronPython is a dynamic .NET scripting language.
IronPython is an implementation of the Python programming language running under .NET and Silverlight. It supports an interactive console with fully dynamic compilation. It's well integrated with the rest of the .NET Framework and makes all .NET libraries easily available to Python programmers, while maintaining compatibility with the Python language.
See IronPython embedding for examples showing ways to call IronPython from .NET apps.
The IronPython Calculator and the Evaluator goes into the details of using IronPython from a C# application.
Lua is often touted as being one of the better ones... Try looking at this other question for more information: What are the most effective ways to use Lua with C#?
Also:
Lua Interface
Binding code to Lua
See "What is the best scripting language to embed in a C# desktop application."
Also see "It Already Is A Scripting Language" from Eric Lippert.
Don't forget LSharp, LISP in .NET. Something to keep an eye on if you are exploring functionality stage. Maybe Rob Blackwell will be glad to hear you're considering it.
You can bake your own scripting environment with Mono.CSharp (just one simple dll) or Roslyn, both are getting quite mature now.
Mono contains the Evaluator class and Roslyn the ScriptEngine, both make it a breeze setting up a script environment. Of course something like ScriptCS already builds on that (Roslyn) and gives you more features.
For an C# script environment built on Mono.CSharp you can check out CShell (which I made).
Depending on your needs, the SILK library might be a good option.
It's an easy to use interpreter. The interpreted language is not C#. It's a custom language that was designed to be easy to use (very little punctuation, not case sensitive, etc.) But it does support functions.
Built-in functions are handled via events. That is, when the interpreted code calls one of your internal functions, it raises an event in your program.
For example, in Java there is Functional Java and Higher-Order Java. Both essentially give a small API for manipulating higher-order, curried functions, and perhaps a few new data types (tuples, immutable lists).
LanguageExt looks very promising for making functional style programming in C# easier.
https://github.com/louthy/language-ext
have you looked into F#?
Also a neat blog post would be here that talks about how to use the new generic / lambda expressions built into c# 3.0.
If you just add using System.Linq to the top of your source file there are a LOT of nice new functions added to working with collections such as folding / filtering / etc.
Assuming you can't use F# for whatever reason, and just want to use functional paradigms and idioms in your C# code to improve quality & reliability:
Functional style pattern matching for C#
Monad library for C#/.Net
There is also 'elevate' which has some functional things like option types (maybes) etc.
I think you want F#
Also, the more recent versions of C# have a lot of functional concepts included in the base langauge.
There may be such a library for C#, but you should probably consider just using F# http://research.microsoft.com/fsharp/fsharp.aspx and http://msdn.microsoft.com/en-us/fsharp/default.aspx.
Microsoft plans to make F# a first-class language in Visual Studio so there should be little risk in using one of the CTPs to build your initial stuff.
If you're looking for something that extends C# then no, but there is F# which is a .NET based functional language. From the "About F#" page:
F# is a typed functional programming language for the .NET Framework. It combines the succinctness, expressivity, and compositionality of typed functional programming with the runtime support, libraries, interoperability, tools and object model of .NET. F# stems from the ML family of languages and has a core language compatible with that of OCaml, though also draws from C# and Haskell. F# was designed from the ground up to be a first-class citizen on .NET, giving smooth interoperability with other .NET languages. For example, C# and F# can call each other directly. This means that F# has immediate access to all the .NET Framework APIs, including, for example, Windows Presentation Foundation and DirectX. Similarly, libraries developed in F# may be used from other .NET languages.
Since F# and OCaml share a similar core language, some OCaml libraries and applications can cross-compile either directly or with minor conditionally-compiled changes. This provides a path to cross-compile and/or port existing OCaml code to .NET, and also allows programmers to transfer skills between these languages. A major focus of the project has been to extend the reach of OCaml-like languages into arenas where they have not traditionally been used. Throughout the project the designers of F# are grateful for the support and encouragement of Xavier Leroy and others in the OCaml community.
Not a shrink-wrapped library per se, but Luca Bolognese of Microsoft has a series of blog posts where he builds a C# library for functional programming with types like tuples, records, type unions and so on:
Also Linq is basically a library for functional programming with syntactial support in C#.
Check out http://code.msdn.microsoft.com/FunctionalCSharp for some samples.
F#, there's a CTP release available from microsoft.
One more option to consider is FuncSharp. It's not so heavy as LanguageExt and it does cover the most important patterns/aspects.