如何制作符号表

时间:2011-12-16 13:23:55

标签: java compiler-construction symbol-table

我们有作为编译器的作业。我们已经进行了词法和语法分析,但我们仍然坚持生成中间代码。我们意识到我们必须实现一个符号表,以便进行中间代码生成,我们不知道,如何做以及它包含什么。

根据下面的代码,符号表应该包含什么? (代码用教育语言编写,如下所述)

另外,我们如何在符号表中实现范围?

<PROGRAM> ::= PROGRAM ID <BLOCK> ENDPROGRAM
<BLOCK> ::= {<DECLARATIONS> <SUBPROGRAMS> <SEQUENCE>}
<DECLARATIONS> ::= ε | DECLARE <VARLIST> ENDDECLARE
<VARLIST> ::= ε | ID ( , ID )*
<SUBPROGRAMS> ::= ( <PROCORFUNC> ) *
<PROCORFUNC> ::= PROCEDURE ID <PROCORFUNCBODY> ENDPROCEDURE |
FUNCTION ID <PROCORFUNCBODY> ENDFUNCTION
<PROCORFUNCBODY> ::= <FORMALPARS> <BLOCK>
<FORMALPARS> ::= ε | ( <FORMALPARLIST> )
<FORMALPARLIST> ::= <FORMALPARITEM> ( , <FORMALPARITEM> )*
<FORMALPARITEM> ::= IN ID | INOUT ID
<SEQUENCE> ::= <STATEMENT> ( ; <STATEMENT> )*
<STATEMENT> ::= ε | <ASSIGNMENT-STAT> |
<IF-STAT> |
<WHILE-STAT> |
<FOR-STAT> |
<EXIT-STAT> |
<CALL-STAT> |
<RETURN-STAT>
<ASSIGNMENT-STAT> ::= ID := <EXPRESSION>
<IF-STAT> ::= IF <CONDITION> THEN <SEQUENCE> <ELSEPART> ENDIF
<ELSEPART> ::= ε | ELSE <SEQUENCE>
<WHILE-STAT> ::= DO {<SEQUENCE>} WHILE (<CONDITION>)
<FOR-STAT> ::= (<ASSIGNMENT-STAT>; <CONDITION>;<ASSIGNMENT-STAT>;)
{<SEQUENCE>}
<EXIT-STAT> ::= EXIT
<CALL-STAT> ::= CALL ID <ACTUALPARS>
<ACTUALPARS> ::= ( <ACTUALPARLIST> ) | ε
<ACTUALPARLIST> ::= <ACTUALPARITEM> ( , <ACTUALPARITEM> )*
<ACTUALPARITEM> ::= IN <EXPRESSION> | INOUT ID
<RETURN-STAT> ::= RETURN <EXPRESSION>
<CONDITION> ::= <BOOLTERM> (OR <BOOLTERM>)*
<BOOLTERM> ::= <BOOLFACTOR> (AND <BOOLFACTOR>)*
<BOOLFACTOR> ::= NOT [<CONDITION>] | [<CONDITION>] |
<EXPRESSION> <RELATIONAL-OPER> <EXPRESSION> |
TRUE | FALSE
<EXPRESSION> ::= <OPTIONAL-SIGN> <TERM> ( <ADD-OPER> <TERM>)*
<TERM> ::= <FACTOR> (<MUL-OPER> <FACTOR>)*
<FACTOR> ::= CONSTANT | (<EXPRESSION>) | ID <IDTAIL>
<IDTAIL> ::= ε | <ACTUALPARS>
<RELATIONAL-OPER> ::= = | < ( ε | = | > ) | > ( ε | = )
<ADD-OPER> ::= + | -
<MUL-OPER> ::= * | /
<OPTIONAL-SIGN> ::= ε | <ADD-OPER>
PROGRAM MULTIPLY
    {
    DECLARE
    A, B, C
    ENDDECLARE
    PROCEDURE Aop(INOUT A)
    {
        A=A+1;
    }
    ENDPROCEDURE
    FUNCTION Bop(IN B){
        IF [NOT[[TRUE AND FALSE]OR[TRUE]]] THEN B := 100 / 2;
        ELSE B := 100;
        ENDIF;
        RETURN B;
        }
    ENDFUNCTION
    CALL Aop(INOUT A);
    CALL Bop(IN B);
    A := 40;
    C := A * B;
    }
ENDPROGRAM

1 个答案:

答案 0 :(得分:6)

符号表将标识符(通常以作用域名称为前缀)映射到有关该标识符的信息,例如其符号类型(局部变量/参数/函数/类等),数据类型,相对于其他标识符的顺序可以通过遍历抽象语法树来生成符号表,通过始终跟踪您所在的范围并在每次点击变量声明时向符号表添加信息。在您的示例中,符号表的一部分可能如下所示(映射到符号类型,数据类型,位置和源代码行):

MULTIPLY.A -> {"LOCAL", "INT", 0, 4}
MULTIPLY.B -> {"LOCAL", "INT", 1, 4}
MULTIPLY.C -> {"LOCAL", "INT", 2, 4}
MULTIPLY.Aop -> {"FUNCTION", "INT", 3, 4}
MULTIPLY.Aop.A -> {"INOUTPARAM", "INT", 0, 6}

现在,您可以解析所有变量引用。例如,在表达式A := A + 1中,如果您知道当前作用域为MULTIPLY.Aop,则symnbol表将让您发现此A是类型{的输入/输出参数{1}},它是第一个参数(此信息将允许您生成堆栈地址偏移量,以便您可以加载/存储变量)。