introducing static semantic analysis to templates using jastadd

33
Introducing Static Introducing Static Semantic Analysis to Semantic Analysis to Templates Templates Using JastAdd Using JastAdd Jos Peeters Jos Peeters

Upload: ophrah

Post on 25-Feb-2016

34 views

Category:

Documents


0 download

DESCRIPTION

Introducing Static Semantic Analysis to Templates Using JastAdd. Jos Peeters. Goal. Ambition Improve the quality of code generators based on templates. Primary Research Question Can static semantic checking be used in template-based code generators using off-the-shelf tooling?. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Introducing Static Semantic Analysis to Templates  Using JastAdd

Introducing Static Semantic Introducing Static Semantic Analysis to Templates Analysis to Templates Using JastAddUsing JastAddJos PeetersJos Peeters

Page 2: Introducing Static Semantic Analysis to Templates  Using JastAdd

/ name of department PAGE 204/22/23

Goal

Ambition• Improve the quality of code generators based on templates.

Primary Research Question• Can static semantic checking be used in template-based code

generators using off-the-shelf tooling?

Page 3: Introducing Static Semantic Analysis to Templates  Using JastAdd

/ name of department PAGE 304/22/23

Research Questions

• Can JastAdd be coupled to Repleo?• Is the resulting coupling scalable?

• How can static semantic checking be performed on templates?• How to deal with syntactic ambiguities?• How to implement this using JastAdd?

Page 4: Introducing Static Semantic Analysis to Templates  Using JastAdd

/ name of department PAGE 404/22/23

• Research Questions• Tools

• Repleo• JastAdd• Connection

• Methodology • Placeholders• Static Semantic Checking• Syntactic Ambiguities

• Implementing the Methodology• Conclusion

Page 5: Introducing Static Semantic Analysis to Templates  Using JastAdd

/ name of department PAGE 504/22/23

Tools

• Repleo• Syntax-safe template engine

• JastAdd• Java-based compiler compiler system• Object-oriented abstract syntax• Advanced attribute grammar features

Page 6: Introducing Static Semantic Analysis to Templates  Using JastAdd

/ name of department PAGE 604/22/23

Repleo

• Templates• Architectural Pattern• Code with empty spaces and generative commands

• Features• Guarantees syntax-safe output code• Provides meta-language for templates

Page 7: Introducing Static Semantic Analysis to Templates  Using JastAdd

/ name of department PAGE 704/22/23

Repleo

Page 8: Introducing Static Semantic Analysis to Templates  Using JastAdd

/ name of department PAGE 804/22/23

JastAdd

• Features• Abstract Syntax• Parser Independent• Extended support Attribute Grammars• Aspect Oriented

Page 9: Introducing Static Semantic Analysis to Templates  Using JastAdd

/ name of department PAGE 904/22/23

JastAdd

Page 10: Introducing Static Semantic Analysis to Templates  Using JastAdd

/ name of department PAGE 1004/22/23

Abstract syntax

abstract BlockStmt;abstract Stmt: BlockStmt;

AssignStmt : Stmt ::= Variable:Access Value:Exp;WhileStmt : Stmt ::= Condition:Exp Body:Stmt;

abstract Exp;abstract Access:Exp;IntLiteral : Exp ::= <Value:String>;

abstract Operator : Exp;abstract BinOperator : Operator ::= lhs:Exp rhs:Exp;PlusOp : BinOperator;

Page 11: Introducing Static Semantic Analysis to Templates  Using JastAdd

/ name of department PAGE 1104/22/23

Attributes

Properties as attributes• Synthesized• Inherited• Equations

Also:• Rewrites• Circular attributes• Non-terminal attributes

Page 12: Introducing Static Semantic Analysis to Templates  Using JastAdd

/ name of department PAGE 1204/22/23

Weaving Aspects

aspect TypeAnalysis { syn lazy TypeDecl Exp.type();

eq BinOperator.type() { if (getlhs().type().isSubtypeOf(getrhs().type()) || getrhs().type().isSubtypeOf(getlhs().type()) )

{ if (getlhs().type().isUnknown() )

return getrhs().type(); return getlhs().type(); } return unknownDecl().type(); }

eq IntLiteral.type() = intType();}

Page 13: Introducing Static Semantic Analysis to Templates  Using JastAdd

/ name of department PAGE 1304/22/23

Connection

• Boolean Evaluator• Simple boolean expressions

• Java 1.4• Using JastAddJ

Page 14: Introducing Static Semantic Analysis to Templates  Using JastAdd

/ name of department PAGE 1404/22/23

Connection

Page 15: Introducing Static Semantic Analysis to Templates  Using JastAdd

/ name of department PAGE 1504/22/23

Connection JastAddJ

Issues• No strict separation between checker and parser

• Instantiation parts of checker• Rewriting during parse phase• Grammar differences between SDF and AST

For placeholder a 1-to-1 transformation is required

Page 16: Introducing Static Semantic Analysis to Templates  Using JastAdd

/ name of department PAGE 1604/22/23

• Research Questions• Tools

• Repleo• JastAdd• Connection

• Methodology • Placeholders• Static Semantic Checking• Syntactic Ambiguities

• Implementing the Methodology• Conclusion

Page 17: Introducing Static Semantic Analysis to Templates  Using JastAdd

/ name of department PAGE 1704/22/23

Placeholders(1)

