MPLc Documentation. Tomi Karlstedt & Jari-Matti Mäkelä

Similar documents
LECTURE 3. Compiler Phases

3.5 Practical Issues PRACTICAL ISSUES Error Recovery

Project Compiler. CS031 TA Help Session November 28, 2011

Grammars and Parsing, second week

Semantic actions for declarations and expressions

Design and Implementation of a Java to CodeML converter

Semantic actions for declarations and expressions. Monday, September 28, 15

CS202 Compiler Construction. Christian Skalka. Course prerequisites. Solid programming skills a must.

JavaCC Parser. The Compilation Task. Automated? JavaCC Parser

Semantic actions for declarations and expressions

Automatic Parallelization of Sequential C Code

Building Compilers with Phoenix

Extending xcom. Chapter Overview of xcom

Lab 2. Lexing and Parsing with ANTLR4

Lecture 09: Data Abstraction ++ Parsing is the process of translating a sequence of characters (a string) into an abstract syntax tree.

Error Handling Syntax-Directed Translation Recursive Descent Parsing

Syntax and Grammars 1 / 21


Abstract Syntax Trees & Top-Down Parsing

CSCI312 Principles of Programming Languages!

Comp 411 Principles of Programming Languages Lecture 3 Parsing. Corky Cartwright January 11, 2019

COMPILER CONSTRUCTION LAB 2 THE SYMBOL TABLE. Tutorial 2 LABS. PHASES OF A COMPILER Source Program. Lab 2 Symbol table

LL(k) Parsing. Predictive Parsers. LL(k) Parser Structure. Sample Parse Table. LL(1) Parsing Algorithm. Push RHS in Reverse Order 10/17/2012

Exercise ANTLRv4. Patryk Kiepas. March 25, 2017

CS152 Programming Language Paradigms Prof. Tom Austin, Fall Syntax & Semantics, and Language Design Criteria

CS 553 Compiler Construction Fall 2009 Project #1 Adding doubles to MiniJava Due September 8, 2009

Chapter 4. Abstract Syntax

A simple syntax-directed

PSOA-to-TPTP Converter Documentation

Abstract Syntax Trees

CS2112 Fall Assignment 4 Parsing and Fault Injection. Due: March 18, 2014 Overview draft due: March 14, 2014

Derivations vs Parses. Example. Parse Tree. Ambiguity. Different Parse Trees. Context Free Grammars 9/18/2012

Abstract Syntax Trees & Top-Down Parsing

Abstract Syntax Trees & Top-Down Parsing

ECE251 Midterm practice questions, Fall 2010

Compiling Regular Expressions COMP360

I/O and Parsing Tutorial

Why are there so many programming languages? Why do we have programming languages? What is a language for? What makes a language successful?

Syntax-Directed Translation. Lecture 14

Compiler Construction

Error Handling Syntax-Directed Translation Recursive Descent Parsing

CS131 Compilers: Programming Assignment 2 Due Tuesday, April 4, 2017 at 11:59pm

Error Handling Syntax-Directed Translation Recursive Descent Parsing

COSE312: Compilers. Lecture 1 Overview of Compilers

Frequently Asked Questions

Undergraduate Compilers in a Day

Parser Tools: lex and yacc-style Parsing

CS /534 Compiler Construction University of Massachusetts Lowell

9/5/17. The Design and Implementation of Programming Languages. Compilation. Interpretation. Compilation vs. Interpretation. Hybrid Implementation

JJTree. The Compilation Task. Automated? JJTree. An easier way to create an Abstract Syntax Tree

Syntax Analysis MIF08. Laure Gonnord

Chapter IV. Introduction

CSE 401 Midterm Exam Sample Solution 2/11/15

CS 330 Homework Comma-Separated Expression

Trustworthy Compilers

Computer Science Department Carlos III University of Madrid Leganés (Spain) David Griol Barres

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

CS 3304 Comparative Languages. Lecture 1: Introduction

ASTs, Objective CAML, and Ocamlyacc

Error Detection in LALR Parsers. LALR is More Powerful. { b + c = a; } Eof. Expr Expr + id Expr id we can first match an id:

COMPILER CONSTRUCTION Seminar 02 TDDB44

CS164: Programming Assignment 2 Dlex Lexer Generator and Decaf Lexer

Programming Project II

CMPT 379 Compilers. Parse trees

Why are there so many programming languages?

COP4020 Programming Languages. Compilers and Interpreters Robert van Engelen & Chris Lacher

Lecture 14: Parser Conflicts, Using Ambiguity, Error Recovery. Last modified: Mon Feb 23 10:05: CS164: Lecture #14 1

Compiler Construction

Programming Assignment III

Time : 1 Hour Max Marks : 30

Syntax Analysis. Martin Sulzmann. Martin Sulzmann Syntax Analysis 1 / 38

Decaf PP2: Syntax Analysis

PRINCIPLES OF COMPILER DESIGN UNIT I INTRODUCTION TO COMPILING

Software Tools ANTLR

Profile Language Compiler Chao Gong, Paaras Kumar May 7, 2001

Introduction to Compiler Design

CMSC 330: Organization of Programming Languages. Context Free Grammars

An Evaluation of Domain-Specific Language Technologies for Code Generation

Compiling and Interpreting Programming. Overview of Compilers and Interpreters

Modules, Structs, Hashes, and Operational Semantics

Compiler Design. Computer Science & Information Technology (CS) Rank under AIR 100

Parser Tools: lex and yacc-style Parsing

Compilers. Compiler Construction Tutorial The Front-end

UPTR - a simple parse tree representation format

Lexical and Syntax Analysis

CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures. Dan Grossman Autumn 2018

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Interpreters. Prof. Clarkson Fall Today s music: Step by Step by New Kids on the Block

Building Compilers with Phoenix

RYERSON POLYTECHNIC UNIVERSITY DEPARTMENT OF MATH, PHYSICS, AND COMPUTER SCIENCE CPS 710 FINAL EXAM FALL 96 INSTRUCTIONS

Repeating patterns of boilerplate code (like parent/child navigation, Visitor pattern implementation, factory methods for parser tree construction)

PRINCIPLES OF COMPILER DESIGN UNIT I INTRODUCTION TO COMPILERS

Compiler Design (40-414)

CS 553 Compiler Construction Fall 2007 Project #1 Adding floats to MiniJava Due August 31, 2005

Early computers (1940s) cost millions of dollars and were programmed in machine language. less error-prone method needed

Lisp: Lab Information. Donald F. Ross

History of Compilers The term

CA Compiler Construction

Install and Configure ANTLR 4 on Eclipse and Ubuntu

Building lexical and syntactic analyzers. Chapter 3. Syntactic sugar causes cancer of the semicolon. A. Perlis. Chomsky Hierarchy

Transcription:

MPLc Documentation Tomi Karlstedt & Jari-Matti Mäkelä July 11, 2008

Contents 1 Introduction 1 2 Implementation 3 3 Structure 5 3.1 Class MPLc............................ 7 3.2 Class AspectJPrinter....................... 7 4 Installation 9 5 Using MPLc 11

Chapter 1 Introduction MPLc is a MPL-to-AspectJ compiler. MPLc uses Antlr v3 to lex and parse MPL files and hand-written tree walking to transform the Antlr generated Abstract Syntax Trees to a printable AspectJ form. Running the MPLc program always prints an aspect if errors do not occur during the parsing or tree walking. Printing can be done into a file or into standard output (System.out). MPLc version 1.1 is compatible with the MPL version 0.7.0 Antlr grammar. MPLc does not require Antlr to be run as the Antlr generated lexer and parser are provided in the JAR package. However, MPLc still depends on the Antlr runtime package. In chapter 2 we describe the compile process. The chapter 3 goes through the implementation class by class. Chapters 4 and 5 give system requirements, installation instructions, and usage instructions. 1

2 CHAPTER 1. INTRODUCTION

Chapter 2 Implementation MPLc is run by calling the main method of the MPLc class. The general control flow of MPLc can be described in 3 steps. The first step reads from policy files and turns the token streams into Abstract Syntax Trees. The second step manipulates the generated trees checking for possible errors and producing trees that are easily printed. This data is saved into a SemanticResult container. The final step is the actual printing process. 1. First pass : a. The main method creates a new instance of FirstPassHandler which runs the compile(string) method for the given policy name. The compile(string) method first executes the Antlr-generated lexer and parser for the policy file which produce an AST. b. The AST nodes are separated into names, imports, policy variables, package imports, and rules. c. For each of the listed imports the method calls compile(string) method recursively and creates a new Module instance with references to the original class members. The pass keeps track of cyclic policy imports and issues an error if one is found. d. The pass produces a tree structure of modules which contains lists of all the defined rules, variables, and imports in all of the policies. 2. Second pass : a. Trees containing class imports are transformed into list of Strings. 3

4 CHAPTER 2. IMPLEMENTATION b. Policy variables are converted from trees to PolicyVariable objects. This is done recursively to preserve the introduction order from imported policies. c. The variables are finally checked of type inconsistencies and the final default value is determined. d. The rules are mapped into rule definitions, condition trees, and effect trees. The mapping preserves the recursive introduction order. e. Finally the effects are validated to the policy variable assignment form. 3. Pretty-print: MPLc creates a printer object with the set of data to be printed and the print stream. The aspect is printed through the printaspect() method. The printing process uses some tree rewrite rules with imaginary tokens to identify MPL construct related tokens. Apart from these rewrites the printing is straight-forward token printing from the given set of data. In MPLc version 1.1 the AspectJ printer is still responsible of certain MPL-to-AspectJ transformation which may cause non-printing related error messages in printing phase. See Section 3.2 for more information.

