Compiler Generator Coco/R


Hanspeter Mössenböck, Johannes Kepler University 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 http://ssw.jku.at/coco/

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<out x> "=" Number<out y>  (. CodeGen.Assign(x, y); .)
  .
//------------------------------------------------------------------------------------
  Ident <out string x>
  = ident                           (. x = t.val; .)
  .
//------------------------------------------------------------------------------------
  Number <out int n>
  = number                          (. n = Convert.ToInt32(t.val); .)
  .
END Demo.