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
semantic_tutorial [2015/11/20 16:01]
deva
semantic_tutorial [2016/12/08 13:25]
deva [Step 5: Type checking]
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.
     * The function called ''​lex''​ asks the current line number from the parser and saves it into a field of the ''​d_loc<​nowiki>​__</​nowiki>''​ attribute of the ''​Parser''​ class. Then it asks for the next token from the lexer and returns it to the parser.     * The function called ''​lex''​ asks the current line number from the parser and saves it into a field of the ''​d_loc<​nowiki>​__</​nowiki>''​ attribute of the ''​Parser''​ class. Then it asks for the next token from the lexer and returns it to the parser.
     * The line number information is used in the ''​error''​ function to print location information for error messages.     * The line number information is used in the ''​error''​ function to print location information for error messages.
-  * Make sure you can compile the parser and it accepts all the correct [[http://​deva.web.elte.hu/​compilers/lab/​while-tests.zip|test files]] and rejects the lexical error ones. Try out the semantic error test cases! They are accepted. The goal of the tutorial is to find those errors as well.+  * Make sure you can compile the parser and it accepts all the correct [[http://​deva.web.elte.hu/​compilers/​while-tests.zip|test files]] and rejects the lexical error ones. Try out the semantic error test cases! They are accepted. The goal of the tutorial is to find those errors as well.
  
 ==== Step 2: Symbol table === ==== Step 2: Symbol table ===
Line 115: Line 115:
   * 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.
     * 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(symbol_table[*$1].var_type);''​
     * 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>​
semantic_tutorial.txt · Last modified: 2016/12/08 13:25 by deva