McLab Tutorial www.sable.mcgill.ca/mclab Part 3 – McLab Frontend Frontend organization Introduction to Beaver Introduction to JastAdd 6/4/2011 Frontend-1McLab.

Slides:



Advertisements
Similar presentations
CPSC 388 – Compiler Design and Construction
Advertisements

Semantics Static semantics Dynamic semantics attribute grammars
ICE1341 Programming Languages Spring 2005 Lecture #6 Lecture #6 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
1 JavaCUP JavaCUP (Construct Useful Parser) is a parser generator Produce a parser written in java, itself is also written in Java; There are many parser.
McLab Tutorial Part 8 – Wrap Up Summary Ongoing and Future Work Further Sources 6/4/2011Wrap Up- 1McLab Tutorial, Laurie Hendren,
TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: AA A AAAA A A AA A A A AA A AA A A.
McLab Tutorial Part 2 – Introduction to MATLAB Functions and Scripts Data and Variables Other Tricky "Features" 6/4/2011Matlab-
Semantic analysis Parsing only verifies that the program consists of tokens arranged in a syntactically-valid combination, we now move on to semantic analysis,
CPSC Compiler Tutorial 9 Review of Compiler.
9/27/2006Prof. Hilfinger, Lecture 141 Syntax-Directed Translation Lecture 14 (adapted from slides by R. Bodik)
Abstract Syntax Trees Compiler Baojian Hua
SMIILE Finaly COBOL! and what else is new Gordana Rakić, Zoran Budimac.
Chapter 2 A Simple Compiler
CSE 413 Programming Languages & Implementation Hal Perkins Autumn 2012 Context-Free Grammars and Parsing 1.
ANTLR.
ANTLR Andrew Pangborn & Zach Busser. ANTLR in a Nutshell ANother Tool for Language Recognition generates lexers generates parsers (and parse trees)‏ Java-based,
8/19/2015© Hal Perkins & UW CSEC-1 CSE P 501 – Compilers Parsing & Context-Free Grammars Hal Perkins Winter 2008.
Parser construction tools: YACC
McLab Tutorial Laurie Hendren, Rahul Garg and Nurudeen Lameed Other McLab team members: Andrew Casey, Jesse Doherty, Anton Dubrau,
Abstract Syntax Trees Lecture 14 Wed, Mar 3, 2004.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
McLab Tutorial Part 6 – Introduction to the McLab Backends MATLAB-to-MATLAB MATLAB-to-Fortran90 (McFor) McVM with JIT 6/4/2011Backends-
Syntax & Semantic Introduction Organization of Language Description Abstract Syntax Formal Syntax The Way of Writing Grammars Formal Semantic.
CPSC 388 – Compiler Design and Construction Parsers – Context Free Grammars.
McLab Tutorial Part 4 – McLab Intermediate Representations High-level McAST Lower-level McLAST Transforming McAST to McLAST 6/4/2011IR-
Getting Started with ANTLR Chapter 1. Domain Specific Languages DSLs are high-level languages designed for specific tasks DSLs include data formats, configuration.
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.
{ Graphite Grigory Arashkovich, Anuj Khanna, Anirban Gangopadhyay, Michael D’Egidio, Laura Willson.
McLab Tutorial Part 5 – Introduction to the McLab Analysis Framework Exploring the Main Components Creating a Simple Analysis.
Grammar Variation in Compiler Design Carl Wu. Three topics Syntax Grammar vs. AST Component(?)-based grammar Aspect-oriented grammar.
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
Syntax Directed Translation. Tokens Parser Semantic checking TAC Peephole, pipeline, …… TAC  assembly code/mc Cmm subexpression,……
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Chapter 2. Design of a Simple Compiler J. H. Wang Sep. 21, 2015.
Towards the better software metrics tool motivation and the first experiences Gordana Rakić Zoran Budimac.
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 1, 08/28/03 Prof. Roy Levow.
CPS 506 Comparative Programming Languages Syntax Specification.
1. 2 Preface In the time since the 1986 edition of this book, the world of compiler design has changed significantly 3.
Introduction Lecture 1 Wed, Jan 12, The Stages of Compilation Lexical analysis. Syntactic analysis. Semantic analysis. Intermediate code generation.
ISBN Chapter 3 Describing Semantics.
Weaving a Debugging Aspect into Domain-Specific Language Grammars SAC ’05 PSC Track Santa Fe, New Mexico USA March 17, 2005 Hui Wu, Jeff Gray, Marjan Mernik,
. n COMPILERS n n AND n n INTERPRETERS. -Compilers nA compiler is a program thatt reads a program written in one language - the source language- and translates.
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.
Chapter 1 Introduction Major Data Structures in Compiler
LESSON 04.
1 Compiler & its Phases Krishan Kumar Asstt. Prof. (CSE) BPRCE, Gohana.
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-Directed Definitions and Attribute Evaluation Compiler Design Lecture (02/18/98) Computer Science Rensselaer Polytechnic.
C H A P T E R T W O Linking Syntax And Semantics Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
1 Introduction to Parsing. 2 Outline l Regular languages revisited l Parser overview Context-free grammars (CFG ’ s) l Derivations.
Chap. 7, Syntax-Directed Compilation J. H. Wang Nov. 24, 2015.
CSC 4181 Compiler Construction
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.
MiniJava Compiler A multi-back-end JIT compiler of Java.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture Ahmed Ezzat.
LECTURE 10 Semantic Analysis. REVIEW So far, we’ve covered the following: Compilation methods: compilation vs. interpretation. The overall compilation.
Chapter 1 Introduction.
Constructing Precedence Table
CS 3304 Comparative Languages
Parsing & Context-Free Grammars
Introduction to Parsing (adapted from CS 164 at Berkeley)
Abstract Syntax Trees Lecture 14 Mon, Feb 28, 2005.
Chapter 1 Introduction.
PROGRAMMING LANGUAGES
Compiler Lecture 1 CS510.
Syntax Questions 6. Define a left recursive grammar rule.
CSE401 Introduction to Compiler Construction
Language Extensions for MATLAB
Parsing & Context-Free Grammars Hal Perkins Autumn 2005
Presentation transcript:

McLab Tutorial Part 3 – McLab Frontend Frontend organization Introduction to Beaver Introduction to JastAdd 6/4/2011 Frontend-1McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen Lameed TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: AAAAAAAAAAA

McLab Frontend Tools to parse MATLAB-type languages – Quickly experiment with language extensions – Tested on a lot of real-world Matlab code Parser generates ASTs Some tools for computing attributes of ASTs A number of static analyses and utilities – Example: Printing XML representation of AST 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedFrontend-2

Tools used Written in Java (JDK 6) MetaLexer and JFlex for scanner Beaver parser generator JastAdd “compiler-generator” for computations of AST attributes Ant based builds We typically use Eclipse for development – Or Vim 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedFrontend-3

Frontend organization 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedFrontend-4 Scanner (MetaLexer and JFlex) Parser (Beaver) AST attributes, rewrites (JastAdd) Matlab source Attributed AST XML Other

Natlab Natlab is a clean subset of MATLAB – Not a trivial subset though – Covers a lot of “sane” MATLAB code MATLAB to Natlab translation tool available – Written using ANTLR – Outside the scope of this tutorial Forms the basis of much of our semantics and static analysis research 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedFrontend-5

Frontend with MATLAB-to-Natlab 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedFrontend-6 Scanner (MetaLexer and JFlex) Parser (Beaver) AST attributes, rewrites (JastAdd) Matlab source Attributed AST XML Other MATLAB-2-Natlab converter

How is Natlab organized? Scanner specifications – src/metalexer/shared_keywords.mlc Grammar files – src/parser/natlab.parser AST computations based on JastAdd – src/natlab.ast – src/*jadd, src/*jrag Other Java files – src/*java 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedFrontend-7

MetaLexer A system for writing extensible scanner specifications Scanner specifications can be modularized, reused and extended Generates JFlex code – Which then generates Java code for the lexer/scanner Syntax is similar to most other lexers Reference: “MetaLexer: A Modular Lexical Specification Language. Andrew Casey, Laurie Hendren” by Casey, Hendren at AOSD /4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedFrontend-8

Frontend-9

Beaver Beaver is a LALR parser generator Familiar syntax (EBNF based) Allows embedding of Java code for semantic actions Usage in Natlab: Simply generate appropriate AST node as semantic action 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedFrontend-10

Beaver Example Stmt stmt = expr.e {: return new ExprStmt(e); :} |BREAK {: return new BreakStmt(); :} |FOR for_assign.a stmt_seq.s END {: return new ForStmt(a,s); :} 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedFrontend-11

Beaver Example Stmt stmt = expr.e {: return new ExprStmt(e); :} |BREAK {: return new BreakStmt(); :} |FOR for_assign.a stmt_seq.s END {: return new ForStmt(a,s); :} 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedFrontend-12 Java type

Beaver Example Stmt stmt = expr.e {: return new ExprStmt(e); :} |BREAK {: return new BreakStmt(); :} |FOR for_assign.a stmt_seq.s END {: return new ForStmt(a,s); :} 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedFrontend-13 Node name in grammar

Beaver Example Stmt stmt = expr.e {: return new ExprStmt(e); :} |BREAK {: return new BreakStmt(); :} |FOR for_assign.a stmt_seq.s END {: return new ForStmt(a,s); :} 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedFrontend-14 Identifier for node

Beaver Example Stmt stmt = expr.e {: return new ExprStmt(e); :} |BREAK {: return new BreakStmt(); :} |FOR for_assign.a stmt_seq.s END {: return new ForStmt(a,s); :} 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedFrontend-15 Java code for semantic action

JastAdd: Motivation You have an AST Each AST node type represented by a class Want to compute attributes of the AST – Example: String representation of a node Attributes might be either: – Inherited from parents – Synthesized from children 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedFrontend-16

JastAdd JastAdd is a system for specifying: – Each attribute computation specified as an aspect – Attributes can be inherited or synthesized – Can also rewrite trees – Declarative philosophy – Java-like syntax with added keywords Generates Java code Based upon “Reference attribute grammars” 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedFrontend-17

How does everything fit? JastAdd requires two types of files: –.ast file which specifies an AST grammar –.jrag/.jadd files which specify attribute computations For each node type specified in AST grammar: – JastAdd generates a class derived from ASTNode For each aspect: – JastAdd adds a method to the relevant node classes 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedFrontend-18

JastAdd AST File example abstract BinaryExpr: Expr ::= LHS:Expr RHS:Expr PlusExpr: BinaryExpr; MinusExpr: BinaryExpr; MTimesExpr: BinaryExpr; 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedFrontend-19

JastAdd XML generation aspect aspect AST2XML{.. eq BinaryExpr.getXML(Document d, Element e){ Element v = d.getElement(nameOfExpr); getRHS().getXML(d,v); getLHS().getXML(d,v); e.add(v); return true; } … 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedFrontend-20

aspect AST2XML{.. eq BinaryExpr.getXML(Document d, Element e){ Element v = d.getElement(nameOfExpr); getRHS().getXML(d,v); getLHS().getXML(d,v); e.add(v); return true; } … 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedFrontend-21 Aspect declaration

aspect AST2XML{.. eq BinaryExpr.getXML(Document d, Element e){ Element v = d.getElement(nameOfExpr); getRHS().getXML(d,v); getLHS().getXML(d,v); e.add(v); return true; } … 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedFrontend-22 “Equation” for an attribute

aspect AST2XML{.. eq BinaryExpr.getXML(Document d, Element e){ Element v = d.getElement(nameOfExpr); getRHS().getXML(d,v); getLHS().getXML(d,v); e.add(v); return true; } … 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedFrontend-23 Add to this AST class

aspect AST2XML{.. eq BinaryExpr.getXML(Document d, Element e){ Element v = d.getElement(nameOfExpr); getRHS().getXML(d,v); getLHS().getXML(d,v); e.add(v); return true; } … 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedFrontend-24 Method name to be added

aspect AST2XML{.. eq BinaryExpr.getXML(Document d, Element e){ Element v = d.getElement(nameOfExpr); getRHS().getXML(d,v); getLHS().getXML(d,v); e.add(v); return true; } … 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedFrontend-25 Attributes can be parameterized

aspect AST2XML{.. eq BinaryExpr.getXML(Document d, Element e){ Element v = d.getElement(nameOfExpr); getRHS().getXML(d,v); getLHS().getXML(d,v); e.add(v); return true; } … 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedFrontend-26 Compute for children

JastAdd weaving 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedFrontend-27 JastAdd Natlab.ast AST2XML.jrag BinaryExpr.javaPlusExpr.javaMinusExpr.java

Overall picture recap Scanner converts text into a stream of tokens Tokens consumed by Beaver-generated parser Parser constructs an AST AST classes were generated by JastAdd AST classes already contain code for computing attributes as methods Code for computing attributes was weaved into classes by JastAdd from aspect files 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedFrontend-28

Adding a node Let’s assume you want to experiment with a new language construct: Example: parallel-for loop construct – parfor i=1:10 a(i) = f(i) end; How do you extend Natlab to handle this? You can either: – Choose to add to Natlab source itself – (Preferred) Setup a project that inherits code from Natlab source directory 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedFrontend-29

Steps Write the following in your project: – Lexer rule for “parfor” – Beaver grammar rule for parfor statement type – AST grammar rule for PforStmt – attributes for PforStmt according to your requirement – eg. getXML() for PforStmt in a JastAdd aspect – Buildfile that correctly passes the Natlab source files and your own source files to tools – Custom main method and jar entrypoints 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedFrontend-30