grammars and meta models grammars and meta models
play

Grammars and meta-models Grammars and meta-models Assignments are - PowerPoint PPT Presentation

Grammars and meta-models Grammars and meta-models Assignments are used to assign the parsed There are three different assignment operators, each g g g information to a feature of the current object. with different semantics: The


  1. Grammars and meta-models Grammars and meta-models • Assignments are used to assign the parsed • There are three different assignment operators, each g g g information to a feature of the current object. with different semantics: • The simple equal sign '=' is the straight forward • The type of the current object, its EClass , is assignment, and used for features which take only one assignment and used for features which take only one specified by the return type of the parser rule . specified by the return type of the parser rule element • Example: • The '+=' sign (the add operator) expects a multi-valued State : f feature and adds the value on the right hand to that t d dd th l th i ht h d t th t 'state' name=ID feature, which is a list feature ('actions' '{' (actions+=[Command])+ '}')? • The '?=' sign (boolean assignment operator) expects a (transitions+=Transition)* feature of type EBoolean and sets it to true if the right 'end' Attributes of State ; hand side was consumed independently from the concrete value of the right hand side g / Faculteit Wiskunde en Informatica / Faculteit Wiskunde en Informatica 29-11-2011 PAGE 0 29-11-2011 PAGE 1 Grammars and meta-models Grammars and meta-models • Extended Backus-Naur Form Expressions • Unordered Groups • Token rules are described using “Extended Backus-Naur • The elements of an unordered group can occur in any order Form”-like (EBNF) expressions but each element can occur at most once. • Unordered groups are separated with '&', e.g. Unordered groups are separated with & , e.g. Modifier: • There are four different possible cardinalities static?='static' & final?='final' & visibility=Visibility; 1. exactly one (the default, no operator) enum Visibility: 2. 2 one or none (operator ?) one or none (operator ?) PUBLIC='public' | PRIVATE='private‘ | PROTECTED='protected'; 3. any (zero or more, operator *) • allows 4. one or more (operator +) public static final static protected final private static public / Faculteit Wiskunde en Informatica 29-11-2011 PAGE 2 / Faculteit Wiskunde en Informatica 29-11-2011 PAGE 3

  2. Grammars and meta-models Grammars and meta-models • Context-free grammars are mapped to signatures • The Xtext specification for Booleans: g g • A signature describes the structure of abstract Model : OrBool ; syntax trees • A meta-model can also describe the structure of A t d l l d ib th t t f OrBool : lhs=AndBool ('|' rhs=OrBool)? ; abstract syntax trees, plus AndBool : lhs=NotBool ('&' rhs=AndBool)? ; • Relations between identifiers e at o s bet ee de t e s • Attributes to store scope information NotBool : (not?='~’)? arg=BracketBool ; • Attributes to store type information BracketBool : '(' orArg=OrBool ')' | conArg=BoolCon ; BoolCon : { TrueNode} 'true' | {FalseNode} 'false' ; / Faculteit Wiskunde en Informatica / Faculteit Wiskunde en Informatica 29-11-2011 PAGE 4 29-11-2011 PAGE 5 Grammars and meta-models Grammars and meta-models • Resulting meta-model (of syntax tree) for Booleans: • Resulting meta-model (of syntax tree) for Booleans: / Faculteit Wiskunde en Informatica 29-11-2011 PAGE 6 / Faculteit Wiskunde en Informatica 29-11-2011 PAGE 7

  3. Grammars and meta-models Grammars and meta-models • First alternative for Boolean meta-model • First alternative for Boolean meta-model / Faculteit Wiskunde en Informatica / Faculteit Wiskunde en Informatica 29-11-2011 PAGE 8 29-11-2011 PAGE 9 Grammars and meta-models Grammars and meta-models • SDF definition of toy language Pico • Xtext specification for Pico: y g g context-free syntax Model : Program ; "begin" DECLS {STATEMENT ";"}* "end" -> PROGRAM "declare" {ID-TYPE ","}* ";" -> DECLS Program : 'begin' decls=Decls? stats=Statements? 'end' ; PICO-ID ":" TYPE -> ID-TYPE PICO-ID ":=" EXP -> STATEMENT Decls : 'declare' idtypes=IdTypes ';' ; "if" EXP "then" {STATEMENT ";"}* "else" {STATEMENT ";"}* "fi" -> STATEMENT "while" EXP "do" {STATEMENT ";"}* "od“ -> STATEMENT IdTypes : pairs+=IdType (',' pairs+=IdType)*; yp p yp ( , p yp ) PICO-ID -> EXP NatCon -> EXP StrCon -> EXP IdType : name=ID ':' type=Type ; EXP "+" EXP -> EXP {left} EXP "-" EXP -> EXP {left} Type : { naturalType} natural | {stringType} string ; Type : { naturalType} 'natural' | {stringType} 'string' ; EXP "||" EXP EXP "||" EXP -> EXP {left} > EXP {l ft} "(" EXP ")" -> EXP {bracket} Statements : statements+=Statement ('; ' statements+=Statement)*; / Faculteit Wiskunde en Informatica 29-11-2011 PAGE 10 / Faculteit Wiskunde en Informatica 29-11-2011 PAGE 11

  4. Grammars and meta-models Grammars and meta-models • Xtext specification for Pico (continued): • Xtext specification for Pico (continued): Statement : AssignStatement | Exp : lhs=Term (bop=BinOp rhs=Exp)? ; IfStatement | WhileStatement ; Term : id= ID | predefined literal=STRING | AssignStatement : lhs= ID ':=' rhs=Exp ; number=INT | '(' Exp ')' ; IfStatement : 'if' Exp 'then' thenpart=Statements? 'else' p p elsepart=Statements? 'fi' ; BinOp: { plusOp} '+' | {minOp} '-' | WhileStatement : 'while' Exp 'do' dopart=Statements? 'od' ; {concOp} '||' ; / Faculteit Wiskunde en Informatica / Faculteit Wiskunde en Informatica 29-11-2011 PAGE 12 29-11-2011 PAGE 13 Grammars and meta-models Grammars and meta-models • Resulting meta-model (of syntax tree) for Pico: • Resulting meta-model (of syntax tree) for Pico: / Faculteit Wiskunde en Informatica 29-11-2011 PAGE 14 / Faculteit Wiskunde en Informatica 29-11-2011 PAGE 15

  5. Grammars and meta-models Grammars and meta-models • Resulting meta-model (of syntax tree) for Pico: • Xtext specification for Pico (with cross references): … AssignStatement : lhs=[ IdType|ID] ':=' rhs=Exp ; C Cross reference … Term : id=[ IdType|ID] | literal=STRING | number=INT | '(' Exp ')' ; [ yp | ] | | | ( p ) … / Faculteit Wiskunde en Informatica / Faculteit Wiskunde en Informatica 29-11-2011 PAGE 16 29-11-2011 PAGE 17 Grammars and meta-models Grammars and meta-models • Xtext specification for Pico: • Xtext offers • “built-in” cross reference mechanism Model : Program ; • scoping mechanism via writing “simple” Java methods, see Program : 'begin' decls=Decls? stats=Statements? 'end' ; http://www.eclipse.org/Xtext/documentation/1_0_0/xtext.html#scoping http://www.eclipse.org/Xtext/documentation/1 0 0/xtext.html#scoping Decls : 'declare' idtypes=IdTypes ';' ; • Xtext mixes in fact context-free parsing with some IdTypes : pairs+=IdType (',' pairs+=IdType)*; yp p yp ( , p yp ) f form semantic evaluation ti l ti IdType : name=ID ':' type=Type ; Type : { naturalType} natural | {stringType} string ; Type : { naturalType} 'natural' | {stringType} 'string' ; Statements : statements+=Statement ('; ' statements+=Statement)*; / Faculteit Wiskunde en Informatica 29-11-2011 PAGE 18 / Faculteit Wiskunde en Informatica 29-11-2011 PAGE 19

  6. Grammars and meta-models Grammars and meta-models • Conclusions on Xtext • EMFtext • popular • is tightly integrated with Eclipse Modeling Framework (EMF) • enables the definition of textual syntax for Ecore-based meta- • well integrated in Eclipse models models • suited for defining concrete syntax of new languages it d f d fi i t t f l • offers a Concrete Syntax Specification Language (CS) is • less suited for existing languages, because of LL class EBNF based • ANLTR based • ANLTR based • Documentation: http://www.emftext.org/EMFTextGuide.php / Faculteit Wiskunde en Informatica / Faculteit Wiskunde en Informatica 29-11-2011 PAGE 20 29-11-2011 PAGE 21 Grammars and meta-models Grammars and meta-models • EMFtext offers • Developing a language with EMFText is an iterative g g g process and consists of the following basic tasks: • modular specification: − import mechanism for various meta-models 1. specifying a language meta-model − modularization and extension of CS specifications modularization and extension of CS specifications 2. 2 specifying the Concrete Syntax of the language specifying the Concrete Syntax of the language • default reference resolving mechanisms 3. generating language tooling − default name resolution mechanism for models with globally 4. optionally customizing the language tooling p y g g g g unique names is available for any syntax q y y • comprehensive syntax analysis − analyses of CS specifications inform the developer about potential errors p / Faculteit Wiskunde en Informatica 29-11-2011 PAGE 22 / Faculteit Wiskunde en Informatica 29-11-2011 PAGE 23

Recommend


More recommend