CocoR Compiler Compiler for C - PowerPoint PPT Presentation

About This Presentation
Title:

CocoR Compiler Compiler for C

Description:

C#, Java, Oberon-2: www.ssw.uni-linz.ac.at/Research/Projects/Coco ... Extension of Coco/R. use semantic information to resolve LL(1)-conflicts ... – PowerPoint PPT presentation

Number of Views:420
Avg rating:3.0/5.0
Slides: 15
Provided by: albrech
Category:
Tags: cocor | coco | compiler

less

Transcript and Presenter's Notes

Title: CocoR Compiler Compiler for C


1
Coco/RCompiler Compiler for C,
  • Hanspeter Mössenböck
  • Albrecht Wöß
  • Markus Löberbauer
  • Johannes Kepler University Linz, Austria
  • SystemSoftWare Group

2
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/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)

3
The Architecture
4
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 ")" ).
5
Expr Term ( "" "-" ) Term
.
Term Factor ( "" "/" )
Factor .
Factor "-" ( number "(" Expr
")" ).
6
Expression Expr.
Expr Term ( "" "-" ) Term
.


Term Factor ( "" "/" )
Factor .


Factor "-" ( number "(" Expr
")" ).


7
Expression 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.)
8
Expr (.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


9
Structure 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
10
Scanner-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).
11
Compiler Main
  • Compiler.cs

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")
12
DEMO
  • Let it run

13
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
14
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
Write a Comment
User Comments (0)
About PowerShow.com