DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. Building a Compiler: Model-to-Model Transformations

Building a Compiler: Model-to-Model Transformations

DSLs have become commonplace tools today. Many of them are developed as internal DSLs in languages like Ruby, but this can be too unsafe for open environments. If you need to develop an external DSL, you'll need to build a compiler or interpreter.

Federico Tomassetti user avatar by
Federico Tomassetti
·
Sep. 03, 16 · Tutorial
Like (7)
Save
Tweet
Share
20.92K Views

Join the DZone community and get the full member experience.

Join For Free

Most of the work done in tools supporting a language consists in manipulating the AST. In this post, we are going to see how to perform transformation and processing on the Abstract Syntax Tree through model-to-model transformations. These techniques will be useful to perform operations like:

  • Validation: finding errors in the AST.
  • Remove syntax sugar: by transforming the AST into equivalent but more explicit forms.
  • Fill symbol tables and resolve symbols.

All these techniques will be based on generic ways to navigate and change the AST. Let’s see how.

Series On Building Your Own Language

Previous posts:

  1. Building a lexer
  2. Building a parser
  3. Creating an editor with syntax highlighting
  4. Build an editor with autocompletion
  5. Mapping the parse tree to the abstract syntax tree

Code is available on GitHub under the tag 06_transformations

The Operations We Need

We will need to:

  • Process the AST: we want to do some operation to extract information from the AST
  • Transform the AST: we want to transform the single nodes of the AST

Implement the Transformations Manually

We could implement the transformation manually on each single class of the AST. For example, this is how we could implement these methods on a SumExpression

fun SumExpression.process(operation: (Node) -> Unit) {
    operation.invoke(this)
    this.left.process(operation)
    this.right.process(operation)
}

fun SumExpression.transform(operation: (Node) -> Node) : Node {
    val newLeft = this.left.transform(operation) as Expression
    val newRight = this.right.transform(operation) as Expression
    return operation(SumExpression(newLeft, newRight))
}

We would have just to repeat it for each single class of our metamodel. Quite boring, eh?

Transformations Using Reflection

The alternative is to use reflection. In this way we can specify these operations for Node and they will work for every class of every metamodel (so basically, for every language we are going to work on).

fun Node.process(operation: (Node) -> Unit) {
    operation(this)
    this.javaClass.kotlin.memberProperties.forEach { p ->
        val v = p.get(this)
        when (v) {
            is Node -> v.process(operation)
            is Collection<*> -> v.forEach { if (it is Node) it.process(operation) }
        }
    }
}

fun Node.transform(operation: (Node) -> Node) : Node {
    operation(this)
    val changes = HashMap<String, Any>()
    this.javaClass.kotlin.memberProperties.forEach { p ->
        val v = p.get(this)
        when (v) {
            is Node -> {
                val newValue = v.transform(operation)
                if (newValue != v) changes[p.name] = newValue
            }
            is Collection<*> -> {
                val newValue = v.map { if (it is Node) it.transform(operation) else it }
                if (newValue != v) changes[p.name] = newValue
            }
        }
    }
    var instanceToTransform = this
    if (!changes.isEmpty()) {
        val constructor = this.javaClass.kotlin.primaryConstructor!!
        val params = HashMap<KParameter, Any?>()
        constructor.parameters.forEach { param ->
            if (changes.containsKey(param.name)) {
                params[param] = changes[param.name]
            } else {
                params[param] = this.javaClass.kotlin.memberProperties.find { param.name == it.name }!!.get(this)
            }
        }
        instanceToTransform = constructor.callBy(params)
    }
    return operation(instanceToTransform)
}

Testing the Transformations

It all looks nice and well but the question is: does it actually work? Let’s try it.

In this little test we will transform references to a variable A into references to a variable B.

class ModelTest {

    @test fun transformVarName() {
        val startTree = SandyFile(listOf(
                VarDeclaration("A", IntLit("10")),
                Assignment("A", IntLit("11")),
                Print(VarReference("A"))))
        val expectedTransformedTree = SandyFile(listOf(
                VarDeclaration("B", IntLit("10")),
                Assignment("B", IntLit("11")),
                Print(VarReference("B"))))
        assertEquals(expectedTransformedTree, startTree.transform {
            when (it) {
                is VarDeclaration -> VarDeclaration("B", it.value)
                is VarReference -> VarReference("B")
                is Assignment -> Assignment("B", it.value)
                else -> it
            }
        })
    }

}

Conclusions

Another building block for the future. In the next steps we will see how to use this, for example to implement validation.

Abstract syntax Syntax (programming languages) Syntax highlighting Tree (data structure) POST (HTTP) Testing Blocks GitHub

Published at DZone with permission of Federico Tomassetti, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Too Many Tools? Streamline Your Stack With AIOps
  • Java Development Trends 2023
  • Deploying Java Serverless Functions as AWS Lambda
  • Automated Performance Testing With ArgoCD and Iter8

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: