This shows you the differences between two versions of the page.
|
alf:teme:tema_en_3 [2017/04/24 23:17] alexandru.radovici |
alf:teme:tema_en_3 [2018/04/24 23:59] (current) alexandru.radovici |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== Homework 3 - Semantic ====== | + | ====== Homework 4 - Semantic ====== |
| Line 14: | Line 14: | ||
| ===== What do you have to do ===== | ===== What do you have to do ===== | ||
| - | The purpose of the homework is to write the semantic analysis, the intermediate language for the Alfy language. | + | The purpose of the homework is to write the semantic analysis, the intermediate language for the Alf language. |
| - | You will receive an AST from the parser that parses correctly a Alfy language source and have to write: | + | You will receive an AST from the parser that parses correctly a Alf language source and have to write: |
| - the symbol table | - the symbol table | ||
| - | - add to every AST node an attribute //symbol// with the context id | + | - add to every AST node an attribute //symbol// with the context id and determine the types for the nodes that return (expression, value, id, element_of_array, element_of_struct elements, value_of_function) |
| - | - determine the types for the nodes that return (expression, value, id, element_of_array, element_of_struct elements, value_of_function) | + | |
| - | - the intermediate language list (without the functions code) | + | |
| - the semantic error list | - the semantic error list | ||
| + | |||
| + | <note> | ||
| + | All the three requirements are verified and graded separately. Examples that produce no errors will receive erroor points only if the symbol table and the ast are correct. | ||
| + | </note> | ||
| The program will receive two parameters from the command line: | The program will receive two parameters from the command line: | ||
| Line 27: | Line 29: | ||
| * the output file | * the output file | ||
| <code bash> | <code bash> | ||
| - | node main.js source.alfy source.alfy.json | + | node main.js source.alf source.alf.json |
| </code> | </code> | ||
| + | |||
| + | ===== The Alf grammar file ===== | ||
| + | You may use your grammar file or the [[https://github.com/alexandruradovici/alf-alfy-language-public/blob/master/alfy.jison|reference file]] | ||
| + | |||
| ===== Hints for solving the homework ===== | ===== Hints for solving the homework ===== | ||
| Line 35: | Line 41: | ||
| * Determine all the variable types in the symbol table | * Determine all the variable types in the symbol table | ||
| * Determine the types for all the instructions that return a result | * Determine the types for all the instructions that return a result | ||
| - | * While determinig the types, write the errors | + | * While determining the types, write the errors |
| | | ||
| ===== Output file ===== | ===== Output file ===== | ||
| Line 71: | Line 77: | ||
| The context object stores | The context object stores | ||
| * the variable declared in that context | * the variable declared in that context | ||
| - | * the functions declared in that context | + | * the message declared in that context |
| - | * the types declared in that context | + | * the classes declared in that context |
| * the parent context id (position in the symbol_table array) | * the parent context id (position in the symbol_table array) | ||
| - | * the type of the context (script or function) | + | * the type of the context (module or message) |
| - | * the name of the function which context it is (unless this is not the script) | + | * the name of the message which context it is (unless this is not the module) |
| - | * the type of the return value of the function which context it is (unless this is not the script) | + | * the name of the class which context it is (unless this is not the module) |
| + | * the type of the return value of the message which context it is (unless this is not the module) | ||
| <note> | <note> | ||
| Context objects are generated by | Context objects are generated by | ||
| - | * the main script | + | * the main module |
| - | * a function definition | + | * a message definition |
| + | * a class | ||
| </note> | </note> | ||
| Line 88: | Line 96: | ||
| { | { | ||
| "variables": { // a dictionary of variables | "variables": { // a dictionary of variables | ||
| - | "variable": { | + | "variable_name": { |
| "type": // type of the variable | "type": // type of the variable | ||
| "line": // the line where the variable was declared | "line": // the line where the variable was declared | ||
| Line 94: | Line 102: | ||
| } | } | ||
| }, | }, | ||
| - | "functions": { // a dictionary of functions | + | "messages": { // a dictionary of functions |
| - | "function": { | + | "message_name": { |
| "type": // the return type of the function | "type": // the return type of the function | ||
| "parameters": [] // the list of parameters the function takes (the parameters node from the AST) | "parameters": [] // the list of parameters the function takes (the parameters node from the AST) | ||
| Line 105: | Line 113: | ||
| "types": { // a dictionary of types | "types": { // a dictionary of types | ||
| "type": { | "type": { | ||
| - | "type": // the type of the new type struct or array | + | "type": // the type of the new type class or array |
| "line": // the line where the type was declared | "line": // the line where the type was declared | ||
| // for array | // for array | ||
| Line 111: | Line 119: | ||
| "from": // the first index | "from": // the first index | ||
| "to": // the last index | "to": // the last index | ||
| - | // for struct | + | // for class |
| "elements": [ // a list of array elements (the node form the AST) | "elements": [ // a list of array elements (the node form the AST) | ||
| { | { | ||
| Line 124: | Line 132: | ||
| "parent": 0, // the parent context position in the symbol_table (except of the main script that has no parent), usually 0, | "parent": 0, // the parent context position in the symbol_table (except of the main script that has no parent), usually 0, | ||
| "type": // the type of the | "type": // the type of the | ||
| - | "fn": // the function name if this is a function context | + | "message": // the message name if this is a function context |
| + | "class": // the class name if the context is in a message that is in a class | ||
| "return_value": // the return type of the function if this is a function context | "return_value": // the return type of the function if this is a function context | ||
| } | } | ||
| Line 137: | Line 146: | ||
| } | } | ||
| }, | }, | ||
| - | "functions": {}, | + | "messages": {}, |
| "types": { | "types": { | ||
| "school": { | "school": { | ||
| - | "type": "struct", | + | "type": "class", |
| "line": 6, | "line": 6, | ||
| "elements": [ | "elements": [ | ||
| Line 168: | Line 177: | ||
| ==== Expression return type ==== | ==== Expression return type ==== | ||
| For each of the following nodes, determine the return type | For each of the following nodes, determine the return type | ||
| - | * expression | + | * exp |
| * value | * value | ||
| - | * id | + | * identifier |
| - | * value_of_function | + | * return |
| - | * element_of_array | + | * element |
| - | * element_of_struct | + | * property |
| + | * dispatch | ||
| - | Set type the by adding a parameter t in the node. | + | Set type the by adding a parameter type in the node. |
| <note> | <note> | ||
| When searching for a variable, the algorithm is: | When searching for a variable, the algorithm is: | ||
| - | * if it is a function context, search the local variables | + | * if it is a message context, search the local variables |
| * if not found, search the parameters | * if not found, search the parameters | ||
| - | * if not found, search the script context local variables | + | * if not found, search the class context local variables |
| - | * if it is the script context, search the local variables | + | * if it is the module context, search the global variables |
| </note> | </note> | ||
| Line 189: | Line 199: | ||
| <code javascript> | <code javascript> | ||
| { | { | ||
| - | "type": "expression", | + | "id": "exp", |
| "op": "=", | "op": "=", | ||
| "left": { | "left": { | ||
| - | "type": "expression", | + | "id": "exp", |
| "op": "mod", | "op": "mod", | ||
| "left": { | "left": { | ||
| - | "type": "id", | + | "id": "id", |
| "value": "n", | "value": "n", | ||
| "line": 8, | "line": 8, | ||
| "symbol": 1, | "symbol": 1, | ||
| - | "t": "int" | + | "type": "int" |
| }, | }, | ||
| "right": { | "right": { | ||
| - | "type": "id", | + | "id": "id", |
| "value": "i", | "value": "i", | ||
| "line": 8, | "line": 8, | ||
| "symbol": 1, | "symbol": 1, | ||
| - | "t": "int" | + | "type": "int" |
| }, | }, | ||
| "line": 8, | "line": 8, | ||
| Line 213: | Line 223: | ||
| }, | }, | ||
| "right": { | "right": { | ||
| - | "type": "value", | + | "id": "value", |
| - | "t": "int", | + | "type": "int", |
| "value": 0, | "value": 0, | ||
| "line": 8, | "line": 8, | ||
| Line 221: | Line 231: | ||
| "line": 8, | "line": 8, | ||
| "symbol": 1, | "symbol": 1, | ||
| - | "t": "logic" | + | "type": "logic" |
| } | } | ||
| </code> | </code> | ||
| Line 234: | Line 244: | ||
| * functions are defined before being used | * functions are defined before being used | ||
| * array indexes are numbers | * array indexes are numbers | ||
| - | * struct elements exist | + | * class properties exist |
| * the variable for element_of_array is an array | * the variable for element_of_array is an array | ||
| - | * the variable for element_of_struct is a struct | + | * the variable for a property is a class |
| * variables are not redefined | * variables are not redefined | ||
| * functions are not redefined | * functions are not redefined | ||
| Line 332: | Line 342: | ||
| </code> | </code> | ||
| - | === UNDEFINED_FUNCTION === | + | === UNDEFINED_MESSAGE === |
| The error occurs when a function call is made to a function that is not defned. | The error occurs when a function call is made to a function that is not defned. | ||
| Line 338: | Line 348: | ||
| <code javascript> | <code javascript> | ||
| { | { | ||
| - | id: // function name | + | id: // message name |
| } | } | ||
| </code> | </code> | ||
| Line 361: | Line 371: | ||
| } | } | ||
| - | === NOT_STRUCT_ELEMENT === | + | === NOT_CLASS_PROPERTY === |
| The error occurs when an element is not part of that struct. | The error occurs when an element is not part of that struct. | ||
| Line 367: | Line 377: | ||
| <code javascript> | <code javascript> | ||
| { | { | ||
| - | struct: // struct name | + | class: // struct name |
| - | element: // element name | + | property: // element name |
| } | } | ||
| </code> | </code> | ||
| - | === NOT_STRUCT === | + | === NOT_CLASS === |
| - | The error occurs when an element is requested for a variable that is not a struct type | + | The error occurs when a property is requested for a variable that is not a class type |
| == Elements == | == Elements == | ||
| Line 402: | Line 412: | ||
| </code> | </code> | ||
| - | === VALUE_OUTSIDE_FUNCTION === | + | === RETURN_OUTSIDE_MESSAGE === |
| - | The error occurs when a value statement (value_of_function node) is used outside a function | + | The error occurs when a value statement (return node) is used outside a message |
| == Elements == | == Elements == | ||
| Line 494: | Line 504: | ||
| Bonus will be awarded only if all other tests pass. | Bonus will be awarded only if all other tests pass. | ||
| </note> | </note> | ||
| + | |||
| + | ===== Copying ===== | ||
| + | |||
| + | The homework is individual. Any attempt of copying will result in **0p** for the homework. Automated anti copying system will be used. | ||
| + | |||
| + | |||
| + | ===== Questions ===== | ||
| + | |||
| + | If you have any questions related to the homework, please ask them by posting an issue on the github [[https://github.com/alexandruradovici/alf2018.git|repository]] with the title format //[calculator] <your question title>//. You will need a github account for that. | ||
| + | |||
| + | <note warning> | ||
| + | **DO NO POST ANY CODE**. This is considered copying and will result in a 0p homework for you. | ||
| + | </note> | ||
| + | |||
| + | If you want to receive an email when issues are posted or when there are new messages, got to the github [[https://github.com/alexandruradovici/alf2018|repository]] and click //Watch//. | ||
| | | ||
| ===== Testing ===== | ===== Testing ===== | ||
| Line 516: | Line 541: | ||
| <code bash> | <code bash> | ||
| - | wget https://nodejs.org/dist/v6.10.0/node-v6.10.0-linux-x64.tar.xz | + | wget https://nodejs.org/dist/v8.9.4/node-v8.9.4-linux-x64.tar.xz |
| - | tar xvfJ node-v6.10.0-linux-x64.tar.xz | + | tar xvfJ node-v8.9.4-linux-x64.tar.xz |
| - | cd node-v6.10.0-linux-x64 | + | cd node-v8.9.4-linux-x64 |
| sudo cp -R * /usr | sudo cp -R * /usr | ||
| </code> | </code> | ||
| Line 549: | Line 574: | ||
| - Create a zip (not rar, ace, 7zip or anything else) archive containing: | - Create a zip (not rar, ace, 7zip or anything else) archive containing: | ||
| - | * your main file (main.js) | + | * your main file |
| - | * your alfy.jison file | + | * your grammar.jison file |
| * your javascript files (*.js) | * your javascript files (*.js) | ||
| - | * yarn.lock (if you are using yarn) | ||
| * the package.json file | * the package.json file | ||
| * the Readme file | * the Readme file | ||
| - sign in with [[https://vmchecker.cs.pub.ro|vmchecker]] | - sign in with [[https://vmchecker.cs.pub.ro|vmchecker]] | ||
| - select the //Automates et Langages Formelles (FILS)// course | - select the //Automates et Langages Formelles (FILS)// course | ||
| - | - select //3. Semantic// | + | - select //4. Semantic// |
| - upload the archive | - upload the archive | ||
| Line 572: | Line 596: | ||
| unzip archive.zip homework | unzip archive.zip homework | ||
| cd homework | cd homework | ||
| - | # if the file yarn.lock exists | ||
| - | yarn | ||
| - | # else | ||
| npm install | npm install | ||
| echo '{ "node":true, "esnext":true }' > .jshintrc | echo '{ "node":true, "esnext":true }' > .jshintrc | ||