How to use CoCo/R and the attributed C# grammar
CoCo/R is a compiler generator which takes a compiler description in the form of an LL(1) attributed grammar (ATG) and generates the scanner and the parser of the described parser.
The C# grammar, as presented in ECMA standard 334, is apparently not LL(1)
and not written in EBNF. So we translated it to EBNF, did some
factorizations, eliminated some productions, and inserted resolver clauses
Regardless of these changes to Coco/R, you can use the CoCo/R toolkit in the familiar way with the advantage of "Peek" functionality in the scanner and availabiliy of named handles for the tokens.
Because one often needs to refer to the tokens when trying to resolve an LL(1)
conflict, we introduced a new keyword (
From the above definition Coco/R generates the following class
Conflict Resolution and "Peek" functionality
To be able to distinguish between two alternatives which are not distinguishable
by only a single lookahead token, we have added "peeking" functionality to
the scanner which is exposed by the two static functions of the generated
Let's do a simple example:
This LL(1) conflict (marked red) could be resolved by simply transforming the grammar to:
But the grammar may loose some semantic information and/or clarity. So you can now solve the problem by taking advantage of the new conflict resolution capability of Coco/R (marked red):
When the parser reaches the point where it has to choose between
The resolution function
Let's do a more complex example:
This LL(1) conflict (marked red) could be resolved by transforming the grammar to:
But again the grammar may loose semantic information and/or clarity and thus want to solve the problem with conflict resolvers in the same way as above.
NOTE: In the previous example a fixed lookahead of k > 1 would do the trick, i.e. the grammar is LL(2). Here this is not possible any more, i.e. the grammar is not LL(k) for any fixed k.
This time the resolution function
This example shows how to easily achieve an arbitrary lookahead where necessary.
And another one:
In this case the LL(1) conflict (marked red, above) cannot be resolved by rewriting the grammar, so we have to use our conflict resolvers (marked red, below):
... and the resolution function looks like this:
Play with the examples
In order to give you a better feel for the whole thing, we put the above three examples
in one attributed grammar (
To test the examples, create a text file and write ONE valid (or invalid, if you want to
check out error handling) sentence into it, e.g.
If you want to experiment with the grammar and regenerate the parser, you need to get
Coco executable and the frame files (
Using the C# grammar
In order to use CoCo/R with these extensions to parse C# source code, we had to instrument the C# grammar in the way demonstrated above.
You can now instrument the grammar as you wish, i.e. add attributes to non-terminals and semantic actions to the productions. But you should not change existing parts of the grammar unless you know exactly what are you doing, because otherwise you might break the parser!
Here is an example:
... C O M I N G S O O N ...
DownloadPlease go to our Rotor Community Project Site for up-to-date downloads (File Sharing).