Click here to Skip to main content
13,595,773 members
Click here to Skip to main content
Add your own
alternative version


52 bookmarked
Posted 29 Oct 2005
Licenced CPOL

Coco Custom Tool for Visual Studio.NET

, 29 Oct 2005
Rate this:
Please Sign up or sign in to vote.
Use the award winning Coco compiler's compiler directly from within Visual Studio
Sample Image - vsCoco.png


I have been publishing one or two articles about formula evaluation, and so far all programs were written manually. For a recent project, I need to parse far more complex grammars and I really needed some help.

I had a look on the internet and I found a project called Coco/R from the Johannes Kepler Universität Linz. This is how they describe their product : "Coco/R takes a compiler description in the form of an attributed grammar (EBNF syntax with attributes and semantic actions) and translates it into a scanner and a recursive descent parser.... Coco/R has been used successfully in academia and industry. It combines the functionality of the well-known Unix tools Lex and Yacc".

I used Coco for a while and despite being extremely good I found working with it rapidly frustrating because I had to run it manually and it was not really well integrated with Visual Studio.


Anyone wanting to use this tool should be familiar with EBNF grammars. There are several good introductions available on the internet.

I also particularly recommend the reading of the Compiler Generator Coco/R User Manual.

Installing vsCoco

You need to download and run the file vsCocoRegistration.exe. The registration should work fine with Visual Studio 2003.

If you want to use Coco with the given sample, there is nothing else to do.

If you want to use it with your own file, you must add your grammar to your project and set its property to:

  • Build Action: Content
  • Custom Tool: Coco
  • Namespace put what you want or blank
  • Optionally you can provide the Parser.frame and/or Scanner.frame files within your project if you want to customize them.

If it doesn't work with your version, please post in the forum below.

If you are able to fix the vsCoCoRegistration please send me the code pascal_cp@ga$ (remove the $).

Using vsCoco

To start with, you can try to play with the Calculator sample I provided in the download. The calculator calculates formula like 12+34*55/2.

The sample contains only 5 lines of C# code.

private void button1_Click(object sender, System.EventArgs e)
        Parser p = new Parser(comboBox1.Text);
        textBox1.AppendText(">" + comboBox1.Text + "\r\n" 
                + p.result.ToString() + "\r\n");

As you can see, most of the login must be in the Parser Object.
The parser is created automatically from this grammar:


    public double result = 0;
// The $L option let you compile directly within your grammar
// You can comment and uncomment the line to fit your development requirements.

    digit = "0123456789".
    cr  = '\r'.
    lf  = '\n'.  
    tab = '\t'.

    number = digit {digit} ['.' {digit}].
// We don't use comments here but this is only a sample
COMMENTS FROM "//" TO cr lf 

IGNORE cr + lf + tab 