Chapter 3 Structure MPLc 1.1 includes Java files as follows: package: mpl MPLc.java: The main compiler class which includes the main program to be run. See Section 3.1 for more information. MPLSet.java: A definition for the MPL set variable used in aspects. Extends generic HashSet<T> by implementing the different set expressions. MPLShutdown.java: An interface for the shutdown sequence. As of MPL 1.0 contains only the shutdown() method that must be implemented by the concrete shutdown object. MPLSimpleShutdown.java: Gives a simple implementation for the shutdown sequence by terminating the program and giving an error message in System.err. package: mpl.backend AspectJPrinter.java: The AspectJ pretty-printer class for the MPLc compiler. In MPLc version 1.1 handles some further MPL-to-AspectJ transformation which is described in Section 3.2. AspectCPPPrinter.java: The AspectC++ pretty-printer class for the MPLc compiler. Not currently implemented. 5

6 CHAPTER 3. STRUCTURE AspectPrinter.java: Generic interface for all pretty-printer classes. package: mpl.frontend FirstPassHandler.java: Loads imports and classifies AST nodes. FirstPassHandlerCore.java: Abstract helper routines for the first pass. Module.java: Represents the data of one MPL policy file. MPLLexer.java: Antlr-generated lexer that takes an MPL policy file as input. MPLParser.java: Antlr-generated parser that takes a token stream from the lexer as input and generates an AST for the given MPL policy. The form of the AST is defined in the MPL grammar for Antlr v3 (MPL.g). Rule.java: Container for preprocessed lists of AST nodes of conditions, effects, and rules. SemanticPassHandler.java: Processes all imports, java package imports, variables, rules, and effects found in MPL files. Also validates effects and variable definitions. SemanticPassHandlerCore.java: Abstract helper routines for the second pass. SemanticResult.java: Container for all relevant data gathered during the first two passes. This container is used to print the aspect. TokenTypes.java: Token enumeration for AST manipulation. Automatically generated. package: mpl.frontend.types Type, TypeList, TypeSet, and TypeSimple represent all supported policy variable types.

3.1. CLASS MPLC 7 package: mpl.frontend.variabl es PolicyVariable, VarList, VarPattern, VarPrimitive, and VarSet represent supported policy variables. 3.1 Class MPLc MPLc includes the main method that handles the given command-line options and starts the compiling process. In delegates the manual tree walking needed to transform the given input files into printable trees first to First- PassHandler, which imports all required policy files, lexes and parses the input files, but also classifies the abstract syntax tree on high level. A simple semantic pass follows the first pass. The second pass extracts more data from the syntax tree into easily accessible lists and partly validates the language (effects and variable conflicts). Finally the main method chooses between available pretty-printers and output streams based on the given command line options. 3.2 Class AspectJPrinter AspectJPrinter prints an aspect from the set of trees (SemanticResult) given as parameter in the constructor. The overall form of the aspect is generated in the printaspect() method which calls other methods to print more finegrained constructs of the aspect including the aspect variables, pointcut definitions, and the different advices. Antlr rewrite rules with imaginary tokens are used to identify MPL-related constructs and modified accordingly while Java expressions are printed token by token. Due to limitations in MPLc design and implementation the compiler object does not create a rule-specific variable type table. This leads to type checking problems in the printer implementation. In particular the printer checks element-to-set (e2s) conversion validity. Therefore it is possible that the printer object halts the compiling process with a proper error message. This will happen after the printer has written some amount of information into the defined print stream.

8 CHAPTER 3. STRUCTURE

Chapter 4 Installation MPLc is distributed in a JAR package containing the compiler and all of the required classes for using MPLc generated aspects. In order to generate aspects from policies you will need Java 1.5 or later and Antlr v3 or later. In order to use the generated aspects you will need AspectJ 5 or later. Also either mplc-rt-1.1.jar or mplc-1.1.jar is needed to compile the AspectJ aspects. Installation: 1 Save mplc-1.1.jar and antlr-runtime-3.0.x.jar into a directory. 2 Add the saved mplc-1.1.jar and antlr-runtime-3.0.x.jar to the CLASSPATH environmental variable. Requirements: - Java 1.5, Antlr runtime Optional requirements: - AspectJ 5 9

10 CHAPTER 4. INSTALLATION

Chapter 5 Using MPLc MPLc is used from the command line like: java MPLc <options> <policymodule> Where policy module is the name of the transformed policy. The policy must be found in a file called <policymodule>.mpl. In MPLc 1.1 the extension doesn t need to be omitted. If a policy includes further imported policies they must be found in separate.mpl files either in the current directory or in same directory as <policymodule>.mpl. MPLc creates a file called <policymodule>.java (unless other options are used) to the current directory where <policymodule> is the name of the given policy. MPLc options include: -print Prints the aspect into System.out instead of a file. -shutdown <name> Uses an object of type <name> as the shutdown sequence. The class <name> must implement the interface MPLShutdown -aspect <name> Prints the aspect into a file called <name>.aj instead of <policymodule>.aj. -language <language> Generate the aspect code in given language, supported options: cpp (AspectC++) and java (AspectJ). -debuginfo Generates extra runtime code to the aspects that prints debug info whenever effects are being executed or conditions tested. 11

12 CHAPTER 5. USING MPLC To compile the generated aspects, Java 1.5 compliance must be used with the AspectJ compiler as well as the mplc-1.1.jar or mplc-rt-1.1.jar must be found in the CLASSPATH env variable.