SlideShare una empresa de Scribd logo
1 de 26
L0081 - 2010-11-08
Redistribution and other use of this material requires written permission from The RCP Company.
ITU - MDD – XText
This presentation describes the use of XText.
This presentation assumes a good knowledge of Data Modeling and Grammars
as previously presented.
This presentation is developed for MDD 2010 course at ITU, Denmark.
L0081 - 2010-11-08
2
 Parse trees are very detailed:
 every step in a derivation is a node
 After the parsing phase is done, the details of derivation are not needed for later
phases
 Semantic Analyzer removes intermediate productions to create an (abstract)
syntax tree – known as an AST
expr
term
factor
ID: x
expr
ID: x
Parse Tree: Abstract Syntax Tree:
Abstract Syntax Trees
L0080 - 2010-11-08
3
Parsing an Expression assignment => ID “=“ expression;
expression => expression “+” term
| expression “-” term
| term
term => term “*” factor
| term “/” factor
| factor
factor => “(“ expression “)”
| ID
| NUMBER
ID ~ y expresssion ~ (2*x + 5)*x - 7
expression ~ (2*x + 5)*x term ~ 7
assignment ~ y = (2*x + 5)*x - 7
factor ~ 7term ~ (2*x + 5)*x
NUMBER ~ 7term ~ (2*x + 5) factor ~ x
ID ~ xfactor ~ (2*x + 5)
expression ~ 2*x + 5
expression ~ 2*x term ~ 5
factor ~ 5term ~ 2*x
NUMBER ~ 5factor ~ xterm ~ 2
ID ~ x
factor ~ 2
NUMBER ~ 2
L0081 - 2010-11-08
4
Building a AST
assignment => ID “=“ expression;
expression => expression “+” term
| expression “-” term
| term
term => term “*” factor
| term “/” factor
| factor
factor => “(“ expression “)”
| ID
| NUMBER
ID ~ y expresssion ~ (2*x + 5)*x - 7
expression ~ (2*x + 5)*x term ~ 7
assignment ~ y = (2*x + 5)*x - 7
factor ~ 7term ~ (2*x + 5)*x
NUMBER ~ 7term ~ (2*x + 5) factor ~ x
ID ~ xfactor ~ (2*x + 5)
expression ~ 2*x + 5
expression ~ 2*x term ~ 5
factor ~ 5term ~ 2*x
NUMBER ~ 5factor ~ xterm ~ 2
ID ~ x
factor ~ 2
NUMBER ~ 2
Assignment(y)
Expression(-)
Expression(*)
NUMBER(7)
Expression(+)
Expression(*)
NUMBER(2)
NUMBER
ID(x)
ID(x)
L0081 - 2010-11-08
5
Level of Details
 How detailed should a grammar be?
 Should the grammar be rich – i.e. contain all details?
 Or should the grammar be as thin as possible?
 In general use a thin grammar to avoid too many keywords
 For XText, use a rich grammar as this is used to provide context assist
 Other similar questions:
 Should you model dates?
 How about ranges?
Flight : 'flight' ID '{' ( ('from'|'to') '=' STRING ';' )* '}'
Flight : 'flight' ID '{' ( ID '=' STRING ';' )* '}'
L0081 - 2010-11-08
6
Line feed or Explicit Terminators
 Should you have an explicit “statement” terminator or use line feeds?
 Statement Terminator such as “;” or “.”
 Pro: much easier to detect errors in the input
 Con: normally not natural for the user
 Statement Terminator such as line feed
 Pro: much easier to detect errors in the input
 Con: cannot divide a logical line over multiple physical lines
 No Statement Terminators
 Pro: very natural for many users
 Con: difficult to detect errors in the input
 In general use a statement terminator – not a statement separator!
 In our scenario, it properly makes good sense to use line feeds
 And indentions for scoping
L0081 - 2010-11-08
7
What is XText exactly
 Xtext is a complete environment for development of textual programming
languages and domain-specific languages.
 It is implemented in Java and is based on Eclipse, EMF, and Antlr.
 The Basic Idea?
 Augment an EBNF grammar
 Roll it through XText
 Have an Eclipse Editor
 The XText tool bench
 Grammar is defined in an EBNF-like format in the Xtext editor.
 The editor provides code completion and constraint checking for the
grammars themselves
 Grammar is a collection of Rules. Rules start with their name followed by “:”
and ending with “;”
L0081 - 2010-11-08
8
Getting Started with XText
 Create a new XText project with the New… wizard
L0081 - 2010-11-08
9
The XText File Structure
 Three projects!
 You can make changes to the files in the src files,
but not the files in the src-gen folders
Your grammar rules
Workflow description
used to create editor
Generated ECore model
Generated ECore
classes
L0081 - 2010-11-08
10
An XText Model File (.xtext)
 Identify of model
 Include of base declarations and terminals
 Directive to create model
 NS URI of model
 It is also possible to import an existing model
grammar org.xtext.example.MyDsl with org.eclipse.xtext.common.Terminals
generate myDsl "http://www.xtext.org/example/MyDsl"
Model :
Type*;
…
L0081 - 2010-11-08
11
Generating Files from a Model
 To generate an Eclipse editor from a a model (.xtext)
 Select the workflow file (.mwe2)
 Use “Run As…”  “MWE2 WorkFlow”