What are placeholders?• Representation of empty space in code• Substitution or generative command• Properties defined by place of occurrence

"<:" TreeQuery ":>" -> PlaceHolderSubstitution[[X]]

Page 18: Introducing Static Semantic Analysis to Templates  Using JastAdd

/ name of department PAGE 1804/22/23

Placeholders(2)

Substitutionint <: x :>;

Conditional<: if x == "true" then :> boolean b; <: else :> int i; <: fi :>

Page 19: Introducing Static Semantic Analysis to Templates  Using JastAdd

/ name of department PAGE 1904/22/23

Placeholders (3)

Iterativeint i = 0;<: foreach $x in X do :> i = i + <: $x :>;<: od :>

Match-Replaceint i = 0;i = <: match X :> <: [$e,$t] = :> <: $e :> + <: $t :> <: [] = :> 0<: end :>;

Page 20: Introducing Static Semantic Analysis to Templates  Using JastAdd

/ name of department PAGE 2004/22/23

Static Semantic Checking

• Target language• Type checking• Context related checks (f.i. duplicate declarations)

• Placeholders language• Uniqueness meta-variables (i.e. $e)

• Combination of both• Checking influence of placeholders on target language• “Bad Smells”

Page 21: Introducing Static Semantic Analysis to Templates  Using JastAdd

/ name of department PAGE 2104/22/23

Bad Smells

Not permitted• errors that are related to properties of placeholders

<: foreach $x in X do :> int i;<: od :>

Permitted• restrictions given only by the input data

<: foreach $x in X do :> int <: $x :>;<: od :>

Page 22: Introducing Static Semantic Analysis to Templates  Using JastAdd

/ name of department PAGE 2204/22/23

Syntactic Ambiguities

• Occur when multiple production can be applied• Parsing problem

• SGLR• Bad Smell

public class A { <: a :> <: b :>(){}}

Page 23: Introducing Static Semantic Analysis to Templates  Using JastAdd

/ name of department PAGE 2304/22/23

Disambiguation

• Via Context• Use static semantic checker• Compare previous use of placeholders

public class A { <: a :> i = 0;

<: a :> <: b :>(){}}

Page 24: Introducing Static Semantic Analysis to Templates  Using JastAdd

/ name of department PAGE 2404/22/23

Disambiguation

• Using prioritization• Select the option

containing the placeholder

{ int i; i = <: a :>;}

Page 25: Introducing Static Semantic Analysis to Templates  Using JastAdd

/ name of department PAGE 2504/22/23

• Research Questions• Tools

• Repleo• JastAdd• Connection

• Methodology • Placeholders• Static Semantic Checking• Syntactic Ambiguities

• Implementing the Methodology• Conclusion

Page 26: Introducing Static Semantic Analysis to Templates  Using JastAdd

/ name of department PAGE 2604/22/23

Implementing the Methodology

PicoJava• Subset of Java• Originally an example of JastAdd

Present• PicoJava language definition in JastAdd• Attributes to support checking• Static semantic checker• SDF definition Placeholders

Page 27: Introducing Static Semantic Analysis to Templates  Using JastAdd

/ name of department PAGE 2704/22/23

Pipeline

Page 28: Introducing Static Semantic Analysis to Templates  Using JastAdd

/ name of department PAGE 2804/22/23

Implementation

Implemented as part of this project• SDF definition PicoJava• Traversal SGLR JastAdd

With JastAdd• Language definition placeholders• Extend attributes for the placeholders• Static semantic checker

• Placeholders• Bad Smells

• Ambiguity nodes• Disambiguation filter

NB. JastAdd is Aspect-Oriented

Page 29: Introducing Static Semantic Analysis to Templates  Using JastAdd

/ name of department PAGE 2904/22/23

Example - Template

template({ <: if x == $e then :> boolean i; i = <: match y :> <: [$e, $t] = :> <: $e :> + <: $t :> <: [] = :> 0 <: end :>; <: fi :> i = 0; })

123456789

101112

Page 30: Introducing Static Semantic Analysis to Templates  Using JastAdd

/ name of department PAGE 3004/22/23

Example – Output(1)

--> Resolving ambiguities 6:21 Ambiguity found: Exp Ambiguity resolved using prioritization 6:32 Ambiguity found: Exp Ambiguity resolved using prioritization Done

Page 31: Introducing Static Semantic Analysis to Templates  Using JastAdd

/ name of department PAGE 3104/22/23

Example – Output(2)--> Language Errors 10:2 : Can not assign to a variable of type boolean a value of type int

--> Placeholder Errors6:6 : duplicate declaration of meta-variables [$e]

--> Placeholder Bad Smells 7:6 : inferred type of placeholder does not match the type of its declaration

10:2 : Variable i might not be declared, dependent on [x]

--> List of PlaceHolders $e PhExp $unknown $unknown $t PhExp $unknown $unknown y PhMRExp $unknown int x PhIfBlockStmt $unknown $unknown

Page 32: Introducing Static Semantic Analysis to Templates  Using JastAdd

/ name of department PAGE 3204/22/23

Conclusions

Presented in this Thesis• Static Semantic Checks on Template• Use of off-the-shelf tools

• Repleo• JastAdd

Methodology• Multi-language approach• Ease-of-use of templates• Syntax-safe• Static Semantic Analysis

Page 33: Introducing Static Semantic Analysis to Templates  Using JastAdd

/ name of department PAGE 3304/22/23

Questions