This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision Next revision Both sides next revision | ||
semantic_tutorial [2015/11/09 22:11] deva [Step 6: Fixing the memory leaks] |
semantic_tutorial [2016/12/01 12:32] deva |
||
---|---|---|---|
Line 3: | Line 3: | ||
==== Step 1: Preparation === | ==== Step 1: Preparation === | ||
- | * In this tutorial we use the [[http://deva.web.elte.hu/compilers/lab/while-language.html|While language]]. It is possible to use your own lexer and parser. If you have not completed it, use [[http://deva.web.elte.hu/compilers/lab/parser.zip|this implementation]]. | + | * In this tutorial we use the [[http://deva.web.elte.hu/compilers/lab/while-language.html|While language]]. It is possible to use your own lexer and parser. If you have not completed it, use [[http://deva.web.elte.hu/compilers/parser.zip|this implementation]]. |
* If you are working not with your own parser, please take the time to understand the grammar. | * If you are working not with your own parser, please take the time to understand the grammar. | ||
* Look into the ''Parser.ih'' file. | * Look into the ''Parser.ih'' file. | ||
Line 111: | Line 111: | ||
* Find out how is the non-terminal symbol for expressions is called in your grammar. (It is ''expression'' in the parser downloadable at the beginning of this tutorial, but it might have a different name in your own solution.) Since this is a non-terminal, you can define the type of its semantic value as follows. (Insert it near the token declarations at the beginning of ''while.y''.) | * Find out how is the non-terminal symbol for expressions is called in your grammar. (It is ''expression'' in the parser downloadable at the beginning of this tutorial, but it might have a different name in your own solution.) Since this is a non-terminal, you can define the type of its semantic value as follows. (Insert it near the token declarations at the beginning of ''while.y''.) | ||
<code> | <code> | ||
- | %type expr_type expression | + | %type <expr_type> expression |
</code> | </code> | ||
* 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 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. | ||
Line 118: | Line 118: | ||
* In case of expressions build using operators, there is need for type checking the arguments. For example, the rule for addition will have this C++ code: | * In case of expressions build using operators, there is need for type checking the arguments. For example, the rule for addition will have this C++ code: | ||
<code> | <code> | ||
- | if($1->expr_type != integer || $3->expr_type != integer) | + | if(*$1 != integer || *$3 != integer) |
{ | { | ||
std::stringstream ss; | std::stringstream ss; | ||
Line 132: | Line 132: | ||
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. | 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): | + | * 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> | <code> | ||
delete $i; | delete $i; | ||
</code> | </code> | ||