L0081 - 2010-11-08
12
The Basic XText Concepts
 The AST is a ECore model
 The rules of the input grammar are used to create the AST (more or less
automatically) or interface to an existing imported model (=AST)
 So we must identify
 The entities of the model
 The attributes of the model
 The relations – containment and references – of the model
 The input language of XText is a augmented EBNF (almost )
L0081 - 2010-11-08
13
Example
 Grammar for a (very) simple type system
Model : Type*;
Type: SimpleType | Entity;
SimpleType: ‘datatype' ID;
Entity : 'entity' ID ('extends' ID)? '{'
Property*
'}';
Property: 'property' ID ':' ID ('[]')?;
datatype A
datatype B
entity E {
property a : A
property b : B[]
}
entity F extends E {
property c : A
}
Model
Type
SimpleType Entity
Property
*
*
Super-type
L0081 - 2010-11-08
14
 XText rules are EBNF plus type information
 Type Rules
 For each rule XText creates an entity in the logical model
 Each rule property results in a property in the logical model
 String Rules
 String rules are parsed to be a string
 These are in effect custom lexer rules as they recognize string patterns
 Enum Rules
 Limited set of alternatives
 Mapped to an enumeration in the logical model
 Native Rules
 A lexer rule that is mapped directly to an ANTLR rule
 Mapped to a String
Different Kinds of XText Rules
L0081 - 2010-11-08
15
 Class of type rule defaults to name of rule 
 Can be overruled via “{ClassName}” construct
 A type rule can be abstract
 An abstract type rule is basically a collection of OR-ed alternatives: R1 | R2 |
R3
 Mapped to an abstract metaclass

The or-ed alternatives become concrete subclasses

Common properties of the alternatives are lifted into the abstract
superclass
Abstract Type Rules
Model : Type*;
Type: SimpleType | Entity;
SimpleType: ‘datatype' ID;
Entity : 'entity' ID ('extends' ID)? '{'
Property*
'}';
Property: 'property' ID ':' ID ('[]')?;
L0081 - 2010-11-08
16
Example with Type Rules
 Every rule corresponds to en entity
 “Or” rules becomes abstract classes with the entities of the sub-rules as child
classes
 Above we have Type as an abstract super class for SimpleType and Entity
Model : Type*;
Type: SimpleType | Entity;
SimpleType: 'type' ID;
Entity : 'entity' ID ('extends' ID)? '{'
Property*
'}';
Property: 'property' ID ':' ID ('[]')?;
L0081 - 2010-11-08
17
Some built-in String Rules (Terminals)
 The definition of the central terminals
 Really part of lexical analysis!
 Specific terminals can be “hidden” in the grammar or for specific rules, meaning
they are recognized but ignored during parsing…
 Very useful if you want a line-oriented grammar
