User Tools

Site Tools


semantic_tutorial

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Next revision Both sides next revision
semantic_tutorial [2015/11/09 21:30]
deva [Step 5: Type checking]
semantic_tutorial [2015/11/09 22:11]
deva [Step 6: Fixing the memory leaks]
Line 113: Line 113:
 %type expr_type expression %type expr_type expression
 </​code>​ </​code>​
-  * For each //​expression ​rules//, do the necessary type checking and set the semantic value of the left-hand side of the rule. The semantic value of the left-hand side can be referenced by the ''​$$''​ symbol.+  * For each //​expression ​rule//, do the necessary type checking and set the semantic value of the left-hand side of the rule. The semantic value of the left-hand side can be referenced by the ''​$$''​ symbol.
     * For example, the rule of number literals will have the following code: ''​$$ = new type(integer);''​     * For example, the rule of number literals will have the following code: ''​$$ = new type(integer);''​
     * In case of the rule of a single variable name, we have to ask the type of the variable from the symbol table. (Note, that we have earlier checked in this rule whether the variable was in the symbol table, so after this check we are safe to retrieve the variable data.) ''​$$ = new type(symboltable[*$1].var_type);''​     * In case of the rule of a single variable name, we have to ask the type of the variable from the symbol table. (Note, that we have earlier checked in this rule whether the variable was in the symbol table, so after this check we are safe to retrieve the variable data.) ''​$$ = new type(symboltable[*$1].var_type);''​
Line 128: Line 128:
   * Type check assignments:​ Emit an error, if the left and right hand sides are of different types.   * Type check assignments:​ Emit an error, if the left and right hand sides are of different types.
   * Test your solution: It should give error messages for all semantic error test cases.   * Test your solution: It should give error messages for all semantic error test cases.
 +
 +==== Step 6: Fixing the memory leaks ====
 +
 +All the semantic values were created using the ''​new''​ keyword of C++. This means we have to delete them in order to avoid memory leakage.
 +  * Review all the rules and delete the semantic values of the symbols on the right-hand side at the end of the corresponding C++ code fragments (change ''​i''​ to the appropriate number):
 +<​code>​
 +delete $i;
 +</​code>​
 +
semantic_tutorial.txt · Last modified: 2016/12/08 13:25 by deva