Document directory
- Model-driven code generation
- Code Generation Technology
- Code Generation Definition Language
- Books
In implementationModel-driven developmentIn, we can explain that the model runs directly on the domain framework, or convert the model into code through the Code Generation Technology and compile and run on the framework. Both methods have advantages and disadvantages and can be used together.OpenexpessappThese two methods will be used. The class library is generated by code, and the meta model such as UI is interpreted and executed through the framework. Since code generation is an important technique in MDD, I will introduce the knowledge of code generation in this article.
Model-driven code generation
In EMF, metamodel is the EMF meta model and the model is the EMF model. In Ms DSL tools, metamodel is the DSL meta model and the template is the T4 template.
Code Generation Technology
- Antl3
Anlr is a well-known open-source language generation tool, which is introduced on the official website:
Anlr, Another tool for language recognition, is a required agetool that provides a framework for constructing recognizers, interpreters, compilers, and translators from grammatical descriptionscontaining actions in a varietyTargetages. Anlr provides excellent support for treeconstruction, tree walking, translation, error recovery, and errorreporting. There are currently about5,000Anlr sourcedownloads a month.
After studying for a while, anlr used it to write an Expression Engine under. net. For details, refer to implementing the Expression Engine Using anlr.
Generally, the following steps are taken to use anlr:
- Write syntax
- Write stringtemplate template (optional)
- Debug syntax in antlworks
- Generate classes from Syntax
- Use generated classes to write applications
The author has written a monograph about the definitive anlr reference, which I have read about a few years ago. However, some people say this book is better, and it also gives a clear idea of the internal design of anlr. In comparison, the previous book can only be regarded as the anlr User Manual, while the new book is regarded as the anlr design idea.
Irony-. NET language implementation kitIrony is an open-source language implementation tool on the codeplex website. The official introduction is as follows:
IronyIs a development kit for implementing languages on. NET platform. it uses the flexibility and power of C # language and. net Framework 3.5 to implement a completely new and streamlined Technology of Compiler Construction. unlike most existing YACC/Lex-style solutions Irony does not employ any errors or parser code generation from grammar specifications written in a specialized meta-language. in irony the target language grammar is coded directly in C # using operator overloading to express grammar constructs. irony's role and parser modules use the grammar encoded as C # class to control the parsing process. see the expression grammar sample for an example of grammar definition in C # class, and using it in a working parser.
......
Code Generation Definition Language
InIntroduction to Specific Domain Modeling (DSM)This section describes the comparison of different DSM tools. The definition language of code generation can also be seen in the table, the following describes the DSL tools T4 and metaedit + Merl (metaedit + reporting language ):
- Metaedit + Merl Language
Metaedit + provides the code generation editor and supports generated debugging.OpenexpressappYou can use metaedit +-like metaedit meta-model to compile a language. Therefore, you may refer to metaedit for future code generation. For more information, seeUser's GuidesOfMetaedit + workbenchCode Generation chapter content.
It provides a report example:
Report 'Test' foreach .State [Watch] { 'State : ' :State name; newline 'Connects to: ' newline do ~From>()~To.State [Watch] { ' ' :State name; newline } newline } endreport
Its syntax is defined as follows:
STRING: "'" CHAR* "'"where CHAR is any character; a ' character must be doubledNUMBER: ("0".."9")+NAMECHAR: "a".."z" | "0".."9" | " " | {_+-[]?} | INTLNAMECHAR | ESCAPECHARINTLNAMECHAR: {äëïöü} | {áéíóú} | {àèìòù} | {âêîôû} | {ñãœçÿ} | {߀} | {¿¡«»}ESCAPECHAR: "\" ECHARwhere ECHAR is anything that is not a letter number or underscoreNAME: NAMECHAR+If NAME contains a space, the whole name should have a ";" after it, or one of ".>~#" forming the start of the next element in a chainClauseWILDNAME: ["^"] (NAMECHAR | "*" | "#")+If WILDNAME contains a space, the whole name should have a ";" after it.# Design element access and output commands (5.3.2)chainOutputClause: (propClauseWithLevel | propClause | (graphEltClause+ propClause)) [";"] [translatorNames] [";"];translatorNames: ("%" <NAMECHAR>+)+;propClauseWithLevel: propClause levelNumber [";"];levelNumber: [";"] " "* ["-"] <NUMBER>+;propClause: ":" (<NAME> | "()");graphEltClause: (objClause | relClause | roleClause | portClause) [";"]objClause: "." typeChoiceClauserelClause: ">" typeChoiceClauseroleClause: "~" typeChoiceClauseportClause: "#" typeChoiceClausetypeChoiceClause: NAME| "()"| "( " WILDNAME {" | " WILDNAME}* ")"# General commands (5.3.3)report: oldreport | newreport;oldreport: "report" <STRING> clause* "endreport";newreport: [newheadersection] clause*;newheadersection: <NAME> ["(" [<NAME> ("," <NAME>)*] ")" ];clause: (comment| basicClause| ifClause| loop| subreportClause| fileClause| md5Clause| executeClause| promptAskClause| variableAssign| variableClause| translationClause| mathClause| chainOutputClause;comment: <comment>;basicClause: atomicClause | iterableClause;atomicClause: newlineClause | separatorClause | literal | variableRef | simpleClause;newlineClause: "newline" [";"];separatorClause: "sep" [";"];literal: <STRING> [translatorNames] [";"];variableRef : "$" <NAME> [translatorNames] [";"];simpleClause: ("id" | "type" | "metatype" | "oid" | "projectid" | "objectid" | "project") [levelNumber] [";"] [translatorNames] [";"]|("x" | "y" | "left" | "right" | "top" | "bottom" | "centerX" | "centerY" | "width" | "height" | "area") [levelNumber] [";"] [translatorNames] [";"];iterableClause: ("decompositions" | "explosions" | "containers" | "contents" | "stack" | "graphs") [";"];# Control and navigation commands (5.3.4)ifClause: "if" [condition]"then" [";"] (clause* | ";")["else" [";"] clause*]"endif" [";"];condition: ("not" condition)| (condition "and" condition)| (condition "or" condition)| ("(" condition ")")| expression;expression: comparison | unary;unary: comparableClause;comparison: comparableClause comp comparableClause["num"];comparableClause : atomicClause | chainClause;comp: "<" | ">" | "<=" | ">=" | "=" | "<>" | "=~" | "=/";loop: ("do" | "dowhile")(chainClause | atomicClause)[whereClause] [filterClause]"{" clause* "}" [";"]|"foreach"graphEltClause [";"][whereClause] [filterClause]"{" clause* "}" [";"];chainClause: (chainElementClause [levelNumber] [";"])+;chainElementClause: graphEltClause | propClause | iterableClause;whereClause: "where" condition;filterClause: orderbyClause [uniqueClause]| uniqueClause;orderbyClause : "orderby" orderCriterion ("," orderCriterion)*;uniqueClause : "unique" [clause+ ("," clause+)*];orderCriterion: clause+ ["num"] ["asc" | "desc"];subreportClause: ("subreport" | "subgenerator") [";"] clause* "run" [";"];# External I/O commands (5.3.5)fileClause: outputFileClause | filenameReadClause | filenamePrintClause;outputFileClause: "filename" [";"] clause*["encoding" [";"] clause+]["md5start" [";"] clause+]["md5stop" [";"] clause+]modeClause clause*"close" [";"];modeClause: ("write" | "merge" | "append") [";"];filenameReadClause: "filename" [";"] clause*["encoding" [";"] clause+]"read" [";"];filenamePrintClause: "filename" [";"] clause* "print" [";"];md5Clause: "md5id" [";"] clause* "md5Block" [";"]clause*"md5sum" [";"];executeClause: ("external" | "internal") [";"]clause*("execute" | "executeBlocking") [";"];promptAskClause: "prompt" [";"] clause* "ask" [";"];# String and number commands (5.3.6)variableClause: variableReadClause | variableWriteClause;variableReadClause: "variable" [";"] clause+ "read" [";"];variableWriteClause: "variable" [";"] clause+variableModeClause clause* [";"]"close" [";"];variableModeClause: ("write" | "append") [";"];variableAssign: "$" <NAME> "=" [";"] (variableAssign | basicClause | chainOutputClause);translationClause: "to" [";"] clause*["translate" [";"] clause*]"endto" [";"];mathClause: "math" [";"] clause* "evaluate" [";"];
- Microsoft's T4
T4: Text Template transformation Toolkit
Books
What's inside:
- Code Generation Basics
- CG techniques and best practices
- Patterns of CG Design
- How to deploy generators
- Extends example generators
Except des generators:
- Database Access
- RPC
- Unit Tests
- Documentation
- Business Logic
- Data Translation
Lop code generation
There are three main methods to generate code in Lop. We will use them together to define model conversion. The first method is traversal. You can enumerate all nodes in the source model and view each of them, some target nodes in the target model are generated based on the viewed information. The second method is to use templates and macros to define how to generate the target language; the third method is to use pattern matching to find which nodes in the source model apply the conversion.
Code Generation by model transformation-A Case Study in transformation modularity
Recommended: online e-books you may need
You are welcome to reprint it. Please note: Reprinted fromZhou jingen [http://zhoujg.cnblogs.com/]