简体   繁体   English

Java-语义分析:使用键和多个值实现哈希表

[英]Java - Semantic Analysis: Implementing a hash table with a key and multiple value

In semantic analysis one of the most popular method or data structure used is Hash Table. 在语义分析中,最常用的方法或数据结构之一是哈希表。 Implementing hash table is relatively easy however reading this peresentation made it complex for me - I don't know if Im right. 实施哈希表是比较容易但阅读本peresentation使它复杂,我-我不知道,如果林权。 Based from the presentation, how do you implement a symbol table with a token ( key ) that has multiple values? 根据演示,如何使用带有多个值的令牌( )来实现符号表?

My idea is to create a table below: 我的想法是在下面创建一个表:

Given input string: 给定输入字符串:
VAR abc, b, c AS INT

   | symbol | datatype | value |
   -----------------------------
   | VAR    | keyword  | null  |
   | abc    | INT      | null  |
   | b      | INT      | null  |
   | c      | INT      | null  |
   | AS     | keyword  | null  |
   | CHAR   | keyword  | null  |

VAR x, w_23='w' AS CHAR

   | symbol | datatype | value |
   -----------------------------
   | VAR    | keyword  | null  |
   | x      | CHAR     | null  |
   | w_23   | CHAR     | 'w'   |
   | AS     | keyword  | null  |
   | CHAR   | keyword  | null  |

So, lets say I have a variable a if I lookup (a) then it should be error because looking at the table key a doesn't exist. 因此,假设我有一个变量a如果我进行查找(a)那应该是错误的,因为查看表 a不存在。 Also, if w_23=10 should also be an error because w_23 is of type CHAR generally it's just a type checking. 同样,如果w_23=10也应该是一个错误,因为w_23的类型为CHAR通常只是类型检查。

My question is, is it possible to create a symbol table like that of above?, if so how do I implement a symbol table with symbol as key and datatype & value as values?. 我的问题是,是否可以像上面那样创建一个符号表?如果是的话,如何实现以symbolkeydatatype & value值的符号表?

You can implement it something like this: 您可以这样实现:

  public class SymbolEntry {
      private String symbol;
      private DataType datatype;
      private Value value;
      ...
  }

where DataType and Value are custom classes that represent the types and values as appropriate. 其中, DataTypeValue是自定义类,它们代表相应的类型和值。 Then the symbol table is simply this: 那么符号表就是这样:

  HashMap<String, SymbolEntry> symtab = new HashMap<>();

where the String key is the symbol. 其中String键是符号。

In short, you need a custom class to represent the values that comprise a symbol table entry. 简而言之,您需要一个自定义类来表示组成符号表条目的值。 The rest is left as "an exercise for the reader". 其余的作为“读者练习”。

I would use an approach based on the Generics. 我将使用基于泛型的方法。

First you define a generic class like this: 首先,您定义一个通用类,如下所示:

public abstract class GenericEntry<T> {
    protected T value;

    @SuppressWarnings("unchecked")
    public Class<? extends T> getDatatype() {
        return (Class<? extends T>) value.getClass();
    }

    public T getValue() {
        return value;
    }

    public void setValue(T value) {
        this.value = value;
    }
}

and then you define a material classes. 然后定义材料类别。 One for the character, for example: 一个用于字符,例如:

public class CharacterEntry extends GenericEntry<Character> { }

and another one for integers: 另一个是整数:

public class IntegerEntry extends GenericEntry<Integer> { }

and a main with some tests: 和一个主要的一些测试:

public class Main {
    public static void main(String[] args) {
        HashMap<String, GenericEntry<?>> myMap = new HashMap<>();

        CharacterEntry characterEntry = new CharacterEntry();
        characterEntry.setValue('c');
        myMap.put("key1", characterEntry);

        IntegerEntry integerEntry = new IntegerEntry();
        integerEntry.setValue(3);
        myMap.put("key2", integerEntry);

        GenericEntry<?> entry1 = myMap.get("key1");
        System.out.println(entry1.getDatatype());
        System.out.println(entry1.getValue());

        GenericEntry<?> entry2 = myMap.get("key2");
        System.out.println(entry2.getDatatype());   
        System.out.println(entry2.getValue());
    }
}

whose output is: 其输出是:

class java.lang.Character
c
class java.lang.Integer
3

as you can see from @stephen-c's answer, the approaches may be differents, but in any case you have to define a class that containts the datatype and the value. 从@ stephen-c的答案可以看出,方法可能有所不同,但是无论如何您都必须定义一个包含数据类型和值的类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM