Modifying C# grammar from version for ANTLR3.2 to version for ANTLR4

112 views
Skip to first unread message

Petr Kloza

unread,
Jan 4, 2015, 6:59:30 AM1/4/15
to antlr-di...@googlegroups.com
Hi all,
I want to use ANTLR4 as a compiler for my project (Generator of Documentation), because it seems to me the best of all compilers I have tested. I want to parse a C# code, but I have not found any grammar for ANTLR4. I do not want to use ANTLR3.2 because it is so laborious to write target code to the grammar for the functionality of my generator and I have also written the Visitor which should make the functionality.
The problem is, that the grammar for ANTLR 3.2 downloaded here: http://antlrcsharp.codeplex.com/ is not compatible with ANTLR4 even when I modified it (I have eliminate syntax errors and warnings). The parser makes error nodes of type Type_declarationContext with NoViableException. I do not know why it want to make everything type_declaration context even if the code does not begin with one of these tokens: class, struct, delegate, enum or interface. Have a look at the picture from debug: In a simple code with one class_declaration, it makes four more Type_declarationContext - which belongs to syntax rule type_declaration, but there should be for example property_declaration inside the class_declaration.

Does anyone know how to update the grammar to be compatible with ANLTR4?

Here You can have a look on my project, where I have done some work on the C# grammar from the codeplex... It is easy to use. I will be so happy if anyone solve the bug and update the C# grammar for ANTLR4.
My project link: http://ulozto.cz/xg8uttEG/argutec-xdoc-zip Password: diplomaproject

Thank You very much!

Petr Kloza


James Petry

unread,
Mar 29, 2015, 5:30:31 AM3/29/15
to antlr-di...@googlegroups.com


On Sunday, 4 January 2015 11:59:30 UTC, Petr Kloza wrote:
Does anyone know how to update the grammar to be compatible with ANLTR4?

As a learning exercise I am updating the C# ANTLR 3 grammar from here:

  https://github.com/ChristianWulf/CSharpGrammar

At the moment there are still some semantic predicates in java in the updated grammar.
I am gradually removing them following the advice in the ANTLR 4 book.

Here is my project with a simple refactoring test that I have run successfully on real life C# code:
  https://github.com/gavilancomun/refactor-csharp-java

Best Wishes,
James

James Petry

unread,
Mar 29, 2015, 6:53:15 AM3/29/15
to antlr-di...@googlegroups.com
I've completed the rewrite, so the ANTLR 4 grammar is now target language-neutral.

Here is a port of my java code to C#, using the same grammar:

  https://github.com/gavilancomun/refactor-csharp-csharp

Best Wishes,
James

Terence Parr

unread,
Mar 29, 2015, 11:29:54 AM3/29/15
to antlr-di...@googlegroups.com
cool. :)  James, can we add this to https://github.com/antlr/grammars-v4 ?  You could fork, then add csharp/CSharp4*.g4 + LICENSE

Ter
--
You received this message because you are subscribed to the Google Groups "antlr-discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to antlr-discussi...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

James Petry

unread,
Mar 30, 2015, 6:12:42 AM3/30/15
to antlr-di...@googlegroups.com
On Sunday, 29 March 2015 16:29:54 UTC+1, the_antlr_guy wrote:
cool. :)  James, can we add this to https://github.com/antlr/grammars-v4 ?  You could fork, then add csharp/CSharp4*.g4 + LICENSE

Sure. There's a pull request with this now.

Best Wishes,
James

Terence Parr

unread,
Mar 30, 2015, 11:51:28 AM3/30/15
to antlr-di...@googlegroups.com
thanks. it’s in now!
T
Reply all
Reply to author
Forward
0 new messages