Click here to Skip to main content
Click here to Skip to main content

Coco Custom Tool for Visual Studio.NET

, 29 Oct 2005 CPOL
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

Introduction

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.

Background

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$naye.com (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);
        p.Parse();
        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:

COMPILER calc

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

/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
CHARACTERS
    digit = "0123456789".
    cr  = '\r'.
    lf  = '\n'.  
    tab = '\t'.

TOKENS
    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 

PRODUCTIONS

/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
OPERAND<OUT val double>        
=            (.  val = 0; .)
  (
  number         (.    val = Double.Parse(t.val,
                NumberStyles.Float, 
                CultureInfo.InvariantCulture); 
            .)
  | "(" 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; .)                
        )
    }                         
    .
  
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
calc
=                    
    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:

COMPILER calc

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

...

OPERAND<OUT val double>        
=                (.  val = 0; .)
  (
  number             (.    val = Double.Parse(t.val,
                    NumberStyles.Float, 
                    CultureInfo.InvariantCulture); 
                .)
  | "(" 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) {
            Get();

#line 34 "C:\dotnet\vsCoco\Calculator\Calc.atg"
                 val = Double.Parse(t.val,
            System.Globalization.NumberStyles.Float, 
            System.Globalization.CultureInfo.InvariantCulture); 
                            
#line hidden

        } else if (la.kind == 2) {
            Get();
            EXPR(
#line 38 "C:\dotnet\vsCoco\Calculator\Calc.atg"
             out val
#line hidden
);
            Expect(3);
        } 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.

Links

History

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

License

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

Share

About the Author

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

Comments and Discussions

 
GeneralWanto to use this skeleton and replace cocoR parser with a C# lex program i've ported te C# from JLex PinmemberAndyHo8-Feb-09 10:30 
GeneralRe: Wanto to use this skeleton and replace cocoR parser with a C# lex program i've ported te C# from JLex PinmemberPascal Ganaye9-Feb-09 7:53 
Hi Andres,
 
This is a bit far and I don't remember everything in detail.
I did a bit of debugging at the time.
 
The trick is to use Visual Studio to debug another instance of visual studio.
From memory, I think you can either :
- In your project debug properties you select start external program and enter: 'C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\devenv.exe'
- You can also use the menu debug 'Attach to process' and select a second instance of Visual Studio you have run previously.
 
For the GUID, perhaps changing a digit is ok.
The best though is to use the menu tools create guid.
I created a few for your own use:
{B08919D1-73B1-46c8-AA95-CFCD02519B98}
{9C57B160-D1D2-4f31-BDAF-9DD530322864}
{74F63F7D-5CCA-4b26-A190-FB5B5DFCD953}
{81A2B711-D30C-4468-B635-6321027B2BAE}
{973AF25C-4469-423a-808E-B90D1128ADEC}
 
Let me know how you're going. my email is : pascal 2007 at ganaye dot com.
GeneralRe: Wanto to use this skeleton and replace cocoR parser with a C# lex program i've ported te C# from JLex PinmemberPascal Ganaye9-Feb-09 8:09 
GeneralRe: Wanto to use this skeleton and replace cocoR parser with a C# lex program i've ported te C# from JLex PinmemberPascal Ganaye9-Feb-09 8:11 
GeneralRe: Wanto to use this skeleton and replace cocoR parser with a C# lex program i've ported te C# from JLex PinmemberPascal Ganaye9-Feb-09 8:14 
GeneralUsing in VS2k8 PinmemberRichardM120-Nov-08 15:36 
GeneralRe: Using in VS2k8 Pinmemberalex_zero23-Nov-08 22:45 
GeneralUsing your own Scanner.frame and Parser.frame PinmemberDangersaur25-Jun-08 9:30 
GeneralRe: Using your own Scanner.frame and Parser.frame PinmemberRichardM120-Nov-08 15:48 
GeneralvsCocoR for vs2005 release Pinmemberalex_zero14-Jan-08 21:19 
GeneralRe: vsCocoR for vs2005 release [modified] PinmemberRichardM120-Nov-08 12:19 
Generalquery PinmemberRana ji4-Apr-07 0:22 
GeneralRe: query PinmemberPascal Ganaye7-Jan-08 1:37 
AnswerRe: query PinmemberRana ji10-Jan-08 22:24 
GeneralRe: query PinmemberRana ji10-Jan-08 22:27 
Generalquery PinmemberRana ji4-Apr-07 0:21 
GeneralGrammars in subfolders in VS2005 and diff. Pinmemberdmoonfire22-Mar-07 9:59 
GeneralWorks with Microsoft Visual C# 2005 Express Edition on Vista PinmemberCarstenKnop27-Feb-07 13:21 
QuestionIt is free to use ? Pinmemberm.raetzel28-Sep-06 3:20 
AnswerRe: It is free to use ? PinmemberTveitane1-Oct-06 7:36 
GeneralRe: It is free to use ? PinmemberRené André22-Dec-06 23:11 
QuestionvsCoco with VS 2005? Pinmemberhahnl5-Aug-06 5:08 
AnswerRe: vsCoco with VS 2005? PinmemberPascal Ganaye5-Aug-06 6:07 
GeneralC# Generics with Coco Pinmemberhahnl4-Aug-06 22:32 
GeneralRe: C# Generics with Coco PinmemberTveitane5-Oct-06 2:50 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.1411023.1 | Last Updated 29 Oct 2005
Article Copyright 2005 by Pascal Ganaye
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid