Program Representations

Similar documents
PROGRAM ANALYSIS & SYNTHESIS

Chapter 3: Describing Syntax and Semantics. Introduction Formal methods of describing syntax (BNF)

Intermediate Code Generation

Lecture 14 Sections Mon, Mar 2, 2009

CSE 12 Abstract Syntax Trees

LECTURE 3. Compiler Phases

Compilers. Compiler Construction Tutorial The Front-end

Semantic actions for expressions

Formal Semantics of Programming Languages

CSE P 501 Compilers. Static Semantics Hal Perkins Winter /22/ Hal Perkins & UW CSE I-1

CSE 431S Type Checking. Washington University Spring 2013

Formal Semantics of Programming Languages

Tree visitors. Context classes. CS664 Compiler Theory and Design LIU 1 of 11. Christopher League* 24 February 2016

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

A Type Graph Model for Java Programs

Foundations: Syntax, Semantics, and Graphs

COMP520 - GoLite Type Checking Specification

Compiler Passes. Semantic Analysis. Semantic Analysis/Checking. Symbol Tables. An Example

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

COMP520 - GoLite Type Checking Specification

Imperative Paradigm. Syntax. Role of Programming Languages. Expressions. Expressions. Role of Programming Languages. Symbols.

CSE P 501 Compilers. Implementing ASTs (in Java) Hal Perkins Autumn /20/ Hal Perkins & UW CSE H-1

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

Introduction to Programming Using Java (98-388)

CS 432 Fall Mike Lam, Professor. Code Generation

Syntax/semantics. Program <> program execution Compiler/interpreter Syntax Grammars Syntax diagrams Automata/State Machines Scanning/Parsing

Lecture 8 CS 412/413 Spring '00 -- Andrew Myers 2. Lecture 8 CS 412/413 Spring '00 -- Andrew Myers 4

ECE251 Midterm practice questions, Fall 2010

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

CSE P 501 Compilers. Implementing ASTs (in Java) Hal Perkins Winter /22/ Hal Perkins & UW CSE H-1

Application: Programming Language Semantics

Winter Compiler Construction Who. Mailing list and forum

Abstract Syntax Trees

Type Inference Systems. Type Judgments. Deriving a Type Judgment. Deriving a Judgment. Hypothetical Type Judgments CS412/CS413

Implementing Actions

What is a compiler? var a var b mov 3 a mov 4 r1 cmpi a r1 jge l_e mov 2 b jmp l_d l_e: mov 3 b l_d: ;done

Writing Evaluators MIF08. Laure Gonnord

Decaf Language Reference Manual

CSCE 314 Programming Languages. Type System

Functional Programming. Pure Functional Programming

Decaf Language Reference