terminal ID : '^'?('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'_'|'0'..'9')*;
terminal INT returns ecore::EInt: ('0'..'9')+;
terminal STRING:
'"' ( '' ('b'|'t'|'n'|'f'|'r'|'"'|"'"|'') | !(''|'"') )* '"' |
"'" ( '' ('b'|'t'|'n'|'f'|'r'|'"'|"'"|'') | !(''|"'") )* "'"
;
terminal ML_COMMENT: '/*' -> '*/';
terminal SL_COMMENT : '//' !('n'|'r')* ('r'? 'n')?;
terminal WS : (' '|'t'|'r'|'n')+;
L0081 - 2010-11-08
18
 A type rule has a rule composition – made of a number of elements
 It may contain keywords (using string literal syntax)
 It also contains properties which will result in properties of the type class

The property type is derived from the called rule
 There are different kinds
of properties

= (single assign)

+= (multiple assign/add)

?= (boolean assign)
 There are different property
cardinalities
 ? (0..1)
 * (0..n)
 + (1..n)
 nothing (1..1)
Properties (Attributes and Relations) in Type Rules
Entity :
'entity' name=ID ('extends' extends=ID)? '{'
properties+=Property*
'}';
Property:
'property' name=ID ':' type=ID (many?='[]')?;
Results in containment relation
“List<Property> properties”
Results in simple attribute
“String: name”
L0081 - 2010-11-08
19
Example of Properties
 Simple property:
 name of SimpleType
 “Many” reference – containment
 elements of Model
 properties of Entity
 Boolean property
 many of Property
Model :
(elements+=Type)*;
Type:
SimpleType | Entity;
SimpleType:
'type' name=ID;
Entity :
'entity' name=ID ('extends' extends=ID)? '{'
properties+=Property*
'}';
Property:
'property' name=ID ':' type=ID (many?='[]')?;
L0081 - 2010-11-08
20
 By default, rules results in a logical model with a tree of entities via containment
 You can reference other elements via a reference rule
 In textual languages a reference has to be by name
 During linking, Xtext “dereferences” these by name-references
 Why References?
 Used in the logical model for references
 XText also uses references for context assist
Reference Relations
Entity :
'entity' name=ID ('extends' extends=[Entity])? '{'
properties+=Property*
'}';
Results in containment relation
“List<Property> properties”
Results in reference relation
“Entity extends”
L0081 - 2010-11-08
21
Example of References
 extends of Entity
 type of Property
Model :
(elements+=Type)*;
Type:
SimpleType | Entity;
SimpleType:
'type' name=ID;
Entity :
'entity' name=ID ('extends' extends=[Entity])? '{'
properties+=Property*
'}';
Property:
'property' name=ID ':' type=[Type] (many?='[]')?;
L0081 - 2010-11-08
22
 A Enum Rule is used to define a limited set of defined alternatives
 It is mapped to an enumeration in the logical model
 It is declared via the Enum keyword and contains Enum Literals
 An Enum Literal has a token name and a string representation
 It can be used just like any other rule
 Properties will get the enumeration type
Enumeration Rules
enum Color:
RED=“red” | GREEN=“green” | BLUE=“blue” ;
Shape: name=ID ( ‘color’ color=Color)? … ;
L0081 - 2010-11-08
23
 A native rule contains a string which is passed to ANTLR without further
processing it.
 It is typically used to define lexer rules that cannot be expressed using Xtext
syntax
 E.g. whitespace-aware lexer rules, such as define custom comment syntax
Native Rules
L0081 - 2010-11-08
24
Importing an Existing Model
 To import an model instead of generating it..
 Simple change to the xtext grammar file:
 Depending on the starting point a simple change to the MWE2 file:
 Not so with the generated MWE2 file 
 Easier to start with the wizard “XText Project from existing ECore Models”
grammar com.rcpcompany.mdd2010.DSL with org.eclipse.xtext.common.Terminals
import "platform:/resource/com.rcpcompany.mdd2010.model/model/Travel.ecore"
import "http://www.eclipse.org/emf/2002/Ecore" as ecore
…
fragment = org.eclipse.xtext.generator.ecore.EcoreGeneratorFragment {
genModels =
"platform:/resource/com.rcpcompany.mdd2010.model/model/Travel.genmodel"
}
L0081 - 2010-11-08
25
More Information
XText and friends
 “Build your own textual DSL with Tools from the Eclipse Modeling Project”
 http://www.eclipse.org/articles/article.php?file=Article-BuildYourOwnDSL/index.html

Older, slightly out-dated, article on how to create your first XText project
 Documentation for XText
 http://www.eclipse.org/Xtext/documentation/
L0081 - 2010-11-08
26
Exercise 1
 Make sure you grammar is on proper EBNF form
 Do you think you can make an interesting editor from the grammar?
 Create an XText project if not already done
 Convert your EBNF grammar into an XText model
 What can you do to your grammar to make it more suitable for XText

Más contenido relacionado

La actualidad más candente

class and objects
class and objectsclass and objects
class and objects
Payel Guria
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).ppt
Alok Kumar
 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with Java
Jussi Pohjolainen
 
Classes in c++ (OOP Presentation)
Classes in c++ (OOP Presentation)Classes in c++ (OOP Presentation)
Classes in c++ (OOP Presentation)
Majid Saeed
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
Abhilash Nair
 
Implementation of oop concept in c++
Implementation of oop concept in c++Implementation of oop concept in c++
Implementation of oop concept in c++
Swarup Kumar Boro
 
Programming in c++
Programming in c++Programming in c++
Programming in c++
Baljit Saini
 

La actualidad más candente (20)

CLASS OBJECT AND INHERITANCE IN PYTHON
CLASS OBJECT AND INHERITANCE IN PYTHONCLASS OBJECT AND INHERITANCE IN PYTHON
CLASS OBJECT AND INHERITANCE IN PYTHON
 
Learn Concept of Class and Object in C# Part 3
Learn Concept of Class and Object in C#  Part 3Learn Concept of Class and Object in C#  Part 3
Learn Concept of Class and Object in C# Part 3
 
Pi j2.3 objects
Pi j2.3 objectsPi j2.3 objects
Pi j2.3 objects
 
Variables in python
Variables in pythonVariables in python
Variables in python
 
Oops in java
Oops in javaOops in java
Oops in java
 
Chapter 01 Introduction to Java by Tushar B Kute
Chapter 01 Introduction to Java by Tushar B KuteChapter 01 Introduction to Java by Tushar B Kute
Chapter 01 Introduction to Java by Tushar B Kute
 
class and objects
class and objectsclass and objects
class and objects
 
Lecture 8 abstract class and interface
Lecture   8 abstract class and interfaceLecture   8 abstract class and interface
Lecture 8 abstract class and interface
 
Oops
OopsOops
Oops
 
Chapter 8 - Exceptions and Assertions Edit summary
Chapter 8 - Exceptions and Assertions  Edit summaryChapter 8 - Exceptions and Assertions  Edit summary
Chapter 8 - Exceptions and Assertions Edit summary
 
Chapter 05 classes and objects
Chapter 05 classes and objectsChapter 05 classes and objects
Chapter 05 classes and objects
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).ppt
 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with Java
 
Classes in c++ (OOP Presentation)
Classes in c++ (OOP Presentation)Classes in c++ (OOP Presentation)
Classes in c++ (OOP Presentation)
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 
Implementation of oop concept in c++
Implementation of oop concept in c++Implementation of oop concept in c++
Implementation of oop concept in c++
 
C++ Object oriented concepts & programming
C++ Object oriented concepts & programmingC++ Object oriented concepts & programming
C++ Object oriented concepts & programming
 
Programming in c++
Programming in c++Programming in c++
Programming in c++
 
Structures in c++
Structures in c++Structures in c++
Structures in c++
 
Abstract data types
Abstract data typesAbstract data types
Abstract data types
 

Destacado

The association between gut microbiota and body weight
The association between gut microbiota and body weightThe association between gut microbiota and body weight
The association between gut microbiota and body weight
Ashwath Venkatasubramanian
 
Màrqueting Mix. Preu i distribució
Màrqueting Mix. Preu i distribucióMàrqueting Mix. Preu i distribució
Màrqueting Mix. Preu i distribució
IMET Club Emprendre
 
Labor Heidrich Zeugnis English Translation
Labor Heidrich Zeugnis English TranslationLabor Heidrich Zeugnis English Translation
Labor Heidrich Zeugnis English Translation
Friedrich Griessel
 
Halloween Horror Show
Halloween Horror ShowHalloween Horror Show
Halloween Horror Show
Leo Zalki
 
Pilot selection anthropometry a comparison with measures taken by a single a...
Pilot selection anthropometry  a comparison with measures taken by a single a...Pilot selection anthropometry  a comparison with measures taken by a single a...
Pilot selection anthropometry a comparison with measures taken by a single a...
Leishman Associates
 
Surgical workload at the role 3 multinational medical unit, kandahar airfield...
Surgical workload at the role 3 multinational medical unit, kandahar airfield...Surgical workload at the role 3 multinational medical unit, kandahar airfield...
Surgical workload at the role 3 multinational medical unit, kandahar airfield...
Leishman Associates
 
სხვიტორი-სავანე
სხვიტორი-სავანესხვიტორი-სავანე
სხვიტორი-სავანე
lazana
 
Guy Danon Beyond 2010
Guy Danon Beyond 2010Guy Danon Beyond 2010
Guy Danon Beyond 2010
eventwithme
 
TeliaSonera
TeliaSoneraTeliaSonera
TeliaSonera
Farhad
 

Destacado (19)

OCL tutorial
OCL tutorial OCL tutorial
OCL tutorial
 
Presentatie 3 A Communicatie
Presentatie 3 A CommunicatiePresentatie 3 A Communicatie
Presentatie 3 A Communicatie
 
The association between gut microbiota and body weight
The association between gut microbiota and body weightThe association between gut microbiota and body weight
The association between gut microbiota and body weight
 
Màrqueting Mix. Preu i distribució
Màrqueting Mix. Preu i distribucióMàrqueting Mix. Preu i distribució
Màrqueting Mix. Preu i distribució
 
Labor Heidrich Zeugnis English Translation
Labor Heidrich Zeugnis English TranslationLabor Heidrich Zeugnis English Translation
Labor Heidrich Zeugnis English Translation
 
Encuentro1 Actividad 1
Encuentro1 Actividad 1Encuentro1 Actividad 1
Encuentro1 Actividad 1
 
Halloween Horror Show
Halloween Horror ShowHalloween Horror Show
Halloween Horror Show
 
Pilot selection anthropometry a comparison with measures taken by a single a...
Pilot selection anthropometry  a comparison with measures taken by a single a...Pilot selection anthropometry  a comparison with measures taken by a single a...
Pilot selection anthropometry a comparison with measures taken by a single a...
 
Surgical workload at the role 3 multinational medical unit, kandahar airfield...
Surgical workload at the role 3 multinational medical unit, kandahar airfield...Surgical workload at the role 3 multinational medical unit, kandahar airfield...
Surgical workload at the role 3 multinational medical unit, kandahar airfield...
 
Web User Experience in 2021
Web User Experience in 2021Web User Experience in 2021
Web User Experience in 2021
 
სხვიტორი-სავანე
სხვიტორი-სავანესხვიტორი-სავანე
სხვიტორი-სავანე
 
Guy Danon Beyond 2010
Guy Danon Beyond 2010Guy Danon Beyond 2010
Guy Danon Beyond 2010
 
Darbo kodeksas
Darbo kodeksasDarbo kodeksas
Darbo kodeksas
 
Irregular verbs
Irregular verbsIrregular verbs
Irregular verbs
 
TeliaSonera
TeliaSoneraTeliaSonera
TeliaSonera
 
Yuqi-Resume
Yuqi-ResumeYuqi-Resume
Yuqi-Resume
 
Presentatie Nationaal Alumni Congres 04112010
Presentatie Nationaal Alumni Congres  04112010Presentatie Nationaal Alumni Congres  04112010
Presentatie Nationaal Alumni Congres 04112010
 
Slidesnotes
SlidesnotesSlidesnotes
Slidesnotes
 
CartaTrabajoCancilléria
CartaTrabajoCancillériaCartaTrabajoCancilléria
CartaTrabajoCancilléria
 

Similar a ITU - MDD - XText

Combinators, DSLs, HTML and F#
Combinators, DSLs, HTML and F#Combinators, DSLs, HTML and F#
Combinators, DSLs, HTML and F#
Robert Pickering
 
