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

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

Introduction

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.

Method

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 http://www.ssw.uni-linz.ac.at/Research/Projects/Coco/Contributions/ – 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 =
  "a"
.

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)

Coco.exe

Parser.Frame

Scanner.Frame

Build.bat

(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
(root)

Coco.exe

Parser.Frame

Scanner.Frame

Build.bat

(root)\FirstPass

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

Scanner.cs

Parser.cs

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

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

(root)\SecondPass

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

Scanner.cs

Parser.cs

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.

Conclusion

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!

History

  • 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

License

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

Share

About the Author

c2423

United Kingdom United Kingdom
No Biography provided

Comments and Discussions

 
GeneralMy vote of 5 PinprofessionalAndreas Gieriet7-Sep-14 9:40 
GeneralRiveting stuff PinmemberAlan Beasley6-May-10 4:13 
GeneralRe: Riveting stuff Pinmemberc24236-May-10 4:37 
GeneralRe: Riveting stuff PinmemberAlan Beasley6-May-10 4:41 
GeneralRe: Riveting stuff Pinmemberc24236-May-10 4:49 
GeneralRe: Riveting stuff PinmemberAlan Beasley6-May-10 6:24 

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
Web02 | 2.8.141223.1 | Last Updated 6 May 2010
Article Copyright 2010 by c2423
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid