Compiler Generator Coco/R


Hanspeter Mössenböck, University of Linz
Coco/R takes a compiler description in the form of an attributed grammar (EBNF syntax with attributes and semantic actions) and translates it into a scanner and a recursive descent parser. The user has to add modules for symbol table handling, optimization, and code generation in order to get a running compiler. LL(1) conflicts can be resolved by a special lookahead mechanism. Coco/R has been used successfully in academia and industry. It combines the functionality of the well-known Unix tools Lex and Yacc.
Coco/R is available under GNU GPL from the following sites:
- Java version
- C# version
- Oberon version
- C, Pascal, Modula-2 versions

Here is an example of a small compiler description with Coco/R:

COMPILER Demo

CHARACTERS
  letter = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrtsuvwxyz".
  digit = "0123456789".
  EOL = '\t'.

TOKENS
  ident = letter {letter | digit}.
  number = digit {digit}.

COMMENTS FROM "/*" TO "*/" NESTED
IGNORE  EOL

PRODUCTIONS
  Demo = Statement {";" Statement}.
(*------------------------------------------------------------------------------------*)
  Statement                         (. string x; int y; .)
  = Ident "=" Number  (. CodeGen.Assign(x, y); .)
  .
(*------------------------------------------------------------------------------------*)
  Ident 
  = ident                           (. x = t.val; .)
  .
(*------------------------------------------------------------------------------------*)
  Number 
  = number                          (. n = Convert.ToInt32(t.val); .)
  .
END Demo.