Programmatic Building of Models Just for Pretty Printing DSM 2006 22 October 2006 Tero Hasu Helsinki Institute for Information Technology.

Slides:



Advertisements
Similar presentations
XML III. Learning Objectives Formatting XML Documents: Overview Using Cascading Style Sheets to format XML documents Using XSL to format XML documents.
Advertisements

Comp 205: Comparative Programming Languages Semantics of Imperative Programming Languages denotational semantics operational semantics logical semantics.
Foundations of Programming Languages: Introduction to Lambda Calculus
INTERPRETER Main Topics What is an Interpreter. Why should we learn about them.
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.
Reasons to study concepts of PL
Visitor Pattern Jeff Schott CS590L Spring What is the Purpose of the Visitor Pattern ? n Represent an operation to be performed on the elements.
Whole Platform Tesi di Dottorato di: RICCARDO SOLMI Università degli Studi di Bologna Facoltà di scienze matematiche, fisiche e naturali Corso di Dottorato.
Chapter 2 A Simple Compiler
Building An Interpreter After having done all of the analysis, it’s possible to run the program directly rather than compile it … and it may be worth it.
Interpreter By: Mahmoodreza Jahanseir Amirkabir University of Technology Computer Engineering Department Fall 2010.
Information Extraction from Documents for Automating Softwre Testing by Patricia Lutsky Presented by Ramiro Lopez.
CPSC Compiler Tutorial 3 Parser. Parsing The syntax of most programming languages can be specified by a Context-free Grammar (CGF) Parsing: Given.
Generative Programming. Generic vs Generative Generic Programming focuses on representing families of domain concepts Generic Programming focuses on representing.
CSE 413 Programming Languages & Implementation Hal Perkins Autumn 2012 Context-Free Grammars and Parsing 1.
CSCI-383 Object-Oriented Programming & Design Lecture 15.
Type-Directed, Whitespace-Delimited Parsing for Embedded DSLs Cyrus Omar School of Computer Science Carnegie Mellon University [GlobalDSL13] Benjamin ChungAlex.
8/19/2015© Hal Perkins & UW CSEC-1 CSE P 501 – Compilers Parsing & Context-Free Grammars Hal Perkins Winter 2008.
JS Arrays, Functions, Events Week 5 INFM 603. Agenda Arrays Functions Event-Driven Programming.
Groovy WHAT IS IT? HOW DOES IT WORK? IS IT USEFUL?
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
Syntax Directed Translation. Syntax directed translation Yacc can do a simple kind of syntax directed translation from an input sentence to C code We.
1 Abstract Syntax Tree--motivation The parse tree –contains too much detail e.g. unnecessary terminals such as parentheses –depends heavily on the structure.
ICS611 Introduction to Compilers Set 1. What is a Compiler? A compiler is software (a program) that translates a high-level programming language to machine.
(1.1) COEN 171 Programming Languages Winter 2000 Ron Danielson.
Compiler1 Chapter V: Compiler Overview: r To study the design and operation of compiler for high-level programming languages. r Contents m Basic compiler.
{ Graphite Grigory Arashkovich, Anuj Khanna, Anirban Gangopadhyay, Michael D’Egidio, Laura Willson.
4 Dec 2001Kestrel1 From Patterns to Programming Languages Matthias Felleisen Northeastern University.
SDPL 2001Notes 4: Intro to Stylesheets1 4. Introduction to Stylesheets n Discussed recently: –Programmatic manipulation of (data-oriented) documents n.
Copyright © 2007 Addison-Wesley. All rights reserved.1-1 Reasons for Studying Concepts of Programming Languages Increased ability to express ideas Improved.
A Little Language for Surveys: Constructing an Internal DSL in Ruby H. Conrad Cunningham Computer and Information Science University of Mississippi.
Generative Programming. Automated Assembly Lines.
410/510 1 of 18 Week 5 – Lecture 1 Semantic Analysis Compiler Construction.
© 2004 MetaCase 1 Collected Experiences of Defining Domain-Specific Modeling Languages 10/24/2004 Steven Kelly MetaCase.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns IX Interpreter, Mediator, Template Method recap.
Stéphane Ducasse 1 Visitor.
1 Languages and Compilers (SProg og Oversættere) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm Hutchinson.
Towards the better software metrics tool motivation and the first experiences Gordana Rakić Zoran Budimac.
1 November 19, November 19, 2015November 19, 2015November 19, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 3: Introduction to Syntactic Analysis.
The Evolution of the StringTemplate Engine Presented to the UC Berkeley Harmonia group 15 December 2004 Terence Parr University of San Francisco
Introduction to Markup Languages January 31, 2002.
SDPL 2002Notes 4: Intro to Style Sheets1 4. Introduction to Style Sheets n Discussed recently: –Programmatic manipulation of documents n Now a more human-oriented.
ModTransf A Simple Model to Model Transformation Engine Cédric Dumoulin.
The Visitor Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
The Interpreter Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
ELEE 4303 Digital II Introduction to Verilog. ELEE 4303 Digital II Learning Objectives Get familiar with background of HDLs Basic concepts of Verilog.
CSE 425: Syntax I Syntax and Semantics Syntax gives the structure of statements in a language –Allowed ordering, nesting, repetition, omission of symbols.
Chapter 16 UML Class Diagrams.
Templates of slides for P2 1. A very brief refresher of your problem Describe in English -what artifacts (programs, etc) you wish to synthesize, -from.
1 COSC generating functions, templates, and macros Yves Lespérance Adapted from Peter Roosen-Runge.
CSSE501 Object-Oriented Development. Chapter 10: Subclasses and Subtypes  In this chapter we will explore the relationships between the two concepts.
Overview of Previous Lesson(s) Over View 3 Model of a Compiler Front End.
Interpreter By: Mahmoodreza Jahanseir Amirkabir University of Technology Computer Engineering Department Fall 2010.
Chap. 7, Syntax-Directed Compilation J. H. Wang Nov. 24, 2015.
2000 Research Overview Dr. Kim Mens Programming Technology Lab Vrije Universiteit Brussel.
CSCI 383 Object-Oriented Programming & Design Lecture 15 Martin van Bommel.
Open Source Compiler Construction (for the JVM)
A Simple Syntax-Directed Translator
Parsing & Context-Free Grammars
Presentation by Julie Betlach 7/02/2009
(Slides copied liberally from Ruth Anderson, Hal Perkins and others)
Interpreter Pattern.
Parsing & Context-Free Grammars Hal Perkins Summer 2004
COP4020 Programming Languages
COP4020 Programming Languages
Parsing & Context-Free Grammars Hal Perkins Autumn 2005
Presentation transcript:

Programmatic Building of Models Just for Pretty Printing DSM October 2006 Tero Hasu Helsinki Institute for Information Technology

Outline How to generate nicely laid out source code easily? Constructive pretty printing. qretty – a tool to support constructive pretty printing.

Constructive Pretty Printing Programmatically construct a model defining what is to be printed – then do a pretty-printing traversal over it. –Models constructed imperatively from objects. –Models concern the domain of prettily printed programs – no extraneous information required. (Yes, one might want programmatic model building in other domains, too.)

Some Approaches to Pretty Printing Source Code Have an abstract syntax tree (AST)? –Traverse that. Don't have one? –Use a template engine. –Or build an AST-like model just so you can traverse it?

Why Build Models Just for Pretty Printing? To separate the specification of the content from the formatting rules. –when specifying what to print, one can focus on actual content, without needing to worry about indentation, line breaking, delimiters –one gets all formatting rules in one place (in the printing routine), not spread around in templates

Approaches to Constructing Models Declaratively: Just list model contents in some syntax, with variable value expressions accounting for variability. – model = parse(“... gc.DrawRect( );...”) – (setq! model (... (call-meth (ident “gc”) “DrawRect” args)...)) Imperatively: Write code that builds a model incrementally by adding objects to a tree structure. – meth.Statements.Add(new CodeMethodInvokeExpression(new CodeVariableReferenceExpression(“gc”), “DrawRect”, args));

Possible Reasons for Building Models Imperatively Many currently popular languages lack sufficiently nice list syntax, but have concise syntax for manipulating objects. Named object properties and their accessors make it easy to manipulate models, allowing one to –build a model in any convenient order –build a model incrementally, in multiple passes [:person, [:name, "Jim"], [:phone, " "]] p = Person.new p.name = "Jim" p.phone = " "

What Information to Include in a Model? Less information to include generally means less work in building a model – best to have as little as possible Must know enough about an object to print it correctly......but not necessarily full target language semantics (If an open-ended set of target languages must be supported, full language semantics required.) Any two objects that can be printed in the same way can be added to the model in the same way; no need to specify that different treatment is required (say by using a different setter method)

What Kind of API to Use for Model Building? General-purpose vs. domain- specific API? –General purpose APIs are tedious to use –Specializing the API by hand for a domain is one solution (e.g. Ruby's Builder for Markup for generating XML) –Better way: Generate the API for each domain from a grammar xm.div { xm.br }

Introducing: qretty A Ruby library Supports constructive pretty printing by automating some of the implementation –takes an annotated grammar as input –produces a grammar-specific class hierarchy (for model building) and associated pretty-printing routines

qretty Grammar Specifications Must explicitly specify which non-terminals require a class Hints influencing API generation can be included Hints influencing pretty printing can be included

Example Grammar Specification class ExampleGrammar include Qretty::Dsl def initialize crule(:program, :exprs) arule(:exprs, opt(seplist(:expr, nl))) # all exprs go to same property to preserve order mfield(:expr, :pname => arule(:expr, choice(:call_expr, :if_expr)) crule(:call_expr, seq(ident(:func), "()")) # CallExpr ctor's first argument is function name cfield(:func) crule(:if_expr, seq("if ", :cond, " then", nl, indent(:exprs), nl, "end")) # condition is an instance of CallExpr mfield(:cond, :cname => :call_expr) finalize_grammar end end

Model Building Example model = ast::Program.new model.call_expr "OpenWindow" model.call_expr "DisplayGreeting" if0 = model.if_expr do |if0| if0.cond "IsNewUser" if0.call_expr "DisplayHelp" end model.call_expr "MakeVisible" unless $no_database if0.call_expr "CreateAccount" end OpenWindow() DisplayGreeting() if IsNewUser() then DisplayHelp() CreateAccount() end MakeVisible()

qretty Evaluation Implementation works, but could use redesign and more speed Large existing grammars hard to adapt (e.g. C++) –hard to come up with memorable naming –a lot of work to make class hierarchy shallow enough –grammar specification language imperfections Suggested feature: allow more declarative model building where convenient – program(call_expr(“OpenWindow”), call_expr(“DisplayGreeting”),...)

Summary One approach to pretty printing in program generation: Construct a model, then print it Benefits include: separation of concerns (what and how to print); model build order may differ from printing order qretty helps by generating a model building API and a pretty printer based on an annotated grammar Usable model building API is key, but API design is not easy – even with qretty