An Annotation Framework for Statically-Typed Syntax Trees
An Annotation Framework for Statically-Typed Syntax TreesAn Annotation Framework for Statically-Typed Syntax Trees
An Annotation Framework for Statically-Typed Syntax Trees
Ray Toal
 
Designing A Syntax Based Retrieval System03
Designing A Syntax Based Retrieval System03Designing A Syntax Based Retrieval System03
Designing A Syntax Based Retrieval System03
Avelin Huo
 
Introduction To Csharp
Introduction To CsharpIntroduction To Csharp
Introduction To Csharp
sarfarazali
 
Introduction to c#
Introduction to c#Introduction to c#
Introduction to c#
singhadarsh
 

Similar a ITU - MDD - XText (20)

Xml and Co.
Xml and Co.Xml and Co.
Xml and Co.
 
Xml session
Xml sessionXml session
Xml session
 
Introduction to XML
Introduction to XMLIntroduction to XML
Introduction to XML
 
Domänenspezifische Sprachen mit Xtext
Domänenspezifische Sprachen mit XtextDomänenspezifische Sprachen mit Xtext
Domänenspezifische Sprachen mit Xtext
 
Linq in C# 3.0: An Overview
Linq in C# 3.0: An OverviewLinq in C# 3.0: An Overview
Linq in C# 3.0: An Overview
 
Combinators, DSLs, HTML and F#
Combinators, DSLs, HTML and F#Combinators, DSLs, HTML and F#
Combinators, DSLs, HTML and F#
 
Xml and DTD's
Xml and DTD'sXml and DTD's
Xml and DTD's
 
XPath Injection
XPath InjectionXPath Injection
XPath Injection
 
An Annotation Framework for Statically-Typed Syntax Trees
An Annotation Framework for Statically-Typed Syntax TreesAn Annotation Framework for Statically-Typed Syntax Trees
An Annotation Framework for Statically-Typed Syntax Trees
 
O9schema
O9schemaO9schema
O9schema
 
Schema
SchemaSchema
Schema
 
Static abstract members nelle interfacce di C# 11 e dintorni di .NET 7.pptx
Static abstract members nelle interfacce di C# 11 e dintorni di .NET 7.pptxStatic abstract members nelle interfacce di C# 11 e dintorni di .NET 7.pptx
Static abstract members nelle interfacce di C# 11 e dintorni di .NET 7.pptx
 
Designing A Syntax Based Retrieval System03
Designing A Syntax Based Retrieval System03Designing A Syntax Based Retrieval System03
Designing A Syntax Based Retrieval System03
 
Introduction To Csharp
Introduction To CsharpIntroduction To Csharp
Introduction To Csharp
 
Introduction to c#
Introduction to c#Introduction to c#
Introduction to c#
 
XPath - XML Path Language
XPath - XML Path LanguageXPath - XML Path Language
XPath - XML Path Language
 
C# programming
C# programming C# programming
C# programming
 
A Recommender System for Refining Ekeko/X Transformation
A Recommender System for Refining Ekeko/X TransformationA Recommender System for Refining Ekeko/X Transformation
A Recommender System for Refining Ekeko/X Transformation
 
Grammarware Memes
Grammarware MemesGrammarware Memes
Grammarware Memes
 
Unit 5 xml (1)
Unit 5   xml (1)Unit 5   xml (1)
Unit 5 xml (1)
 

Más de Tonny Madsen

EclipseCon '08 - BoF - Building a local Eclipse user group
EclipseCon '08 - BoF - Building a local Eclipse user groupEclipseCon '08 - BoF - Building a local Eclipse user group
EclipseCon '08 - BoF - Building a local Eclipse user group
Tonny Madsen
 

Más de Tonny Madsen (20)

L0043 - Interfacing to Eclipse Standard Views
L0043 - Interfacing to Eclipse Standard ViewsL0043 - Interfacing to Eclipse Standard Views
L0043 - Interfacing to Eclipse Standard Views
 
L0037 - Basic Eclipse Configuration
L0037 - Basic Eclipse ConfigurationL0037 - Basic Eclipse Configuration
L0037 - Basic Eclipse Configuration
 
L0036 - Creating Views and Editors
L0036 - Creating Views and EditorsL0036 - Creating Views and Editors
L0036 - Creating Views and Editors
 
L0033 - JFace
L0033 - JFaceL0033 - JFace
L0033 - JFace
 
L0020 - The Basic RCP Application
L0020 - The Basic RCP ApplicationL0020 - The Basic RCP Application
L0020 - The Basic RCP Application
 
L0018 - SWT - The Standard Widget Toolkit
L0018 - SWT - The Standard Widget ToolkitL0018 - SWT - The Standard Widget Toolkit
L0018 - SWT - The Standard Widget Toolkit
 
L0016 - The Structure of an Eclipse Plug-in
L0016 - The Structure of an Eclipse Plug-inL0016 - The Structure of an Eclipse Plug-in
L0016 - The Structure of an Eclipse Plug-in
 
L0001 - The Terminology of the Eclipse Platform
L0001 - The Terminology of the Eclipse PlatformL0001 - The Terminology of the Eclipse Platform
L0001 - The Terminology of the Eclipse Platform
 
