1

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.

Jordi
  • 20,868
  • 39
  • 149
  • 333

1 Answers1

1

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.

Community
  • 1
  • 1
Ira Baxter
  • 93,541
  • 22
  • 172
  • 341
  • Thanks a lot for your answer. As far I'm able to figure out, everything you have written out could be pointed out at the same time you go through AST. Nevertheless, once you have gone through the AST, it would be advisable to build a representation of what each sentence is/do. It is the model that defines/describes the semantics. Do you agree? Obviously, it's really easier with DSLs (i.e. a simple domain query language) – Jordi Mar 29 '16 at 07:08
  • "everything you have written... could be pointed out at the same time you go through the AST". If you believe that, you could do same as you go through the characters, because the AST is derived from that, and that's clearly nonsense. ... – Ira Baxter Mar 29 '16 at 08:24
  • The point of some "model" is to have an explicit representation of what is important. If you want to capture the full semantics, you need to 1) interpret the text/AST/etc. to extract a model as a collection of symbol tables/ type checks, flow graphs or 2) you have to translate the program to some other (lower level language) and call the translated result the (full) semantic model. In the latter case, you are counting on the presumably simpler semantics of the target language, to make the semantics of the source language clearer. DSLs don't make this easier. – Ira Baxter Mar 29 '16 at 08:26