0

I have to build a compiler that translates the java language into pyhton. I'm using the Flex and Bison tools. I created the flex file and I defined the syntactic grammar in Bison for some restrictions that I have to implement (such as array, management of cycles, management of a class, management of logical-arithmetic operators, etc.). In semantic analysis, I have a problem with the type checking of variable and array. I am using a symbol table created using the uthash library. This is the structure of symbol table in the module symboltable.h:

struct symtable{

  char *scopename;            // key 
  struct symtable2 *subtable; // symble table secondary
  UT_hash_handle hh;          // to make the structure hash

}

struct symtable2            // secondary symbol structure
{

  char *name;               // Name of the symbol (key)
  char *element;            // it can be a variable or an array
  char *type;               // Indicates the type assumed by the token (int, float, char, bool)
  char *value;              // value assigned to the variable
  int dim;                  // Array size, it is zero in the case of a variable.
  UT_hash_handle hh;        // to make the structure hash

};

I tried to manage the type checking between variables with this function present in the parser but I have no way at the time to verify the correctness:

int typeChecking (variable1, variable2) {
   struct symtable2 *s2;

   s2=find_symbol (scopename, variable1);
   if (s2!=NULL) {
     int type1= s2->type;
     char element1 = s2->element;
   }
   else{
     printf("\n\n\033[01;31mVariable 1 not defined.\033[00m\n");
     return -1;
   }

   s2=find_symbol (scopename, variable2);
   if (s2!=NULL) {
     int type2= s2->type;
     char element2 = s2->element;
   }
   else{
     printf("\n\n\033[01;31mVariable 2 not defined.\033[00m\n");
     return -1;
   }

   if(element1=='variable' && element2=='variable'){

     if (type1 == type2){
       return 0;
     }
     else {
       return 1;
     }
   }

   else {
     printf("\n\n\033[01;31m Different elements.\033[00m\n");
     return -1;
   }
  }

Now, I can not understand how I could implement type checking on the homogeneity of the type of the elements of the array. Can you advise me something? Should I use the lists?

I hope you can help me, thank you very much!

Micheal
  • 11
  • 6
  • What is an example of a program in which the elements of the array are not homogenously typed? (In other words, what exactly are you trying to detect?) Why is it not sufficient to keep the element type for an array in the symbol table? – rici Aug 28 '18 at 20:53
  • I'm a beginner with parsing, so I could probably be wrong. What I want to point out are the possible errors that can be made in initializing a static array with a certain number of elements that are not homogeneous. For example, int [] a = {3, 5, 6, 9, 10} is correct, but if I did int [] a = {3, 5, 6, "hello", 10} would be incorrect. In your opinion @rici , is not it necessary to detect this type of error in the semantic actions of an initialization of an array? – Micheal Aug 29 '18 at 07:34
  • That error should definitely be detected by the type checker, but note that the problem here isn't that `"hello"` has a different type than 3, 5, 6 or 10, but that it has a different type than the declared element type of the array. `int a[] = {"hello", "world"};` would still be a type error even though all elements of the initializer have the same type. In other words, you should compare the type of each element to the declared element type of the array - not to the types of the other elements. – sepp2k Aug 29 '18 at 12:19
  • Yes @sepp2k , I might have expressed it badly, but clearly it is this kind of control that I have to do. The problem is that the type of the array is present in the symbol table, but the types of the elements of the vector are not. If the array is allocated and statically initialized, how can I do this type of control? Should I create another structure or list that will contain the elements of the array with its type? I hope you can suggest something to me, thank you! – Micheal Aug 29 '18 at 13:19
  • @michele The point is that you don't need to remember the types of the elements. All you need is the declared type of the array and the type of the element you're looking at right now (which you don't need to store anywhere). – sepp2k Aug 29 '18 at 13:24
  • A moment, maybe something is missing from me. @sepp2k What do you mean by "the type of the element you're looking at right now"? How do i get the type of item i'm looking at right now? For example, I have an array allocated and statically initialized as int [] a = {3, 5, 6, 9, 10}. This will be given as input to the parser which should compare, through semantic rules, the type of the vector and the type of each single element. Now, the type of the vector is present in the symbol table, but the type of each individual element how do I get it? Excuse me if I'm repetitive. – Micheal Aug 29 '18 at 14:44
  • @Michele Let's take a step back from arrays for a second. Let's say you see the lines `int x = 42;` and `int y = "hello";`: How are you currently making sure that the former compiles and the latter produces an error? Type-checking array-elements shouldn't work any differently than that. – sepp2k Aug 29 '18 at 15:04
  • You're right @sepp2k , I thank you for clarifying the ideas. I completely ignored the control over the type of a simple declaration of a variable with a value. But since we are, do you have any suggestions to give me on how to implement this? I'm really making a lot of confusion about semantics. – Micheal Aug 29 '18 at 17:23
  • @Michele If you have something like a function `Type* typeOf(Expression*, Env*)` that gives you the type of a well-typed expression, an array initializer should simply check that `typeOf(exp, env)` is assignable to the declared type of the array for all expressions in the initializer. Likewise a normal expression initializer (i.e. `type varName = expression;`) should check that `typeOf(expression, env)` is assignable to `type`. Likewise for each assignment statement `lexp = exp;`, you should check that `typeOf(exp, env)` is assignable to `typeOf(lexp, env)`. – sepp2k Aug 29 '18 at 21:03
  • Very kind @sepp2k , I do not know how to thank you. I was not aware of the typeOf function in c. One last thing I wanted to ask you hoping you can help me even in this circumstance. A few days ago I posted this question to this link: ( https://stackoverflow.com/questions/52074834/how-to-manage-semantic-rule-of-declaration-of-variable-in-bison ) In your opinion, is it possible to handle semantics and translation directly within the action (among {}) of the grammar in Bison, without having to create the ast (Abstract Syntax tree) to manage semantics and translation? – Micheal Aug 31 '18 at 09:42
  • @Michele I wasn't referring to the `typeof` built-in (which is a compiler-specific extension and, in any case, not applicable to your problem). I was referring to a function that you'd define yourself (or rather one that I assumed you might already have defined yourself). You don't *have* to go through an AST, but I'd say it helps with structuring your code probably. If you don't use an AST, the implementation of the `typeOf` function I suggested would instead be spread out through the actions of the various expression rules. – sepp2k Aug 31 '18 at 10:02

0 Answers0