Jordi
Jordi

Reputation: 23247

Semantic Model from a grammar

I would like ask for some thoughts about the concepts: Domain Object and a Semantic Model.

So, I really want to understand what's a Domain Object / Semantic Model for and what's not Domain Object / Semantic Model for.

As far I've been able to figure out, given a grammar is absolutly advisable do these separation concepts.

However, I'm not quite figure out how to do it. For example, given this slight grammar, how do you build a Domain Object or a Semantic Model.

It's exactly what I'm trying to figure out...

Most of books suggest this approach in order to go through an AST. Instead of directly translate at the same time you go throguh the AST creating a semantic model and then connect to it an interpreter.

Example (SQL Syntax Tree):

Instead of generate directly a SQL sentence, I create a semantic model, and then I'm able to connent an interpreter that translate this semantic model to a SQL sentence.

Abstract Systex Tree -> Semantic Model -> Interpreter

By this way, I could have a Transact-SQL Interpreter and another onr for SqLite.

Upvotes: 1

Views: 250

Answers (1)

Ira Baxter
Ira Baxter

Reputation: 95354

The terms "domain object" and "semantic model" aren't really standard terms from the compiler literature, so you'll get lots of random answers.

The usual terms related to parsing are "concrete syntax tree" (matches the shape of the grammar rules), "abstract syntax tree" (an attempt to make a tree which contains less accidental detail, although it might not be worth the trouble.).

Parsing is only a small part of the problem of processing a language. You need a lot of semantic interpretation of syntax, however you represent it (AST, CST, ...). This includes concepts such as :

  • Name resolution (for every identifier, where is it defined? used?
  • Type resolution (for every identifier/expression/syntax construct, what is the type of that entity?
  • Type checking (is that syntax construct used in a valid way?)
  • Control flow analysis (what order are the program parts executed in, possibly even parallel/dynamic/constraint-determined)
  • Data flow analysis (where are values defined? consumed?)
  • Optimization (replacement of one set of syntax constructs by another semantically equivalent set with some nice property [executes faster after compilation is common]), at high or low levels of abstraction
  • High level code generation, e.g, interpreting sets of syntactic constructs in the language, to equivalent sets in the targeted [often assembly-language like] language

Each of these concepts more or less builds on top of the preceding ones.

The closest I can come to "semantic model" is that high-level code generation. That takes a lot of machinery that you have to build on top of trees.

ANTLR parses. You have to do/supply the rest.

Upvotes: 1

Related Questions