Last changed on 12.5.2005
based on CUP v0.10j by Scott E. Hudson
C# CUP is a C# version of the popular Java based Parser Constructor CUP[1]. C# as a representant of the .NET Language Family is more often used in business and for educational purpose. It's clear that a tool for building automatic LR-Parser in such language will be useful in future. The difference between C# and Java lies more in the different System libraries as in the language specification. Therefore it was not very difficult to translate existing Java CUP to the .NET platform. This make possible to change developing platform with minimal effort on adaptation in existing cup specification files. For more information about compiler building and parsing i recommend you the YACC manual or book[3].
Parser cannot work without appropriate lexer (Tokenizer Tool). JLex is mostly used with JAVA CUP and I translated this tool too to the .NET platform(see the CS_Lex Homepage[2]).
This project was done during as by baccalaureus project at Distributed System Group on the Vienna Technical University. I would like to thank the department and especially Engin Kirda, my supervisor, for his support.
.NET Platform made the redistribution and installation of application much easier. C# CUP is provided in two different forms.
You can get C# CUP as compiled application with DLL-library. The whole installation consist of unpacking this two files in a same directory. To run C# CUP you need .NET Framework 1.0.3705 or higher installed on your computer. The DLL Library contains Runtime classes definition. This classes are needed to use the generated files later in your own application. Therefore they are in separated in DLL. If you wish you can of course make your own compilation from the source codes, where all classes will be in a single application.
You can get C# Cup source codes files. There are many reasons why you would like to have them. At first you can put the Runtime Classes directly into your own application without the need of external DLL libraries. Otherwise you could find some bugs in CUP ;-) and you would like to fix them. However you can try it...
To compile the source files I recommend the usage of Visual Studio .NET but you can do that also with the command-line C#compiler (csc), which is in the .NET Framework SDK.
On a computer with installed .NET Framework the command-line usage of C# Cup is: C# CUP.exe [options] <cs_cup_definition.cup>
You can omit all options at all. For full specification of of command-line switches see the original Java CUP specification[1]. The most important of them are:
The language specification is almost equal to the CUP Specification(0.10j version). You have to define terminal and nonterminal symbols. Afterwards you have to specify the grammar how non-terminals are build up. Finally you have to define user code, which is executed on operators and functions. For complete specification see the original Java CUP manual.
The only difference to the Java Specification, is that you have to use C# code in the user-code block. According to different keywords in C# you have to use namespace instead of package. And usage instead of import.
|
JAVA CUP |
C# CUP |
|
package |
namespacec |
|
import |
usage |
Java and C# are very similar languages, but there remains some differences, which should be noticed in case of conversion to .NET platform. A Java Conversion Assistant from Microsoft exists, which should mostly automatically perform this conversion. As I don't have the proper .NET Version I couldn't use it, so I'm not able to comment this tool. However the transformation is not too difficult and for smaller projects it's maybe more effective to do it manually in order to learn and understand the differences in the languages. The following list should be a guide or checklist.
This list is not complete and should be taken only like a starting help. As I already mentioned the system functions differ mostly only in the first letter or in the writing, but the are exceptions. Some Classes are left out or are total rewritten. Some functions can be missing. So you have to check this and possible make some of them by yourself.
There is one more difference in the Interfaces Enumaration -> IEnumerator. IEnumerator have a MoveNext() functions which advance to a new element and returns if this movement was successful. The current Element can be retrieved by .Current. This is a small example of equivalent codes in Java and C#
|
JAVA Code |
C# |
for (Enumerator e= a.getEnumerator();e.hasMoreElements();) {..... a = (Integer) e.nextElement(); } |
IEnumerator e= a.GetEnuration() while (e.MoveNext()) {..... a = (int) e.Current; } |
A full- functional parser is not working without an lexical analyzer, which split the file input into tokens symbols. Each time the parser is ready to work a new symbol it calls scanner.next_token(). The scanner should return an object of the type Symbol. This contains the next analyzed symbol. The questions remains, how do we get such lexer. Of course it's possible to write one by himself. This way is not very comfortable if working with definition, which changing ofen or is really complex.
The JLex is really good tool to build automatic lexer from a simple rule definition. I have rewritten it also to C# and you can read more on the C#Lex Site [2]. This tool have a switch, which generates C#CUP conform lexer, which are mostly even quicklier as hand-written lexer.
One example can mostly explain more than thousand words of description. I took the examples suplied with JCup and translated them to C# Cup.
The first one named minimal.cup defines a minimalistic calculator with plus and multiply on positive integers with paranthesis support. Each expression have to be ended with ';'. Afterwards its evalueted by the parser. To have a fully runnable application a C#Lex example file is included, which contains definition of simple lexer for this task. Together with C#Lex you lexer file( minimal.lex.cs). C#Cup produce in basic configuration parser.cs and sym.cs. These 3 files together makes already runnable application, which you can test and enlarge in order to get accustommed with C#CUP.
The second one names calc.cup contains definition of a simple calculator. With just basic instructions defined. As there is no lexer or lex file defined you have to build a simple wrapper-application arround or adapt lexer file from the another example.
The whole grammar definition of C#Cup is also defined in a cup file. This means you can build up a new parser class for the C#CUP definition. This new parser class is identical with the suplied parser file in the source code, only in different style and with less comments. I recommend to look on it to see more complex definitions too.
Following files are available for download:
CUP was originally written by Scott Hudson, in August of 1995.
It was extended to support precedence by Frank Flannery, in July of 1996.
It was translated to C# and .NET Plattform by Samuel Imriska in September 2003 as part of my baccalareus work on Vienna Technical University.
I would like to thank to all that people, who send me mails, if some bug or missing file occurs. Without those people, who forward their knowledge and problems the Internet community wouldn't be the same.