Compilers. Lecture 2 Overview. (original slides by Sam

Abstract Syntax Trees Synthetic and Inherited Attributes

CS558 Programming Languages

Big-step Operational Semantics (aka Natural Semantics)

CIS 194: Homework 3. Due Wednesday, February 11, Interpreters. Meet SImPL

CS422 - Programming Language Design

1 Lexical Considerations

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

COMP 202 Recursion. CONTENTS: Recursion. COMP Recursion 1

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

EXAMINATIONS 2008 MID-YEAR COMP431 COMPILERS. Instructions: Read each question carefully before attempting it.

Semantic Analysis. How to Ensure Type-Safety. What Are Types? Static vs. Dynamic Typing. Type Checking. Last time: CS412/CS413

Note that in this definition, n + m denotes the syntactic expression with three symbols n, +, and m, not to the number that is the sum of n and m.

10/30/18. Dynamic Semantics DYNAMIC SEMANTICS. Operational Semantics. An Example. Operational Semantics Definition Process

Static Semantics. Lecture 15. (Notes by P. N. Hilfinger and R. Bodik) 2/29/08 Prof. Hilfinger, CS164 Lecture 15 1

CSE 340 Fall 2014 Project 4

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

A Simple Syntax-Directed Translator

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

(Not Quite) Minijava

Hoare Logic: Proving Programs Correct

Introduction to Programming (Java) 2/12

Partial Exam Compilation and Program Analysis (CAP) October, 24th, 2016 Duration: 2 Hours

COMP 181 Compilers. Administrative. Last time. Prelude. Compilation strategy. Translation strategy. Lecture 2 Overview

3. Logical Values. Boolean Functions; the Type bool; logical and relational operators; shortcut evaluation

CS 381: Programming Language Fundamentals

The role of semantic analysis in a compiler

Intermediate Representations

3. Logical Values. Our Goal. Boolean Values in Mathematics. The Type bool in C++

3. Logical Values. Our Goal. Boolean Values in Mathematics. The Type bool in C++

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan

CMSC330 Spring 2017 Midterm 2

CSE450. Translation of Programming Languages. Lecture 11: Semantic Analysis: Types & Type Checking

Project Overview. 1 Introduction. 2 A quick introduction to SOOL

MiniJava Parser and AST. Winter /27/ Hal Perkins & UW CSE E1-1

Lexical and Syntax Analysis

Homework #3: CMPT-379

Context-free grammars (CFG s)

Defining syntax using CFGs

DM550 Introduction to Programming part 2. Jan Baumbach.

Lexical Considerations

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler so far

In Our Last Exciting Episode

More On Syntax Directed Translation

Compiling and Interpreting Programming. Overview of Compilers and Interpreters

Compiler construction 2009

Outline. Top Down Parsing. SLL(1) Parsing. Where We Are 1/24/2013

The SPL Programming Language Reference Manual

CS 3360 Design and Implementation of Programming Languages. Exam 1

Homework 3 Semantic Analysis. Remi Meier Compiler Design

CS 4120 Introduction to Compilers

Lecture 7: Type Systems and Symbol Tables. CS 540 George Mason University

COMP-202. Recursion. COMP Recursion, 2011 Jörg Kienzle and others

Semantic actions for declarations and expressions

Building the Abstract Syntax Trees

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

Semantic actions for declarations and expressions

CS 360 Programming Languages Interpreters

Transcription:

Program Representations 17-654/17-765 Analysis of Software Artifacts Jonathan Aldrich Representing Programs To analyze software automatically, we must be able to represent it precisely Some representations Source code Abstract syntax trees Control flow graph Bytecode Assembly code Binary code 2 1

The WHILE Language A simple procedural language with: assignment statement sequencing conditionals while loops Used in early papers (e.g. Hoare 69) as as a sandbox for thinking about program semantics We will use it to illustrate several different kinds of analysis 3 WHILE Syntax Categories of syntax S Stmt statements a AExp arithmetic expressions x,y Var variables n Num number literals b BExp boolean expressions Syntax S ::= x := a skip S 1 S 2 if b then S 1 else S 2 while b do S a ::= x n a 1 op a a 2 op a ::= + - * / b ::= true false not b b 1 op b b 2 a 1 op r a 2 op b ::= and or op r ::= < = > 4 2

Example WHILE Program Computes the factorial function, with the input in x and the output in z 5 Abstract Syntax Trees A tree representation of source code Based on the language grammar One type of node for each production S ::= x := a := x S ::= while b do S while b a S 6 3

Parsing: Source to AST Parsing process (top down) 1. Determine the top-level production to use 2. Create an AST element for that production 3. Determine what text corresponds to each child of the AST element 4. Recursively parse each child Algorithms have been studied in detail For this course you only need the intuition Details covered in compiler courses 7 y := x while S 2 while 8 4

y := while x S 2 while 9 := y x z := 1 while S 2 while 10 5

:= y x := while S 2 while z 1 11 := y x := while S 2 while z 1 y>1 z :=... 12 6

:= y x := while S 2 while z 1 > z :=... y 1 13 := y x := while S 2 while z 1 > y 1 z:=z*y y:=y-1 14 7

:= y x := while S 2 while z 1 > y 1 := y:=y-1 z z*y 15 := y x := while S 2 while z 1 > y 1 := y:=y-1 z * z y 16 8

:= y x := while S 2 while z 1 > y 1 := := z * y y-1 z y 17 := y x := while S 2 while z 1 > y 1 := := z * y - 18 z y y 1 9

WHILE ASTs in Java Java data structures mirror grammar S ::= x := a skip S 1 S 2 if b then S 1 else S 2 while b do S class AST { class Stmt extends AST { class Assign extends Stmt { Var var AExpr expr class Skip extends Stmt { class Seq extends Stmt { Stmt left Stmt right class If extends Stmt { BExpr cond Stmt thenstmt Stmt elsestmt class While extends Stmt { BExpr cond Stmt body 19 Course Analysis Toolkit Eclipse Open-source Java integrated development environment Extensible through plugins Crystal Plugin for Eclipse Provides a Java AST for analysis Eclipse has its own AST, but it s not ideal for analysis Provides a basic analysis framework Supports interaction with end user 20 10

Extending Crystal Download and install Java version 5 or 6 Download and install Eclipse 3.2 Download and install Crystal Implement a class that extends: ICrystalAnalysis for global analyses AbstractCrystalMethodAnalysis for methodat-a-time analyses This will usually be the case Register your new analysis with Crystal It can then be run from the Crystal menu 21 AbstractCrystalMethodAnalysis public void beforeallmethods() { Called at the beginning of an analysis cycle Use for analysis setup public abstract void analyzemethod(imethoddeclarationnode d) Invoked by the framework for each method in the system You must override this to perform your analysis task for each method public void afterallmethods() { Called at the end of an analysis cycle Use for analysis cleanup and any reporting that s still left 22 11

Example: PrintMethods Crystal crystal = Crystal.getInstance() public void beforeallmethods() { crystal.userout().println( Printing methods: ) public void analyzemethod(imethoddeclarationnode md) { crystal.userout().println(md.getid()) public void afterallmethods() { crystal.userout().println( Done. ) 23 Registering the Analysis In CrystalPlugin.java: public void setupcrystalanalyses( Crystal crystal) { PrintMethods pm = new PrintMethods() crystal.registeranalysis(pm) 24 12

The Crystal AST View Tree in Eclipse Interface package: com.surelogic.ast.java.operator Browse hierarchy IJavaOperatorNode root of tree IDeclarationNode class, field, methods IClassDeclarationNode - classes IMethodDeclarationNode - methods IFieldDeclarationNode and DeclaratorNode int i,j=6,k vs j=6 IStatement Node IBlockStatementNode blocks { IDeclStatementNode variables If, For, Return IExprStatementNode expression statements (incl. assignments) IReturnTypeNode void, other types IPrimitiveTypeNode: boolean, int, float IReferenceTypeNode: int[], String, Foo<T> 25 The Crystal AST Browse hierarchy (continued) IInitializerNode IExpressionNode expressions IBinopExpressionNode: binary expressions IArithBinopExpressionNode: + - * / etc. IAssignmentExpressionNode: assignment IAssignExpressionNode (regular) IOpAssignExpressionNode (+=, -=, *=, etc.) IUnopExpressionNode:!b, x++ ILiteralExpressionNode: 5, 7.3, true, hello, null IPrimaryExpressionNode IAllocationExpressionNode: new X() IFieldRefNode: this.f ISomeFunctionCallNode: x.m(5) ISomeThisExpression: this, X.this ITypeExpressionNode: used as the receiver of static methods (artificial) IVariableUseExpressionNode: x, y IBinding binds a node to a declaration example: call.resolvebinding(), type.resolvetype() IDeclarationNode: declarations are bindings (getnode() returns this) IFieldDeclarationNode IFunctionBinding: gets the function declaration IType IPrimitiveType binds to IPrimitiveTypeNode IDerivedRefType arrays, etc. IDeclaredType a class or interface ITypeFormal a type parameter 26 13

Demo Installing Crystal Run Assignment 0 Look at Assignment 0 code Look at Visitor Results of Assignment 1 27 The Visitor Pattern interface IVisitor<T> { // one for each element type T visit(element e) class DescendingVisitor<T> implements IVisitor<T> { T visit(element e) { T rv = null for(element child : e.getchildren()) { rv = child.accept(this) return rv interface INode { <T> T accept(visitor<t> v) class Element implements INode { <T> T accept(visitor<t> v) { return visitor.visit(this) 28 14