How to use CoCo/R and the attributed C# grammarNOTE: This documentation refers to the early (alpha) version of Coco/R with LL(1) conflict resolution capabilies. It is fundamentally different from the current version which we describe in our presentation at the 2nd Rotor Workshop and which can be downloaded from the Rotor Community Project Site.
Only the current version will be updated from now on!
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 artificial tokens to get an LL(1)-EBNF-C# grammar. The artificial tokens are generated on the fly by peek functions which look ahead in the token stream until they can decide which alternative matches the actual tokens.
Regardless of these changes to Coco/R, you can use the CoCo/R toolkit in the familiar way with the advantage of "Peek" functionality and availabiliy of handles for the tokens.
Because we need to refer to the tokens in the peek functions, we altered
Tokens, which do not have a name in the ATG, like "+", "-", "++", etc., get a suggestive name like "_Plus", "_Minus", "_PlusPlus", etc.
To distinguish between two alternatives which are not distinguishable by the single look ahead token alone, we have added a "peek mode" exposed by the two functions:
Let's do a simple example:
This LL(1) conflict could be simply solved by writing:
But the grammar may loose some semantic information and clarity. So you can now solve the problem also by taking advantage of the peek facility in a semantic action:
NOTE: We aligned the semantic actions farther right to separate it from the syntax and thus (hopefully) increase the readability of the grammars.
The peek function
With the two artificial tokens:
The artificial tokens start with "²" (because this character does not appear in the given grammar) followed by the token name in order to guarantee the uniqueness of every token.
Let's do a more interresting example:
The LL(1) conflict in this case cannot be solved by rewriting the grammar, so we have to use our peek functionality:
In this case the peek function could be implemented like:
With an artificial token definded as above:
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. We inserted a
You can now instrument the grammar as you wish, but you should not touch the semantic actions which we inserted unless you know exactly what are you doing.
What you need too use CoCo/R on C# source files
In order to create an application, add your own semantic actions to this
grammar and then have Coco/R generate the scanner and parser for you.
Add the main application code to another C# file (e.g. MyApp.cs) that
uses the Scanner and Parser.
What you may be intrested in
Note: These files of the Coco/R compiler generator include the changes for this project and therefore differ from the official version.