Download presentation
Presentation is loading. Please wait.
1
Coco/R Compiler Compiler for C#, …
Hanspeter Mössenböck Albrecht Wöß Markus Löberbauer Johannes Kepler University Linz, Austria SystemSoftWare Group Coco/R Compiler Compiler for C#, …
2
Coco/R - Compiler Generator
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: C/C++, Modula-2, Pascal: cs.ru.ac.za/homes/cspt/cocor.htm Delphi: parserbuilder.sourgeforge.net, publications: Paper and Report: Pat Terry, Compilers & Compiler Generators - An Introduction with C++, Thomson Computer Press, 1997 ( July 25, 2002 Coco/R - Compiler Generator
3
Coco/R - Compiler Generator
The Architecture July 25, 2002 Coco/R - Compiler Generator
4
Coco/R - Compiler Generator
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
5
add attributes Expr = Term { ( "+" | "-" ) Term }.
Term = Factor { ( "*" | "/" ) Factor }. add attributes Factor = [ "-" ] ( number | "(" Expr ")" ).
6
add semantic actions Expression = Expr<out value>.
<out int val> Expr = Term { ( "+" | "-" ) Term }. <out val> <out val1> <out int val> Term = Factor { ( "*" | "/" ) Factor }. <out val> <out val1> add semantic actions Factor = [ "-" ] ( number | "(" Expr ")" ). <out int val> <out val1>
7
Expression = Expr<out value>.
Expr<out int val> = Term<out val> { ( "+" | "-" ) Term<out val1> }. (.int val1, sign;.) (.sign = 1;.) (.sign = -1;.) (.val += val1 * sign;.) Term<out int val> = Factor<out val> { ( "*" | "/" ) Factor<out val1> }. (.int val1; bool mul;.) (.mul = true;.) (.mul = false;.) (.val = (mul) ? val*val1 : val/val1;.) Factor<out int val> = [ "-" ] ( number | "(" Expr<out val1> ")" ). (.int val1; val = 1;.) (.val = -1;.) (.val *= Convert.ToInt32(token.val);.) (.val *= val1;.)
8
Expr<out int val>. (. int val1, sign;
Expr<out int val> (.int val1, sign;.) = Term<out val> { ( "+" (.sign = 1;.) | "-" (.sign = -1;.) ) Term<out val1> (.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; } } + –
9
Coco/R - Compiler Generator
Structure of Cocol using System; COMPILER Expression; public static int value; CHARACTERS digit = " ". TOKENS number = digit { digit }. PRODUCTIONS 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;.) ) Factor<out val1> (.val = (mul) ? val*val1 : val/val1;.) }. Factor<out int val> (.int val1; val = 1;.) = [ "-" (.val = -1;.) ] ( number (.val *= Convert.ToInt32(token.val);.) | "(" Expr<out val1> ")" (.val *= val1;.) ). END Expression. arbitrary C# declarations scanner specification parser specification July 25, 2002 Coco/R - Compiler Generator
10
Scanner-Specification
CHARACTERS letter = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz". notZeroDigit = " ". 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
11
Coco/R - Compiler Generator
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
12
DEMO Let it run …
13
Coco/R - Compiler Generator
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
14
Coco/R - Compiler Generator
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.