Difference between revisions of "Semantic Analysis/The Compact language: semantic analysis example and C generation"

From Wiki**3

< Semantic Analysis
(The Compact case (CDK3))
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
<font color='red'><b>OBSOLETE SECTION</b></font>
 
<font color='red'><b>OBSOLETE SECTION</b></font>
* '''SEE [[Semantic Analysis/The Simple language: semantic analysis|The Simple language: semantic analysis]]'''
+
* '''For up to date information, see "[[Semantic Analysis/The Simple language: semantic analysis|The Simple language: semantic analysis]]"'''
  
 +
== The Compact case (CDK3) ==
 
The package provided here contains the Compact compiler for CDK3.
 
The package provided here contains the Compact compiler for CDK3.
  
Line 10: Line 11:
 
* [http://www.l2f.inesc-id.pt/~david/ist/docencia/compiladores/2007-2008/compact-with-simple-type-checking-200805111819.tar.bz2 Compact with simple type checking (200805111819)]
 
* [http://www.l2f.inesc-id.pt/~david/ist/docencia/compiladores/2007-2008/compact-with-simple-type-checking-200805111819.tar.bz2 Compact with simple type checking (200805111819)]
  
The visitor approach to type checking is justified by the independence of code generation from the details of type checking. In addition, the same type validator may be shared by several visitors performing code generation. In our example, the only changes to the original Compact are located in [http://www.l2f.inesc-id.pt/~david/ist/docencia/compiladores/2007-2008/compact-with-simple-type-checking-200805111819/Cwriter.cpp Cwriter.cpp] (in each node we wish to check). The validation code is self-contained and is integrated as the drop-in class <tt>TypeValidator</tt> ([http://www.l2f.inesc-id.pt/~david/ist/docencia/compiladores/2007-2008/compact-with-simple-type-checking-200805111819/TypeValidator.h TypeValidator.h] and [http://www.l2f.inesc-id.pt/~david/ist/docencia/compiladores/2007-2008/compact-with-simple-type-checking-200805111819/TypeValidator.cpp TypeValidator.cpp]).
+
The visitor approach to type checking is justified by the independence of code generation from the details of type checking. In addition, the same type validator may be shared by several visitors performing code generation. In our example, the only changes to the original Compact are located in '''Cwriter.cpp''' (in each node we wish to check). The validation code is self-contained and is integrated as the drop-in class '''TypeValidator'''.
  
 
Example 1 from Compact: the original Compact compiler would accept the code, but the new one does not: since '''x''' is an integer and conditions must be boolean, it is rejected as a condition for '''if'''.
 
Example 1 from Compact: the original Compact compiler would accept the code, but the new one does not: since '''x''' is an integer and conditions must be boolean, it is rejected as a condition for '''if'''.

Latest revision as of 21:02, 5 May 2014

OBSOLETE SECTION

The Compact case (CDK3)

The package provided here contains the Compact compiler for CDK3.

Note that, even though it does not work (or even compile) with CDK4 and newer, the principles are the same.

This version has been improved to perform type checking in the C generator visitor. This visitor uses a second visitor to check types before deciding on which code to generate. The checks are related with the use of boolean expressions in tests (while, if, and if-else nodes) and with type consistency in operators (in this case, although we could have decided otherwise, operators such as + and - must have arguments of the same type and this type must be integer). In addition, the compiler now checks if a variable has the proper type in certain cases (for instance, in the tests mentioned above).

The visitor approach to type checking is justified by the independence of code generation from the details of type checking. In addition, the same type validator may be shared by several visitors performing code generation. In our example, the only changes to the original Compact are located in Cwriter.cpp (in each node we wish to check). The validation code is self-contained and is integrated as the drop-in class TypeValidator.

Example 1 from Compact: the original Compact compiler would accept the code, but the new one does not: since x is an integer and conditions must be boolean, it is rejected as a condition for if. <text> program x = 0; if (x) print x; while (x < 30) {

 print x;
 x = x + 1;

} end </text>

Also, notice that type checking is only active for the C target code generator (as intended). The procedure for including the validator calls in the other targets is similar.