330 likes | 436 Views
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?.
E N D
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? / name of department
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? / name of department
Research Questions • Tools • Repleo • JastAdd • Connection • Methodology • Placeholders • Static Semantic Checking • Syntactic Ambiguities • Implementing the Methodology • Conclusion / name of department
Tools • Repleo • Syntax-safe template engine • JastAdd • Java-based compiler compiler system • Object-oriented abstract syntax • Advanced attribute grammar features / name of department
Repleo • Templates • Architectural Pattern • Code with empty spaces and generative commands • Features • Guarantees syntax-safe output code • Provides meta-language for templates / name of department
Repleo / name of department
JastAdd • Features • Abstract Syntax • Parser Independent • Extended support Attribute Grammars • Aspect Oriented / name of department
JastAdd / name of department
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; / name of department
Attributes Properties as attributes • Synthesized • Inherited • Equations Also: • Rewrites • Circular attributes • Non-terminal attributes / name of department
Weaving Aspects aspect TypeAnalysis { synlazy 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(); } / name of department
Connection • Boolean Evaluator • Simple boolean expressions • Java 1.4 • Using JastAddJ / name of department
Connection / name of department
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 / name of department
Research Questions • Tools • Repleo • JastAdd • Connection • Methodology • Placeholders • Static Semantic Checking • Syntactic Ambiguities • Implementing the Methodology • Conclusion / name of department
Placeholders(1) What are placeholders? • Representation of empty space in code • Substitution or generative command • Properties defined by place of occurrence "<:" TreeQuery ":>" -> PlaceHolderSubstitution[[X]] / name of department
Placeholders(2) Substitution int <: x :>; Conditional <: if x == "true" then :> boolean b; <: else :> int i; <: fi :> / name of department
Placeholders (3) Iterative int i = 0; <: foreach $x in X do :> i = i + <: $x :>; <: od :> Match-Replace int i = 0; i = <: match X :> <: [$e,$t] = :> <: $e :> + <: $t :> <: [] = :> 0 <: end :>; / name of department
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” / name of department
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 :> / name of department
Syntactic Ambiguities • Occur when multiple production can be applied • Parsing problem • SGLR • Bad Smell publicclass A { <: a :> <: b :>(){} } / name of department
Disambiguation • Via Context • Use static semantic checker • Compare previous use of placeholders publicclass A { <: a :> i = 0; <: a :> <: b :>(){} } / name of department
Disambiguation • Using prioritization • Select the option containing the placeholder { int i; i = <: a :>; } / name of department
Research Questions • Tools • Repleo • JastAdd • Connection • Methodology • Placeholders • Static Semantic Checking • Syntactic Ambiguities • Implementing the Methodology • Conclusion / name of department
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 / name of department
Pipeline / name of department
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 / name of department
template( { <: if x == $e then :> boolean i; i = <: match y :> <: [$e, $t] = :> <: $e :> + <: $t :> <: [] = :> 0 <: end :>; <: fi :> i = 0; } ) 1 2 3 4 5 6 7 8 9 10 11 12 Example - Template / name of department
Example – Output(1) --> Resolving ambiguities 6:21 Ambiguity found: Exp Ambiguity resolved using prioritization 6:32 Ambiguity found: Exp Ambiguity resolved using prioritization Done / name of department
Example – Output(2) --> Language Errors 10:2 : Can not assign to a variable of type boolean a value of type int --> Placeholder Errors 6: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 / name of department
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 / name of department
Questions / name of department