背景: 私はコンパイラーを作成しています。スキャナーとパーサーは既に完成しています。プログラムを次のように定義する正式な文法があります。
program =
declarations
procedureDeclarations
main ( ){
declarations
statementSequence
}
したがって、宣言はメイン メソッドに対してグローバルまたはローカルのいずれかであることがわかります。したがって、解析するときに、次の 3 つのことを格納する方法が必要です: 入ってくる識別子が定数であるか変数であるか (parseConst() または parseVar() のどちらの解析方法を使用しているかがわかります)、グローバルまたはローカル、その実際の名前、およびその値 (まだわかっている場合)。
これをどうやって収納しようか迷っています。私のデータ構造クラスでは、実際に格納する必要があるのは、キーと値の 2 つだけです。だから私は次のようなものがあります:
identHashMap<String, String> // where the first string is the ident's name, and the second is if it's a constant or var (this could be a boolean also)
constantIdentifiers<String, String> // first string is the ident's name, second is whether it's local/global (could be a string)
varIdentifiers<String, String> // first string is the ident's name, second is whether it's local/global (could be a string)
identifierValues<String, Integer> // ident's name, Ident's value
単純なタスクにはデータ構造が多すぎるようです。クラス Identifier を作成し、Global/local ブール値フィールドと定数/var ブール値を持つ必要がありますか? そして、それらすべてを 1 つの識別子ハッシュマップに入れますか?
ありがとう!