OPERAND<OUT val double>        
=            (.  val = 0; .)
  number         (.    val = Double.Parse(t.val,
  | "(" EXPR<OUT val> ")"    
// Priorities in FGL 
//       ()        (Parenthesis)
// 10    -        (Unary neg)
// 09    * /        (Multiply and Divide)
// 07    + -        (Add and Subtract)

EXPR10<OUT val double>
=                       (.    bool neg=false; .) 
        ( '-'    (.    neg=!neg; .)
        | '+'    (.    /*nothing to do*/ .)
    OPERAND<OUT val>        (.    if (neg) val*=-1; .)

EXPR09<OUT val double>    
    EXPR10<OUT val>        
    {        (.    double val2; .)
        ( '*' 
        EXPR10<OUT val2>    (.    val*=val2; .)
        | '/' 
        EXPR10<OUT val2>    (.    val/=val2; .)

EXPR<OUT val double>    
    EXPR09<OUT val>
    {        (.    double val2; .)            
        ( '+'                
        EXPR09<OUT val2>    (.    val+=val2; .)                
        | '-'                
        EXPR09<OUT val2>    (.    val-=val2; .)                
    EXPR<OUT result>.

END calc.

This grammar is a fairly standard one. If I try to read it in English, it would say:

  • This grammar will produce a parser called calc.
  • Calc parser return expressions
  • An expression can be a sum or if not, a product of signed numbers.
  • The multiplication should be done before the additions however the minus and plus sign have more priority if they are signs.

As you can see, there is more complexity than it looks. This is a bit hard for me to describe what this grammar does and how it works and this is not my goal.

What I would like to share with you is this tool and hopefully raise an interest for Compiler's compilers if you are new in this subject.

How Does It Work?

Coco Modifications - 1: The #line

I made several major modifications to Coco/R.

First I wanted to trace within the grammar. This was the easy part, I modified Coco source file and added the $L option. If you insert $L in the beginning of your grammar, Coco compiler will add many #line in your code.

For example:


    public double result = 0;
// The $L option let you compile directly within your grammar
// You can comment and uncomment the line to fit your development requirements.


OPERAND<OUT val double>        
=                (.  val = 0; .)
  number             (.    val = Double.Parse(t.val,
  | "(" EXPR<OUT val> ")"    

will generate:

    void OPERAND(
#line 31 "C:\dotnet\vsCoco\Calculator\Calc.atg"
        out double val
#line hidden
) {

#line 32 "C:\dotnet\vsCoco\Calculator\Calc.atg"
            val = 0; 
#line hidden

        if (la.kind == 1) {

#line 34 "C:\dotnet\vsCoco\Calculator\Calc.atg"
                 val = Double.Parse(t.val,
#line hidden

        } else if (la.kind == 2) {
#line 38 "C:\dotnet\vsCoco\Calculator\Calc.atg"
             out val
#line hidden
        } else SynErr(9);

This #lines are very helpful, the Visual Studio IDE understands it well and lets you debug your generated program using the original source grammar.

I find it very useful; you can comment and uncomment the $L line to fit your development requirements.

Coco Modifications - 2: A Real Visual Studio Custom Tool

My second goal was to run Coco directly from Visual Studio as a custom tool, rather than having to use batch files.

The main advantage of a custom tool is that it will be automatically called when the source grammar changes and not at each compile.

Visual Studio publishes an interface called IVsSingleFileGenerator.

This interface defines two methods:

  • int DefaultExtension(out string)
  • int Generate(string, string, string, System.IntPtr[], out uint, Microsoft.VisualStudio.Shell.Interop.IVsGeneratorProgress)

Providing these two interfaces is the base of the work needed to make a Visual Studio Custom tool.
With the good information, this is after all fairly straight forward. I used and modified the GotDotNet User Sample: BaseCodeGeneratorWithSite.

Coco Modifications - 3 : An Installer for the Visual Studio Custom Tool

Now that we have a DLL which can be a Visual Studio plugin, you need to register it. This could prove a lot harder than expected. Fortunately, I read a excellent article called Automated Registration of Visual Studio Custom Tools by Michael McKechney.

I butchered his sample program and produced vsCocoRegistration.exe.

Known Bugs

vsCocoRegistration does not yet work with all version of Visual Studio .NET. This is just a question of changing the Registry GUIDs but I don't have that many versions to test it with. So feel free to ask in the forum below.



  • October 29th 2005 - First release
  • November 1st 2005 - Corrected a couple of mistakes in the article


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Pascal Ganaye
Software Developer (Senior)
France France
I am a French programmer.
These days I spend most of my time with the .NET framework, JavaScript and html.

You may also be interested in...


Comments and Discussions

QuestionIt is free to use ? Pin
m.raetzel28-Sep-06 2:20
memberm.raetzel28-Sep-06 2:20 
AnswerRe: It is free to use ? Pin
Tveitane1-Oct-06 6:36
memberTveitane1-Oct-06 6:36 
Sounds good that it can easily be made to works with VS2005 - how about your changes to the installer for VS2005, would you like to share them with the world? I would be grateful...

All the best,
Knut Tveitane
GeneralRe: It is free to use ? Pin
René André22-Dec-06 22:11
memberRené André22-Dec-06 22:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web03-2016 | 2.8.180621.3 | Last Updated 29 Oct 2005
Article Copyright 2005 by Pascal Ganaye
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid