Title: CocoR Compiler Compiler for C
1Coco/RCompiler Compiler for C,
- Hanspeter Mössenböck
- Albrecht Wöß
- Markus Löberbauer
- Johannes Kepler University Linz, Austria
- SystemSoftWare Group
2What is Coco/R?
- easy-to-use compiler generator
- Input attributed, context-free EBNF grammar
(LL(1)) - Output scanner recursive descent parser
- available versions
- C, Java, Oberon-2 www.ssw.uni-linz.ac.at/Researc
h/Projects/Coco/ - C/C, Modula-2, Pascal cs.ru.ac.za/homes/cspt/co
cor.htm - Delphi parserbuilder.sourgeforge.net,
www.tetzel.com/CocoR - publications
- Paper and Report www.ssw.uni-linz.ac.at/Research/
Projects/Coco/ - Pat Terry, Compilers Compiler Generators - An
Introduction with C, Thomson Computer Press,
1997(www.scifac.ru.ac.za/compilers)
3The Architecture
4An Example
- Simple Integer Expression Evaluator
- Task
- read an expression from a text file
- evaluate it
- write result to console
- Grammar in EBNF
Expr Term ("""-") Term . Term Factor
("""/") Factor . Factor "-" ( number "("
Expr ")" ).
5Expr Term ( "" "-" ) Term
.
Term Factor ( "" "/" )
Factor .
Factor "-" ( number "(" Expr
")" ).
6Expression Expr.
Expr Term ( "" "-" ) Term
.
Term Factor ( "" "/" )
Factor .
Factor "-" ( number "(" Expr
")" ).
7Expression Expr.
Expr Term ( ""
"-" ) Term .
(.int val1, sign.)
(.sign 1.)
(.sign -1.)
(.val val1 sign.)
Term Factor ( ""
"/" ) Factor .
(.int val1 bool mul.)
(.mul true.)
(.mul false.)
(.val (mul) ? valval1 val/val1.)
Factor "-" ( number
"(" Expr ")" ).
(.int val1 val 1.)
(.val -1.)
(.val Convert.ToInt32(token.val).)
(.val val1.)
8Expr (.int val1, sign.) Termval ( "" (.sign 1.) "-" (.sign
-1.) ) Term (.val val1
sign.) .
in file " Parser.cs"
static void Expr (out int val) int val1,
sign Term(out val) while (t.kind2
t.kind3) if (t.kind2)
Get() sign 1 else
Get() sign -1 Term(out
val1) val val1 sign
9Structure of Cocol
using SystemCOMPILER Expression public
static int valueCHARACTERS digit
"0123456789".TOKENS number digit digit
.PRODUCTIONSExpression Exprvalue.Expr (.int val1, sign.)
Term ( "" (.sign 1.)
"-" (.sign -1.) ) Termval1 (.val val1 sign.) .Termval (.int val1 bool mul.) Factor
( "" (.mul true.) "/" (.mul
false.) ) Factor (.val (mul)
? valval1 val/val1.) .Factorval (.int val1 val 1.) "-" (.val
-1.) ( number (.val Convert.ToInt32(tok
en.val).) "(" Expr ")" (.val
val1.) ).END Expression.
arbitrary C declarations
scanner specification
parser specification
10Scanner-Specification
CHARACTERS letter "ABCDEFGHIJKLMNOPQRSTUVWXYZa
bcdefghijklmnopqrstuvwxyz". notZeroDigit
"123456789". digit notZeroDigit "0".
hexDigit digit "ABCDEFabcdef". noDigit
ANY - digit. tab CHR(9). TOKENS ident
letter letter digit "_" . intNumber
notZeroDigit digit . hexNumber "0x"
hexDigit hexDigit . PRAGMAS option ""
letter. (.switch (t.val1) .) COMMENTS
FROM "/" TO "/" NESTED IGNORE tab CHR(10)
CHR(13).
11Compiler Main
using Systemnamespace Expression public
class Compiler public static void Main
(string args) if (args.Length 0)
Scanner.Init(args0)
Parser.Parse() if (Errors.count 0)
Console.WriteLine(Parser.value)
else Console.WriteLine("No source file
specified")
12DEMO
13The Project
- Extension of Coco/R
- use semantic information to resolve
LL(1)-conflicts - use Coco/R for non-LL(1)-grammars
- ATG template for C
- provides complete parsing facilities for C
programs - just add attributes and semantic actions to
create customized applications
Compiler Generation Tools for C
14Applications
- Instrumenting C source code
- insert, delete or rewrite code fragments
- e.g. add profiling, testing or debugging output,
- Analyzing C source code
- extract arbitrary information
- e.g. complexity measures, style measures, call
graphs, - Translating C source code
- convert into arbitrary output format
- e.g. Java-Bytecode, syntax-highlighted HTML,
MORE IDEAS WELCOME !
visit dotnet.jku.at/Projects/Rotor