Maya: Multiple-Dispatch Syntax Extension in Java Jason Baker and Wilson C. Hsieh University of Utah.

Slides:



Advertisements
Similar presentations
Vilmos Zsombori , Shanghai
Advertisements

CPSC 388 – Compiler Design and Construction
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
1 Mooly Sagiv and Greta Yorsh School of Computer Science Tel-Aviv University Modern Compiler Design.
Translator Architecture Code Generator ParserTokenizer string of characters (source code) string of tokens abstract program string of integers (object.
ISBN Chapter 10 Implementing Subprograms.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Defining practical programming languages Carlos Varela RPI.
9/27/2006Prof. Hilfinger, Lecture 141 Syntax-Directed Translation Lecture 14 (adapted from slides by R. Bodik)
WPSM Programming Language A simple language that transform simple data structure into complex xML format Wai Y. Wong Peter Chen Seema Gupta Miqdad Mohammed.
1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice.
CS 330 Programming Languages 09 / 13 / 2007 Instructor: Michael Eckmann.
Chapter3: Language Translation issues
Runtime Aspect Weaving Through Metaprogramming Jason Baker and Wilson Hsieh Handi-Wrap.
Slide 1 Chapter 2-b Syntax, Semantics. Slide 2 Syntax, Semantics - Definition The syntax of a programming language is the form of its expressions, statements.
) y + 1 * => square ( January 31, 2005MacrosClaus Brabrand y + 1 Widespread … ……… … ……... Dylan Dylan Macro Mechamism M4 Unix Macro Preprocessor L A T.
1 Terminology l Statement ( 敘述 ) »declaration, assignment containing expression ( 運算式 ) l Grammar ( 文法 ) »a set of rules specify the form of legal statements.
Chapter 3 Program translation1 Chapt. 3 Language Translation Syntax and Semantics Translation phases Formal translation models.
Parsing — Part II (Ambiguity, Top-down parsing, Left-recursion Removal)
Compiler Summary Mooly Sagiv html://
The Structure of the GNAT Compiler. A target-independent Ada95 front-end for GCC Ada components C components SyntaxSemExpandgigiGCC AST Annotated AST.
Maya: Multiple-Dispatch Syntax Extension in Java Jason Baker and Wilson C. Hsieh University of Utah.
COP4020 Programming Languages
(2.1) Grammars  Definitions  Grammars  Backus-Naur Form  Derivation – terminology – trees  Grammars and ambiguity  Simple example  Grammar hierarchies.
CSC 8310 Programming Languages Meeting 2 September 2/3, 2014.
Presented By: Shriraksha Mohan Nathaniel Nystrom, Michael R. Clarkson, and Andrew C. Myers.
Syntax Directed Definitions Synthesized Attributes
COP4020 Programming Languages
©J.Tiberghien - ULB-VUB Version Troisième Partie Chapitre 1 Les supports à la programmation.
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 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
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.
Chapter 6 Programming Languages (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
Joey Paquet, Lecture 12 Review. Joey Paquet, Course Review Compiler architecture –Lexical analysis, syntactic analysis, semantic.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 8: Semantic Analysis and Symbol Tables.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
Review 1.Lexical Analysis 2.Syntax Analysis 3.Semantic Analysis 4.Code Generation 5.Code Optimization.
Formal Semantics Chapter Twenty-ThreeModern Programming Languages, 2nd ed.1.
1 SystemVerilog Enhancement Requests Daniel Schostak Principal Engineer February 26 th 2010.
Chapter 2. Design of a Simple Compiler J. H. Wang Sep. 21, 2015.
CPS 506 Comparative Programming Languages Syntax Specification.
11/25/2015© Hal Perkins & UW CSEH-1 CSE P 501 – Compilers Implementing ASTs (in Java) Hal Perkins Winter 2008.
D Goforth COSC Translating High Level Languages Note error in assignment 1: #4 - refer to Example grammar 3.4, p. 126.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 3: Introduction to Syntactic Analysis.
What am I? while b != 0 if a > b a := a − b else b := b − a return a AST == Abstract Syntax Tree.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
Syntax and Semantics Form and Meaning of Programming Languages Copyright © by Curt Hill.
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
 Fall Chart 2  Translators and Compilers  Textbook o Programming Language Processors in Java, Authors: David A. Watts & Deryck F. Brown, 2000,
Semantic Analysis II Type Checking EECS 483 – Lecture 12 University of Michigan Wednesday, October 18, 2006.
Programming Languages and Design Lecture 2 Syntax Specifications of Programming Languages Instructor: Li Ma Department of Computer Science Texas Southern.
C H A P T E R T W O Linking Syntax And Semantics Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
Chap. 7, Syntax-Directed Compilation J. H. Wang Nov. 24, 2015.
BY: JAKE TENBERG & CHELSEA SHIPP PROJECT REVIEW: JGIBBERISH.
CS412/413 Introduction to Compilers Radu Rugina Lecture 11: Symbol Tables 13 Feb 02.
Syntax-Directed Definitions CS375 Compilers. UT-CS. 1.
Syntax Analysis Or Parsing. A.K.A. Syntax Analysis –Recognize sentences in a language. –Discover the structure of a document/program. –Construct (implicitly.
Chapter 3 – Describing Syntax CSCE 343. Syntax vs. Semantics Syntax: The form or structure of the expressions, statements, and program units. Semantics:
5th MaCS Debrecen On the Turing-Completeness of Generative Metaprograms Zoltán Porkoláb, István Zólyomi Dept. of Programming.
LECTURE 10 Semantic Analysis. REVIEW So far, we’ve covered the following: Compilation methods: compilation vs. interpretation. The overall compilation.
Lecture 9 Symbol Table and Attributed Grammars
Chapter 3 – Describing Syntax
Describing Syntax and Semantics
A Simple Syntax-Directed Translator
Constructing Precedence Table
Interpreters Study Semantics of Programming Languages through interpreters (Executable Specifications) cs7100(Prasad) L8Interp.
CS 536 / Fall 2017 Introduction to programming languages and compilers
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
CSE401 Introduction to Compiler Construction
High-Level Programming Language
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Presentation transcript:

Maya: Multiple-Dispatch Syntax Extension in Java Jason Baker and Wilson C. Hsieh University of Utah

Example for (Enumeration enum$ = v.elements(); enum$.hasMoreElements(); ) { String st; st = (String) enum$.nextElement(); /* … */ } v.elements().foreach(String st) { /*…*/ } java.util.Vector v; Maya code

v.elements().foreach(String st) { /*…*/ } javamaya.util.Vector v; Specialized Expansion { Vector v$ = v; int len$ = v$.size(); Object[] arr$ = v$.getElementData(); for (int i$ = 0; i$ < len$; i$++) { String st = (String) arr$[i$]; /* … */ }} other Maya code

Maya: Java With Macros Programs that transform syntax Best chosen for each occurrence –based on syntactic structure, –and static type information

Compiled Application Compiled Extensions Application Source Maya’s Extensible Compiler mayac

Compiled Application Extension Library Extension Source Compiled Extensions Application Source Maya’s Extensible Compiler mayac

Extensible Parser Provide a fixed hierarchy of AST node types And a grammar that uses these types as symbols S → S + T{ new S($0, $2) } T → Lit{ $0 }

Adding Productions Allow grammar to be extended New actions defined in terms of existing node types S → S + T{ new S($0, $2) } T → Lit{ $0 } S → inc(S){ new S{ 1 + $2 } }

Overriding Actions Several Mayans defined on a single production Dispatch to best match for nodes on the parse stack S → S + T{ new S($0, $2) } { new S{ $0.concat($2) } } T → Lit{ $0 } S → inc(S){ new S{ 1 + $2 } }

Key Features Hygienically operate on ASTs Generate ASTs through robust and flexible template mechanism Dispatched on rich set of specializers –In particular, static types of expressions

Structuring Extensions Compiler loads Mayans by calling an interface method Mayans, like class declarations, may be local Mayans may be loaded locally to a lexical scope

Parse Order Mayans execute during parsing Mayans are dispatched based on static types of arguments Some arguments can only be checked after expansion Lazy parsing made explicit in grammar v.elements().foreach(String st) { /*…*/ } maya.util.Vector v;

Defining a Production syntax for v.elements().foreach(String st){…} Statement  MethodName ( Formal ) LazyBlock In Maya syntax: abstract Statement syntax(MethodName(Formal) lazy(BraceTree, BlockStmts));

Declaring a Mayan IdentifierExpression. MethodName(Formal)lazy(BraceTree, BlockSmts) Statement Statement syntax EForEach(Expression:Enumeration enumExp. foreach(Formal var) lazy(BraceTree, BlockStmts) body)) { /* Mayan body */ }

Parameter Specializers Runtime types accepted by a Mayan Determine dispatch order Maya support other specializers –substructure –static type of expression –token value –type denoted by name

Parameter Specializers –substructure –static type of expression –token value –type denoted by name Statement syntax EForEach(Expression:Enumeration enumExp. foreach(Formal var) lazy(BraceTree, BlockStmts) body)) { /* Mayan body */ }

Parameter Specializers –substructure –static type of expression –token value –type denoted by name Statement syntax EForEach(Expression:Enumeration enumExp. foreach(Formal var) lazy(BraceTree, BlockStmts) body)) { /* Mayan body */ }