EclipseCon '11 - Using Adapters to Handle Menus and Handlers in Large Scale A...
EclipseCon '11 - Using Adapters to Handle Menus and Handlers in Large Scale A...EclipseCon '11 - Using Adapters to Handle Menus and Handlers in Large Scale A...
EclipseCon '11 - Using Adapters to Handle Menus and Handlers in Large Scale A...
 
PROSA - Eclipse Is Just What?
PROSA - Eclipse Is Just What?PROSA - Eclipse Is Just What?
PROSA - Eclipse Is Just What?
 
Eclipse Demo Camp 2010 - Eclipse e4 – The Status and the Future
Eclipse Demo Camp 2010 - Eclipse e4 – The Status and the FutureEclipse Demo Camp 2010 - Eclipse e4 – The Status and the Future
Eclipse Demo Camp 2010 - Eclipse e4 – The Status and the Future
 
Eclipse Demo Camp 2010 - UI Bindings - An Introduction
Eclipse Demo Camp 2010 - UI Bindings - An IntroductionEclipse Demo Camp 2010 - UI Bindings - An Introduction
Eclipse Demo Camp 2010 - UI Bindings - An Introduction
 
ITU - MDD – Model-to-Model Transformations
ITU - MDD – Model-to-Model TransformationsITU - MDD – Model-to-Model Transformations
ITU - MDD – Model-to-Model Transformations
 
IDA - Eclipse Workshop II (In Danish)
IDA - Eclipse Workshop II (In Danish)IDA - Eclipse Workshop II (In Danish)
IDA - Eclipse Workshop II (In Danish)
 
IDA - Eclipse Workshop I (In Danish)
IDA - Eclipse Workshop I (In Danish)IDA - Eclipse Workshop I (In Danish)
IDA - Eclipse Workshop I (In Danish)
 
