Coco/R - Compiler Generator

Download Report

Transcript Coco/R - Compiler Generator

Hanspeter Mössenböck
Albrecht Wöß
Markus Löberbauer
Johannes Kepler University Linz, Austria
SystemSoftWare Group
Coco/R
Compiler Compiler for C#, …
What 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/Research/Projects/Coco/
• C/C++, Modula-2, Pascal: cs.ru.ac.za/homes/cspt/cocor.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)
July 25, 2002
Coco/R - Compiler Generator
2
The Architecture
July 25, 2002
Coco/R - Compiler Generator
3
An 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 ")" ).
July 25, 2002
Coco/R - Compiler Generator
4
Expr
= Term
{ ( "+"
| "-"
)
Term
}.
Term
= Factor
{ ( "*"
| "/"
)
Factor
}.
Factor
= [ "-"
]
( number
| "(" Expr
")"
).
add attributes
Expression = Expr<out value>.
Expr<out int val>
= Term<out val>
{ ( "+"
| "-"
)
Term<out val1>
}.
Term<out int val>
= Factor<out val>
{ ( "*"
| "/"
)
Factor<out val1>
}.
Factor<out int val>
= [ "-"
]
( number
| "(" Expr <out val1>
")"
).
add semantic actions
Expression = Expr<out value>.
Expr<out int val>
(.int val1, sign;.)
= Term<out val>
(.sign = 1;.)
{ ( "+"
(.sign = -1;.)
| "-"
)
Term<out val1>
(.val += val1 * sign;.)
}.
Term<out int val>
(.int val1; bool mul;.)
= Factor<out val>
{ ( "*"
(.mul = true;.)
| "/"
(.mul = false;.)
)
(.val = (mul) ? val*val1 : val/val1;.)
Factor<out val1>
}.
Factor<out int val>
(.int val1; val = 1;.)
= [ "-"
(.val = -1;.)
]
(.val *= Convert.ToInt32(token.val);.)
( number
| "(" Expr<out val1>
")"
(.val *= val1;.)
).
Expr<out int val>
= Term<out val>
{ ( "+"
| "-"
)
Term<out val1>
}.
(.int val1, sign;.)
(.sign = 1;.)
(.sign = -1;.)
(.val += val1 * sign;.)
in file " Parser.cs"
static void Expr (out int val) {
int val1, sign;
–
+
Term(out val);
while (t.kind==2 || t.kind==3) {
if (t.kind==2) {
Get(); sign = 1;
} else {
Get(); sign = -1;
}
Term(out val1);
val += val1 * sign;
}
}
Structure of Cocol
using System;
COMPILER Expression;
public static int value;
CHARACTERS digit = "0123456789".
TOKENS number = digit { digit }.
PRODUCTIONS
Expression = Expr<out value>.
Expr<out int val>
= Term<out val>
{ ( "+"
| "-"
) Term<out val1>
}.
Term<out int val>
= Factor<out val>
{ ( "*"
| "/"
) Factor<out val1>
}.
Factor<out int val>
= [ "-"
] ( number
| "(" Expr<out val1> ")"
).
arbitrary C# declarations
scanner specification
(.int val1, sign;.)
(.sign = 1;.)
(.sign = -1;.)
(.val += val1 * sign;.)
(.int val1; bool mul;.)
(.mul = true;.)
(.mul = false;.)
(.val = (mul) ? val*val1 : val/val1;.)
(.int
(.val
(.val
(.val
parser
specification
val1; val = 1;.)
= -1;.)
*= Convert.ToInt32(token.val);.)
*= val1;.)
END Expression.
July 25, 2002
Coco/R - Compiler Generator
9
Scanner-Specification
CHARACTERS
letter = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".
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.val[1]) { … }.)
COMMENTS FROM "/*" TO "*/" NESTED
IGNORE tab + CHR(10) + CHR(13).
July 25, 2002
Coco/R - Compiler Generator
10
Compiler Main
• Compiler.cs:
using System;
namespace Expression {
public class Compiler {
public static void Main (string[] args) {
if (args.Length > 0) {
Scanner.Init(args[0]);
Parser.Parse();
if (Errors.count == 0)
Console.WriteLine(Parser.value);
} else Console.WriteLine("No source file specified");
}
}
}
July 25, 2002
Coco/R - Compiler Generator
11
DEMO
Let it run …
The 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#
July 25, 2002
Coco/R - Compiler Generator
13
Applications
• 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
July 25, 2002
Coco/R - Compiler Generator
14