Sunday, March 18, 2007

Semantic Analysis - the Meaning of a Sentence in Mynx

I’ve been lax in blogging more about Mynx, but coding/thinking about code = programming takes time, and sometimes its best to let the mind work on things in the background...the subconscious mind.

Originally I implemented semantic checks in a easy to difficult, with the intent to handle the more difficult semantic checks last...not a good approach. When initial task are trivially easy, the more difficult tasks become tedious -- routine instead of fun tasks to work out.

Semantic checks for Mynx are not the standard type checking presented in many compiler texts. Since Mynx uses a parse oriented around a sentence, the semantic analysis is oriented around the sentence as the foundational structure.

Each sentence performs its own semantic check, passing semantic data into a semantic context object. One advantage of sentence semantic check is that each sentence is mutually exclusive from the other, so a semantic error can be very specific to a sentence and the semantic information. This also (in the future) implies semantic checks can be parallelized.

After a sentence, other semantic checks around structures work upward towards the unit -- a class or program. The structure is that of an abstract syntax tree, only a sentence is the basic leaf element, with the unit -- a class or program as the root of the tree.

For a class attribute, there are approximately seventeen semantic checks on the sentence. If there is any semantic error, then higher level semantic checks would simply be a cascade or avalanche effect -- semantic errors in a sentence cause semantic errors in higher level structures to be detected.

However, the important principle of the semantic checks on all the sentences (not all sentences are checked) in Mynx is that the sentence is semantically valid in the next stage of the semantic analysis process.

Example of a semantic error in a class attribute sentence:


private Integer int to null out for all;


The sentence semantic check only has information relating within the sentence, nothing beyond or before it -- no a priori data.

The semantic error is that the class attribute is private, so inaccessible outside the class, but internally has a scope outside for all methods in the class. Thus the class attribute cannot be accessed externally or internally -- so its semantically as if the class attribute is invisible.

Syntactically it is a valid sentence, but semantically is equivalent to saying “This statement does not exist.” It does, so therefore it does not. The philisophical and logic implications are not important, as it is semantically invalid.

Example of a semantic error in a class attribute sentence:


public constant String STR to null;


The semantic error is that a constant is initialized to null or nothing -- which makes no sense as a constant is immutable once created. The class attribute mode of a constant is incompatible with an initialization value of null. A constant of nothing is contradictory in terms of what a constant is for in the Mynx language.

The various features of a class attribute: access, mode, initialization, scope, etc. have to be checked for contradictions or inconsistencies, such as access-scope and mode-initialization.

The top three sentences with the largest semantic checks are:

  1. class attribute
  2. program attribute
  3. expression statement

An expression statement is a syntax form for a declaration, the attributes of program and class are declarations -- for a sentence semantic check, declaration has the most semantic checks. Not what I’d expect, but as declarations are information fed into the compiler (not executable statements) via the semantic context, it seems to track more semantic checks to “guard” against semantically invalid information being passed along.

Labels: , , , ,

Website Spy Software