Parameter Specializers –substructure –static type of expression –token value –type denoted by name Statement syntax EForEach(Expression:Enumeration enumExp. foreach(Formal var) lazy(BraceTree, BlockStmts) body)) { /* Mayan body */ }

Parameter Specializers –substructure –static type of expression –token value –type denoted by name Statement syntax EForEach(Expression:Enumeration enumExp. foreach(Formal var) lazy(BraceTree, BlockStmts) body)) { /* Mayan body */ }

Templates and Hygiene Easy way to construct syntax trees: Statically ensure –syntactic correctness –hygiene and referential transparency –references to bound variables new Expression { * 3 } new AddExpr( new IntegerLiteral(1), new MulExpr(/*... */)) vs.

return new Statement { for (Enumeration enum = $_______; enum.hasMoreElements(); ) { $____________________ $_________________________ = ($________) enum.nextElement(); $____ } }; Template Example Expression enumExp; Formal var; DelayedStmt body;

return new Statement { for (Enumeration enum = $enumExp; enum.hasMoreElements(); ) { $____________________ $_________________________ = ($________) enum.nextElement(); $body } }; Unquoting Parameters Expression enumExp; Formal var; DelayedStmt body;

final StrictTypeName castType = StrictTypeName.make(var.getType()); return new Statement { for (Enumeration enum = $enumExp; enum.hasMoreElements(); ) { $(DeclStmt.make(var)) $(Reference.makeExpr(var)) = ($castType) enum.nextElement(); $body } }; Other Expressions Expression enumExp; Formal var; DelayedStmt body;

