Click here to Skip to main content
12,950,268 members (69,766 online)
Click here to Skip to main content
Add your own
alternative version


10 bookmarked
Posted 5 May 2010

Setting up COCO/R for Use in C# Visual Studio Projects

, 6 May 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
How to set up the COCO/R compiler with Visual Studio


COCO/R is a free compiler generator tool. Compiler generator tools are useful not only in creating compilers – they can be used to read files, or do calculations, and various other things. This post is not about the benefits of using COCO/R or about how to write compilers, but is rather about how to set it up for use with C# and Visual Studio.

Visual Studio is not an easy tool to customize to read new languages – C#, VB.NET, etc. are already supported, F# support can be added with extra installs from Microsoft (older versions of Visual Studio only - F# is now included in VS2010.) However, reading and compiling non standard languages is not so easy to achieve.

This article aims at showing how to incorporate COCO/R as a tool to be used with Visual Studio.


COCO/R can be downloaded from the COCO/R homepage. The files that need to be download for COCO/R to work are as follows:

  • Coco.exe – This executable reads template files and produces class files to make up a basic compiler (or file reader or calculator or...).
  • Scanner.frame – This is the template file used to produce a Scanner.
  • Parser.frame – This is the template file used to produce a Parser.

In addition, it is useful to download a template file for input to COCO. While there are samples available on the COCO/R homepage, these are files for complex grammars, and as such do not work well for creating grammars from scratch. A good template can be found at – In this template, Empty.atg provides a solid template with comments on how to document what each feature is. For the sake of getting set up, only minimal work is to be done with this file. Under the "Productions" section of the file, the following lines can be found:

Empty =


This should be changed to:

Empty =

This ensures that the production rule is not blank, and so when running COCO/R against this file, there should be no errors. In terms of the actual detail of the file, I won't explain it here as it is not strictly required for getting set up – this is the only change required to work on the build process.

Now that all the files are downloaded into a folder and set up, COCO/R can be run to generate a basic compiler. From the command line, the program can be run:

coco Empty.atg -namespace "MyNamespace"

Here, we are using "Empty.atg" as an input file to coco, and specifying that the namespace to be used is MyNamespace.

The first time this is run, two files are generated in the target directory. Parser.cs and Scanner.cs form the basic compiler. The second time the command is run, COCO/R creates backups – the existing Scanner and Parser have "old" added to their extensions so that the programmer can revert to older versions if needed. A new Parser and Scanner are also created. So after the second run, there are always 4 generated files in the target folder. The "old" files are overwritten with each run of COCO/R.

The output files save a lot of work for the programmer, however I personally have a couple of problems with the outputs...

The classes are not marked "partial" – any changes made to the files within the VS editor will be lost whenever COCO/R is used. It would be advantageous for the files to be marked partial, so that extra methods can be added to separate files which are not written over by COCO/R. People who know how to use COCO/R might at this point be saying that the ATG input file does allow free form code at the top for this reason, however I would argue that while this works, you do not get the benefit of the VS design time features because the code would need to be written in a plain text editor, rather than the IDE. So the solution I would propose for the issue is to open the frame files, and mark the start of the classes as being partial (and possibly also internal if the compiler is to be included as part of a class library with a façade interface.)

The second major problem is that the output files always have the same names, no matter what the compiler name will be. If a multi-pass compiler is to be created or if multiple languages are to be supported, multiple Parser and Scanner files will need to be created, with different file names so as not to be overwritten by each other. The only solution I have found for this is to keep ATG files and output files in different directories and namespaces.

Since the command to compile is time consuming to type, and multiple targets may be used, I find it helpful to set up a batch file to rebuild classes.

First, the directory structure is set up like so:

Directory Files





(root)\FirstPass Empty.atg (containing the specification for the first compiler pass)
(root)\SecondPass Empty.atg (containing the specification for the second compiler pass)

Below are the contents of the batch file which compiles the two pass compiler:

coco "Pass1\Empty.atg" -namespace "Pass1? -frames "" -o "Pass1?
coco "Pass2\Empty.atg" -namespace "Pass2? -frames "" -o "Pass2"

Here we are compiling the Empty.atg files in two directories, giving them different namespaces to each other. We specify that the frame files to be used are in the same directory as coco.exe with the empty string, and with “-o” we specify the output directories below the root. Assuming that the ATG was a valid COCO input, the folders should now be structured as follows:

Directory Files






Empty.atg (containing the specification for the first compiler pass)



Scanner.cs.old (if not the first run)

Parser.cs.old (if not the first run)


Empty.atg (containing the specification for the second compiler pass)



Scanner.cs.old (if not the first run)

Parser.cs.old (if not the first run)

Of course, if there is error information from COCO/R, the batch file will need to be run from the command line so that the error information can be read.

The same build information as the batch file can be put into the pre build information of the project, but I prefer not to rebuild the COCO/R components with each build of the project, and would rather have control of when the Parser and Scanner are rebuilt, to avoid compile time problems if changes to the ATG input are not completed.


I hope that the above explanations show that COCO/R support can be easily added to C# Visual Studio projects with a bit of setup in advance. A one click build process is advocated by Joel Spolsky (see Point 2 of this article), and this architecture and approach allows COCO/R to be used in such a process.

Points of Interest

I wrote a first version of this in January 2009 as a blog post, and only got round to making changes and uploading it as an article a long time after (May 2010). Then I discovered that there are a couple of Visual Studio plugins that do this. I tried to use them, but found that while at work my machine has some permissions problems, so did not work. So I'll freely admit that the plugins look nicer than my way of doing things, but I maintain that my way still has its place for machines that are more crippled by network policy!


  • Version 1 5th May - Initial version
  • Version 2 5th May - Corrected some typos
  • Version 3 6th May - Added a history section and a points of interest section


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


About the Author

United Kingdom United Kingdom
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
Andreas Gieriet7-Sep-14 8:40
professionalAndreas Gieriet7-Sep-14 8:40 
GeneralRiveting stuff Pin
Alan Beasley6-May-10 3:13
memberAlan Beasley6-May-10 3:13 
GeneralRe: Riveting stuff Pin
c24236-May-10 3:37
memberc24236-May-10 3:37 
GeneralRe: Riveting stuff Pin
Alan Beasley6-May-10 3:41
memberAlan Beasley6-May-10 3:41 
GeneralRe: Riveting stuff Pin
c24236-May-10 3:49
memberc24236-May-10 3:49 
GeneralRe: Riveting stuff Pin
Alan Beasley6-May-10 5:24
memberAlan Beasley6-May-10 5:24 

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 | Terms of Use | Mobile
Web02 | 2.8.170525.1 | Last Updated 6 May 2010
Article Copyright 2010 by c2423
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid