This is an old revision of the document!


Homework 3 - Semantic

Information

Deadline: 7th of May, 23:55
Points: 2 points out of the final grade
Upload the homework: vmchecker.cs.pub.ro
Late upload: 0.1 points / day (maximum 4 days)

What do you have to do

The purpose of the homework is to write the semantic analysis, the intermediate language for the Alfy language.

You will receive an AST from the parser that parses correctly a Alfy language source and have to write:

  • the symbol table
  • 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 program will receive two parameters from the command line:

  • the source file
  • the output file
node main.js source.alfy source.alfy.json

Hints for solving the homework

  • Make the symbol table
  • Determine all the variable types in the symbol table
  • Determine the types for all the instructions that return a result
  • While determinig the types, write the errors

Output file

The format of the output file is the following

{
    symbol_table: [...], // the symbol_table
    ast: {...}, // the ast with the type for very node that returns a value
    error_list [] // the error list
}

Symbol table

The symbol table is an array of context objects.

[
   // context object id 0 - the context of the main script
   {
      ...
   },
   // context object id 1 - the context object of a function
   {
      ...
   },
   ...
]

While determining the symbol table, add to each AST node a parameter symbol with the value of the its context id.

Context Object

The context object stores

  • the variable declared in that context
  • the functions declared in that context
  • the types declared in that context
  • the parent context id (position in the symbol_table array)
  • the type of the context (script or function)
  • the name of the function which context it is (unless this is not the script)
  • the type of the return value of the function which context it is (unless this is not the script)

Context objects are generated by

  • the main script
  • a function definition

The object looks like

{
   "variables": { // a dictionary of variables
      "variable": {
         "type": // type of the variable
         "line": // the line where the variable was declared
         "value": // the expression that the variable initially has (optional, if the define was with an assignment)
      }
   },
   "functions": { // a dictionary of functions
      "function": {
         "type": // the return type of the function
         "parameters": [] // the list of parameters the function takes (the parameters node from the AST)
         "line": // the line where the function was declared
         "symbol": // the context object id that the function creates
 
      }
   }, 
   "types": {  // a dictionary of types
      "type": {
               "type":  // the type of the new type struct or array
               "line":  // the line where the type was declared
               // for array
               "elements_type":  // the type of each array element
               "from":  // the first index
               "to": // the last index
               // for struct
               "elements": [ // a list of array elements (the node form the AST)
                  {
                     "type": 
                     "id": 
                     "line": 
                  },
                  ...
               ]
            }
   },
   "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 
   "fn": // the function name if this is a function context
   "return_value": // the return type of the function if this is a function context
}

Exemple

{
   "variables": {
      "s": {
         "type": "school",
         "line": 8
      }
   },
   "functions": {},
   "types": {
      "school": {
         "type": "struct",
         "line": 6,
         "elements": [
            {
               "type": "int",
               "id": "type",
               "line": 5
            },
            {
               "type": "logic",
               "id": "private",
               "line": 4
            },
            {
               "type": "string",
               "id": "name",
               "line": 3
            }
         ]
      }
   }
}

Verify Types in the AST

Expression return type

For each of the following nodes, determine the return type

  • expression
  • value
  • id
  • value_of_function
  • element_of_array
  • element_of_struct

Set type the by adding a parameter t in the node.

When searching for a variable, the algorithm is:

  • if it is a function context, search the local variables
  • if not found, search the parameters
  • if not found, search the script context local variables
  • if it is the script context, search the local variables

Example

{   
   "type": "expression",
   "op": "=",
   "left": {
      "type": "expression",
      "op": "mod",
      "left": {
         "type": "id",
         "value": "n",
         "line": 8,
         "symbol": 1,
         "t": "int"
      },
      "right": {
         "type": "id",
         "value": "i",
         "line": 8,
         "symbol": 1,
         "t": "int"
      },
      "line": 8,
      "symbol": 1,
      "t": "int"
   },
   "right": {
      "type": "value",
      "t": "int",
      "value": 0,
      "line": 8,
      "symbol": 1
   },
   "line": 8,
   "symbol": 1,
   "t": "logic"
}

Verify types

For all the AST nodes, verify that the types match:

  • attributions work
  • expressions can be calculated
  • the if/while/repeat aslongas expression is a logic value
  • the for variable is a number
  • that variables are defined before being used
  • functions are defined before being used
  • array indexes are numbers
  • struct elements exist
  • the variable for element_of_array is an array
  • the variable for element_of_struct is a struct
  • variables are not redefined
  • functions are not redefined
  • types are not redefined

This is where most of the errors appear from.

Error List

The error list is an array that contains error objects. The order in which these errors are in the list is up to you.

Error Object Format

Each error has the following format:

{
    type: //  string with the error type
    line: // the line number in the source (starting at 1)
    elements: // items for the error, each type of error has different items
    text: // the error text message
}

Error type

The error type is a string with one of the following titles

VARIABLE_ALREADY_DEFINED

The error occurs when a variable definition is repeated.

Elements
{
    variable: // variable name
}

FUNCTION_ALREADY_DEFINED

The error occurs when a function definition is repeated.

Elements
{
    function: // function name
}

TYPE_ALREADY_DEFINED

The error occurs when a type definition is repeated (array or struct).

Elements
{
    type: // type name
}

STRUCT_ELEMENT_ALREADY_DEFINED

The error occurs when a struct element definition is repeated.

Elements

{

  struct: // struct type name
  element: // element name

}

ARRAY_INDEX_VALUE

The error occurs when the array lower index is higher than the higher index

Elements
{
    array: // array type name
    low_index: // the lower index value
    high_index: // the higher index value
}

TYPE_RESOLUTION

The error occurs when the type of a variable is not determinable (bonus only).

Elements
// variable
{
    variable: // the name of the variable with the unresolved type
}
// struct element
{
    struct: // name of struct type
    element: // name of struct element with the unresolved type
}

UNDEFINED_FUNCTION

The error occurs when a function call is made to a function that is not defned.

Elements
{
   id: // function name
}

UNDEFINED_VARIABLE

The error occurs when a variable that is not defined is used.

Elements
{
   variable: // variable name
}

UNDEFINED_TYPE

The error occurs when a type that is not defined is used.

Elements

{

  variable: // the variable that has that type
  type: // the type that is undefined

}

NOT_STRUCT_ELEMENT

The error occurs when an element is not part of that struct.

Elements
{
   struct: // struct name
   element: // element name
}

NOT_STRUCT

The error occurs when an element is requested for a variable that is not a struct type

Elements
{
   type: // actual type that the variable is (instead of a struct type)
}

NOT_ARRAY

The error occurs when an index is requested for a variable that is not an array

Elements
{
   type: // actual type that the variable is (instead of an array type)
}

ARRAY_INDEX_TYPE

The error occurs when an index for an array is not a number or symbol

Elements
{
   array: // array type
   index: // supplied index type
}

VALUE_OUTSIDE_FUNCTION

The error occurs when a value statement (value_of_function node) is used outside a function

Elements
{
   // empty
}

TYPE_EXPRESSION

The error occurs when an expression has incompatible types

Elements
// left op right
{
    left:
    right:
    op:
}
// op value
{
    value:
    op:
}
// if, while or repeat - aslongas
{
    expression: // expression type
    op: // if, while, repeat
}
// for
{
    expression: // expression type
    op: "for"
    element: // variable, from, to or step
}
// value_of_function
{
    op: "value"
    to: // return type
    from: // provided type
}
// is
{
    op: "is"
    to: // to type
    from: // from type
}
// struct element type is undefined
{
    struct: // struct type,
    element: // name of struct element
}
// variable type is undefined
{
    variable: // name of the variable
}

LEXICAL

This is a lexical error

Elements
  • id
  • element

SYNTAX

This is a syntax error

Elements
  • id
  • element
alf/teme/tema_en_3.1493064193.txt.gz · Last modified: 2017/04/24 23:03 by alexandru.radovici
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0