final StrictTypeName castType = StrictTypeName.make(var.getType()); return new Statement { for (Enumeration enum = $enumExp; enum.hasMoreElements(); ) { $(DeclStmt.make(var)) $(Reference.makeExpr(var)) = ($castType) enum.nextElement(); $body } }; Hygiene Expression enumExp; Formal var; DelayedStmt body;

Pattern Parsing Used when compiling Mayan argument trees and template definitions Patterns contain both terminal and non- terminal symbols Pattern parser generates parse tree with non-terminal leaves Used to statically implement hygiene

MultiJava Features Clifton et al. [OOPSLA 2000] Multiple dispatch –static checks for ambiguous and incomplete generic functions Open classes –external methods can be added to classes Implemented in KJC (20k lines)

MultiJava in Maya 2500 lines of code Maya provides necessary features –static type information –syntax extension –overriding behavior of syntax Maya supports large syntax extensions –local Mayans –lexically scoped Mayan imports

Related Work JSE [Bachrach and Playford 2001] JTS [Batory et al. 1998] –not appropriate for simple macros –doesn’t do type checking –lacks pattern parser OpenJava [Tatsubori et al. 2000] –limited ability to define syntactic forms –single dispatch

Conclusions Maya’s features provide flexibility –static type information –flexible templates and pattern matching –detection of errors in templates Through novel techniques –lazy parsing –pattern parsing