IDA - Fra forretningside til bundlinie: Eclipse følger dig hele vejen (In Dan...
IDA - Fra forretningside til bundlinie: Eclipse følger dig hele vejen (In Dan...IDA - Fra forretningside til bundlinie: Eclipse følger dig hele vejen (In Dan...
IDA - Fra forretningside til bundlinie: Eclipse følger dig hele vejen (In Dan...
 
ITU - MDD - EMF
ITU - MDD - EMFITU - MDD - EMF
ITU - MDD - EMF
 
ITU - MDD - Eclipse Plug-ins
ITU - MDD - Eclipse Plug-insITU - MDD - Eclipse Plug-ins
ITU - MDD - Eclipse Plug-ins
 
eclipse.dk - Eclipse RCP Under the Hood
eclipse.dk - Eclipse RCP Under the Hoodeclipse.dk - Eclipse RCP Under the Hood
eclipse.dk - Eclipse RCP Under the Hood
 
EclipseCon '08 - BoF - Building a local Eclipse user group
EclipseCon '08 - BoF - Building a local Eclipse user groupEclipseCon '08 - BoF - Building a local Eclipse user group
EclipseCon '08 - BoF - Building a local Eclipse user group
 

Último

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
MateoGardella
 

Último (20)

Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
 

ITU - MDD - XText

  • 1. L0081 - 2010-11-08 Redistribution and other use of this material requires written permission from The RCP Company. ITU - MDD – XText This presentation describes the use of XText. This presentation assumes a good knowledge of Data Modeling and Grammars as previously presented. This presentation is developed for MDD 2010 course at ITU, Denmark.
  • 2. L0081 - 2010-11-08 2  Parse trees are very detailed:  every step in a derivation is a node  After the parsing phase is done, the details of derivation are not needed for later phases  Semantic Analyzer removes intermediate productions to create an (abstract) syntax tree – known as an AST expr term factor ID: x expr ID: x Parse Tree: Abstract Syntax Tree: Abstract Syntax Trees
  • 3. L0080 - 2010-11-08 3 Parsing an Expression assignment => ID “=“ expression; expression => expression “+” term | expression “-” term | term term => term “*” factor | term “/” factor | factor factor => “(“ expression “)” | ID | NUMBER ID ~ y expresssion ~ (2*x + 5)*x - 7 expression ~ (2*x + 5)*x term ~ 7 assignment ~ y = (2*x + 5)*x - 7 factor ~ 7term ~ (2*x + 5)*x NUMBER ~ 7term ~ (2*x + 5) factor ~ x ID ~ xfactor ~ (2*x + 5) expression ~ 2*x + 5 expression ~ 2*x term ~ 5 factor ~ 5term ~ 2*x NUMBER ~ 5factor ~ xterm ~ 2 ID ~ x factor ~ 2 NUMBER ~ 2
  • 4. L0081 - 2010-11-08 4 Building a AST assignment => ID “=“ expression; expression => expression “+” term | expression “-” term | term term => term “*” factor | term “/” factor | factor factor => “(“ expression “)” | ID | NUMBER ID ~ y expresssion ~ (2*x + 5)*x - 7 expression ~ (2*x + 5)*x term ~ 7 assignment ~ y = (2*x + 5)*x - 7 factor ~ 7term ~ (2*x + 5)*x NUMBER ~ 7term ~ (2*x + 5) factor ~ x ID ~ xfactor ~ (2*x + 5) expression ~ 2*x + 5 expression ~ 2*x term ~ 5 factor ~ 5term ~ 2*x NUMBER ~ 5factor ~ xterm ~ 2 ID ~ x factor ~ 2 NUMBER ~ 2 Assignment(y) Expression(-) Expression(*) NUMBER(7) Expression(+) Expression(*) NUMBER(2) NUMBER ID(x) ID(x)
  • 5. L0081 - 2010-11-08 5 Level of Details  How detailed should a grammar be?  Should the grammar be rich – i.e. contain all details?  Or should the grammar be as thin as possible?  In general use a thin grammar to avoid too many keywords  For XText, use a rich grammar as this is used to provide context assist  Other similar questions:  Should you model dates?  How about ranges? Flight : 'flight' ID '{' ( ('from'|'to') '=' STRING ';' )* '}' Flight : 'flight' ID '{' ( ID '=' STRING ';' )* '}'
  • 6. L0081 - 2010-11-08 6 Line feed or Explicit Terminators  Should you have an explicit “statement” terminator or use line feeds?  Statement Terminator such as “;” or “.”  Pro: much easier to detect errors in the input  Con: normally not natural for the user  Statement Terminator such as line feed  Pro: much easier to detect errors in the input  Con: cannot divide a logical line over multiple physical lines  No Statement Terminators  Pro: very natural for many users  Con: difficult to detect errors in the input  In general use a statement terminator – not a statement separator!  In our scenario, it properly makes good sense to use line feeds  And indentions for scoping
  • 7. L0081 - 2010-11-08 7 What is XText exactly  Xtext is a complete environment for development of textual programming languages and domain-specific languages.  It is implemented in Java and is based on Eclipse, EMF, and Antlr.  The Basic Idea?  Augment an EBNF grammar  Roll it through XText  Have an Eclipse Editor  The XText tool bench  Grammar is defined in an EBNF-like format in the Xtext editor.  The editor provides code completion and constraint checking for the grammars themselves  Grammar is a collection of Rules. Rules start with their name followed by “:” and ending with “;”
  • 8. L0081 - 2010-11-08 8 Getting Started with XText  Create a new XText project with the New… wizard
  • 9. L0081 - 2010-11-08 9 The XText File Structure  Three projects!  You can make changes to the files in the src files, but not the files in the src-gen folders Your grammar rules Workflow description used to create editor Generated ECore model Generated ECore classes
  • 10. L0081 - 2010-11-08 10 An XText Model File (.xtext)  Identify of model  Include of base declarations and terminals  Directive to create model  NS URI of model  It is also possible to import an existing model grammar org.xtext.example.MyDsl with org.eclipse.xtext.common.Terminals generate myDsl "http://www.xtext.org/example/MyDsl" Model : Type*; …
  • 11. L0081 - 2010-11-08 11 Generating Files from a Model  To generate an Eclipse editor from a a model (.xtext)  Select the workflow file (.mwe2)  Use “Run As…”  “MWE2 WorkFlow”
  • 12. L0081 - 2010-11-08 12 The Basic XText Concepts  The AST is a ECore model  The rules of the input grammar are used to create the AST (more or less automatically) or interface to an existing imported model (=AST)  So we must identify  The entities of the model  The attributes of the model  The relations – containment and references – of the model  The input language of XText is a augmented EBNF (almost )
  • 13. L0081 - 2010-11-08 13 Example  Grammar for a (very) simple type system Model : Type*; Type: SimpleType | Entity; SimpleType: ‘datatype' ID; Entity : 'entity' ID ('extends' ID)? '{' Property* '}'; Property: 'property' ID ':' ID ('[]')?; datatype A datatype B entity E { property a : A property b : B[] } entity F extends E { property c : A } Model Type SimpleType Entity Property * * Super-type
  • 14. L0081 - 2010-11-08 14  XText rules are EBNF plus type information  Type Rules  For each rule XText creates an entity in the logical model  Each rule property results in a property in the logical model  String Rules  String rules are parsed to be a string  These are in effect custom lexer rules as they recognize string patterns  Enum Rules  Limited set of alternatives  Mapped to an enumeration in the logical model  Native Rules  A lexer rule that is mapped directly to an ANTLR rule  Mapped to a String Different Kinds of XText Rules
  • 15. L0081 - 2010-11-08 15  Class of type rule defaults to name of rule   Can be overruled via “{ClassName}” construct  A type rule can be abstract  An abstract type rule is basically a collection of OR-ed alternatives: R1 | R2 | R3  Mapped to an abstract metaclass  The or-ed alternatives become concrete subclasses  Common properties of the alternatives are lifted into the abstract superclass Abstract Type Rules Model : Type*; Type: SimpleType | Entity; SimpleType: ‘datatype' ID; Entity : 'entity' ID ('extends' ID)? '{' Property* '}'; Property: 'property' ID ':' ID ('[]')?;
  • 16. L0081 - 2010-11-08 16 Example with Type Rules  Every rule corresponds to en entity  “Or” rules becomes abstract classes with the entities of the sub-rules as child classes  Above we have Type as an abstract super class for SimpleType and Entity Model : Type*; Type: SimpleType | Entity; SimpleType: 'type' ID; Entity : 'entity' ID ('extends' ID)? '{' Property* '}'; Property: 'property' ID ':' ID ('[]')?;
  • 17. L0081 - 2010-11-08 17 Some built-in String Rules (Terminals)  The definition of the central terminals  Really part of lexical analysis!  Specific terminals can be “hidden” in the grammar or for specific rules, meaning they are recognized but ignored during parsing…  Very useful if you want a line-oriented grammar terminal ID : '^'?('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'_'|'0'..'9')*; terminal INT returns ecore::EInt: ('0'..'9')+; terminal STRING: '"' ( '' ('b'|'t'|'n'|'f'|'r'|'"'|"'"|'') | !(''|'"') )* '"' | "'" ( '' ('b'|'t'|'n'|'f'|'r'|'"'|"'"|'') | !(''|"'") )* "'" ; terminal ML_COMMENT: '/*' -> '*/'; terminal SL_COMMENT : '//' !('n'|'r')* ('r'? 'n')?; terminal WS : (' '|'t'|'r'|'n')+;
  • 18. L0081 - 2010-11-08 18  A type rule has a rule composition – made of a number of elements  It may contain keywords (using string literal syntax)  It also contains properties which will result in properties of the type class  The property type is derived from the called rule  There are different kinds of properties  = (single assign)  += (multiple assign/add)  ?= (boolean assign)  There are different property cardinalities  ? (0..1)  * (0..n)  + (1..n)  nothing (1..1) Properties (Attributes and Relations) in Type Rules Entity : 'entity' name=ID ('extends' extends=ID)? '{' properties+=Property* '}'; Property: 'property' name=ID ':' type=ID (many?='[]')?; Results in containment relation “List<Property> properties” Results in simple attribute “String: name”
  • 19. L0081 - 2010-11-08 19 Example of Properties  Simple property:  name of SimpleType  “Many” reference – containment  elements of Model  properties of Entity  Boolean property  many of Property Model : (elements+=Type)*; Type: SimpleType | Entity; SimpleType: 'type' name=ID; Entity : 'entity' name=ID ('extends' extends=ID)? '{' properties+=Property* '}'; Property: 'property' name=ID ':' type=ID (many?='[]')?;
  • 20. L0081 - 2010-11-08 20  By default, rules results in a logical model with a tree of entities via containment  You can reference other elements via a reference rule  In textual languages a reference has to be by name  During linking, Xtext “dereferences” these by name-references  Why References?  Used in the logical model for references  XText also uses references for context assist Reference Relations Entity : 'entity' name=ID ('extends' extends=[Entity])? '{' properties+=Property* '}'; Results in containment relation “List<Property> properties” Results in reference relation “Entity extends”
  • 21. L0081 - 2010-11-08 21 Example of References  extends of Entity  type of Property Model : (elements+=Type)*; Type: SimpleType | Entity; SimpleType: 'type' name=ID; Entity : 'entity' name=ID ('extends' extends=[Entity])? '{' properties+=Property* '}'; Property: 'property' name=ID ':' type=[Type] (many?='[]')?;
  • 22. L0081 - 2010-11-08 22  A Enum Rule is used to define a limited set of defined alternatives  It is mapped to an enumeration in the logical model  It is declared via the Enum keyword and contains Enum Literals  An Enum Literal has a token name and a string representation  It can be used just like any other rule  Properties will get the enumeration type Enumeration Rules enum Color: RED=“red” | GREEN=“green” | BLUE=“blue” ; Shape: name=ID ( ‘color’ color=Color)? … ;
  • 23. L0081 - 2010-11-08 23  A native rule contains a string which is passed to ANTLR without further processing it.  It is typically used to define lexer rules that cannot be expressed using Xtext syntax  E.g. whitespace-aware lexer rules, such as define custom comment syntax Native Rules
  • 24. L0081 - 2010-11-08 24 Importing an Existing Model  To import an model instead of generating it..  Simple change to the xtext grammar file:  Depending on the starting point a simple change to the MWE2 file:  Not so with the generated MWE2 file   Easier to start with the wizard “XText Project from existing ECore Models” grammar com.rcpcompany.mdd2010.DSL with org.eclipse.xtext.common.Terminals import "platform:/resource/com.rcpcompany.mdd2010.model/model/Travel.ecore" import "http://www.eclipse.org/emf/2002/Ecore" as ecore … fragment = org.eclipse.xtext.generator.ecore.EcoreGeneratorFragment { genModels = "platform:/resource/com.rcpcompany.mdd2010.model/model/Travel.genmodel" }
  • 25. L0081 - 2010-11-08 25 More Information XText and friends  “Build your own textual DSL with Tools from the Eclipse Modeling Project”  http://www.eclipse.org/articles/article.php?file=Article-BuildYourOwnDSL/index.html  Older, slightly out-dated, article on how to create your first XText project  Documentation for XText  http://www.eclipse.org/Xtext/documentation/
  • 26. L0081 - 2010-11-08 26 Exercise 1  Make sure you grammar is on proper EBNF form  Do you think you can make an interesting editor from the grammar?  Create an XText project if not already done  Convert your EBNF grammar into an XText model  What can you do to your grammar to make it more suitable for XText