Coco/R Compiler Compiler for C#, …

Slides:



Advertisements
Similar presentations
 2005 Pearson Education, Inc. All rights reserved Introduction.
Advertisements

1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
6-1 6 Syntactic analysis  Aspects of syntactic analysis  Tokens  Lexer  Parser  Applications of syntactic analysis  Compiler generation tool ANTLR.
Compiler Generation Tools for C# Albrecht Wöß Hanspeter Mössenböck Markus Löberbauer Johannes Kepler University Linz, Austria SSW SystemSoftWare Group.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
Some basic I/O.
Context-sensitive Analysis. Beyond Syntax There is a level of correctness that is deeper than grammar fie(a,b,c,d) int a, b, c, d; { … } fee() { int f[3],g[0],
Chapter 2 A Simple Compiler
Basic Elements of C++ Chapter 2.
Grammars and Parsing. Sentence  Noun Verb Noun Noun  boys Noun  girls Noun  dogs Verb  like Verb  see Grammars Grammar: set of rules for generating.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
CSc 453 Semantic Analysis Saumya Debray The University of Arizona Tucson.
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
1 Programming Languages Tevfik Koşar Lecture - II January 19 th, 2006.
AN IMPLEMENTATION OF A REGULAR EXPRESSION PARSER
Lexical Analysis - An Introduction. The Front End The purpose of the front end is to deal with the input language Perform a membership test: code  source.
Lexical Analysis - An Introduction Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
The Java Programming Language
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
CS 461 – Oct. 7 Applications of CFLs: Compiling Scanning vs. parsing Expression grammars –Associativity –Precedence Programming language (handout)
1 Top Down Parsing. CS 412/413 Spring 2008Introduction to Compilers2 Outline Top-down parsing SLL(1) grammars Transforming a grammar into SLL(1) form.
COMP Parsing 2 of 4 Lecture 22. How do we write programs to do this? The process of getting from the input string to the parse tree consists of.
Intro and Review Welcome to Java. Introduction Java application programming Use tools from the JDK to compile and run programs. Videos at
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
PL&C Lab, DongGuk University Compiler Lecture Note, MiscellaneousPage 1 Miscellaneous 컴파일러 입문.
D. M. Akbar Hussain: Department of Software & Media Technology 1 Compiler is tool: which translate notations from one system to another, usually from source.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
BUILD ON THE POLYGLOT COMPILER FRAMEWORK MIHAL BRUMBULLI 7th Workshop “SEERE” Montenegro-Risan 9-14 September 2007 SimJ Programming Language.
Chapter 2. Design of a Simple Compiler J. H. Wang Sep. 21, 2015.
CPS 506 Comparative Programming Languages Syntax Specification.
Topic #1: Introduction EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
D Goforth COSC Translating High Level Languages Note error in assignment 1: #4 - refer to Example grammar 3.4, p. 126.
YACC. Introduction What is YACC ? a tool for automatically generating a parser given a grammar written in a yacc specification (.y file) YACC (Yet Another.
CS 153: Concepts of Compiler Design October 21 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
1 Introduction to Parsing. 2 Outline l Regular languages revisited l Parser overview Context-free grammars (CFG ’ s) l Derivations.
Costas Busch - LSU1 Parsing. Costas Busch - LSU2 Compiler Program File v = 5; if (v>5) x = 12 + v; while (x !=3) { x = x - 3; v = 10; } Add v,v,5.
ICS312 Introduction to Compilers Set 23. What is a Compiler? A compiler is software (a program) that translates a high-level programming language to machine.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
MiniJava Compiler A multi-back-end JIT compiler of Java.
2-1. LEX & YACC. 2 Overview  Syntax  What its program looks like –Context-free grammar, BNF  Syntax-directed translation –A grammar-oriented compiling.
Compiler Principle and Technology Prof. Dongming LU Apr. 15th, 2015.
1 4.Semantic Processing and Attribute Grammars. 2 Semantic Processing The parser checks only the syntactic correctness of a program Tasks of semantic.
Programming Languages Concepts Chapter 1: Programming Languages Concepts Lecture # 4.
1 8.Compiler Generators 8.1Overview 8.2Yacc 8.3Lex 8.4Coco/R.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture Ahmed Ezzat.
Lecture 4 CS140 Dick Steflik. Reading Keyboard Input Import java.util.Scanner – A simple text scanner which can parse primitive types and strings using.
Lecture 9 Symbol Table and Attributed Grammars
System Software Theory (5KS03).
4. Semantic Processing and Attribute Grammars
Chapter 1.2 Introduction to C++ Programming
Parsing 2 of 4: Scanner and Parsing
Chapter Topics The Basics of a C++ Program Data Types
Introduction to programming in java
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
A Simple Syntax-Directed Translator
CS 3304 Comparative Languages
Basic Elements of C++.
Compiler Construction
Basic Elements of C++ Chapter 2.
Compiler Design 22. ANTLR AST Traversal (AST as Input, AST Grammars)
Programming Languages 2nd edition Tucker and Noonan
CSE 3302 Programming Languages
CSE401 Introduction to Compiler Construction
Compiler Lecture Note, Miscellaneous
Course Overview PART I: overview material PART II: inside a compiler
Presentation transcript:

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#, …

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: 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

Coco/R - Compiler Generator The Architecture July 25, 2002 Coco/R - Compiler Generator

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

add attributes Expr = Term { ( "+" | "-" ) Term }. Term = Factor { ( "*" | "/" ) Factor }. add attributes Factor = [ "-" ] ( number | "(" Expr ")" ).

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>

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;.)

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; } } + –

Coco/R - Compiler Generator 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> (.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

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

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

DEMO Let it run …

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

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