对 SIC 汇编器源代码进行标记

发布于 2024-07-22 04:45:29 字数 1362 浏览 8 评论 0原文

我已经基本完成了系统编程课程的 SIC 汇编程序的编写,但我对标记化部分感到困惑。

例如,采用这行源代码:

格式(自由格式)为: {LABEL} OPCODE {OPERAND{,X}} {COMMENT}

卷曲表示该字段是可选的。

此外,每个字段必须至少用一个空格或制表符分隔。

ENDFIL      LDA     EOF         COMMENT GOES HERE

上面的代码更容易组织,但下面的代码片段给我带来了困难。

        RSUB                COMMENT GOES HERE

我的代码将读取注释的第一个单词,就像它是一个操作数一样。

这是我的代码:

//tokenize line
    if(currentLine[0] != ' ' && currentLine[0] != '\t')
    {
        stringstream stream(currentLine);
        stream >> LABEL;
        stream >> OPCODE;
        stream >> OPERAND;
        stream.str("");


        if(LABEL.length() > 6 || isdigit(LABEL[0]) || !alphaNum(LABEL))
        {
            errors[1] = 1;
        }
        else if(LABEL.length() == currentLine.length())
        {
            justLabel = true;
            errors[6] = 1;
            return;
        }
    }
    else
    {
        stringstream stream(currentLine);
        stream >> OPCODE;
        stream >> OPERAND;
        stream.str("");
    }

我的教授要求使用两个版本的源代码来测试汇编器——一种有错误,一种没有错误。

RSUB 操作码不依赖于操作数,因此我知道 RSUB 操作码之后的所有内容都可以被视为注释,但是如果错误的源代码包含操作数字段中的值,或者缺少依赖于操作数的操作码OPERAND 值,我该如何补偿? 我需要将这些标记为错误并打印出错误的操作数值(或缺少该值)。

我的问题是: 如何防止代码的注释部分被视为操作数?

I've pretty much finished coding a SIC assembler for my systems programming class but I'm stumped on the tokenizing part.

For example, take this line of source code:

The format (free format) is: {LABEL} OPCODE {OPERAND{,X}} {COMMENT}

The curls indicate that the field is optional.

Also, each field must be separated by at least one space or tab.

ENDFIL      LDA     EOF         COMMENT GOES HERE

The code above is a bit easier to organize but the following snippet is giving me difficulties.

        RSUB                COMMENT GOES HERE

My code will read in the first word of the comment as if it were an OPERAND.

Here is my code:

//tokenize line
    if(currentLine[0] != ' ' && currentLine[0] != '\t')
    {
        stringstream stream(currentLine);
        stream >> LABEL;
        stream >> OPCODE;
        stream >> OPERAND;
        stream.str("");


        if(LABEL.length() > 6 || isdigit(LABEL[0]) || !alphaNum(LABEL))
        {
            errors[1] = 1;
        }
        else if(LABEL.length() == currentLine.length())
        {
            justLabel = true;
            errors[6] = 1;
            return;
        }
    }
    else
    {
        stringstream stream(currentLine);
        stream >> OPCODE;
        stream >> OPERAND;
        stream.str("");
    }

My professor requires that the assembler be tested with two versions of the source code--one with errors and one without.

The RSUB OPCODE isn't dependent on an OPERAND so I understand that everything after the RSUB OPCODE can be considered a comment, but If the erroneous source code contains a value in the OPERAND field or if an OPCODE which is dependent on an OPERAND is missing the OPERAND value, how do I compensate for this? I need to flag these as errors and print out the erroneous OPERAND value (or lack thereof).

My question is:
How do I prevent the comment portion of the code from being considered an OPERAND?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

方觉久 2024-07-29 04:45:29

在我见过的汇编语言(如其他编程语言)中,有一个标记注释的分隔符:例如注释前的分号:

ENDFIL LDA EOF ;COMMENT GOES HERE
RSUB ;ANOTHER COMMENT GOES HERE

但是,在您的语法中,您能否通过以下内容来判断某些内容是否是注释:该行前面有空格,例如操作码和注释之间有两个(不仅仅是一个)空格事件?

{LABEL}<whitespace>OPCODE<whitespace>{OPERAND{,X}}<whitespace>{COMMENT}

In the assembly languages (as in other programming languages) that I've seen, there's a delimiter that marks a comment: for example a semicolon before the comment:

ENDFIL LDA EOF ;COMMENT GOES HERE
RSUB ;ANOTHER COMMENT GOES HERE

In your syntax however, can you tell whether something is a comment by the amount of whitespace which precedes it on the line, e.g. by the fact that there are two (not just one) whitespace events between the opcode and the comment?

{LABEL}<whitespace>OPCODE<whitespace>{OPERAND{,X}}<whitespace>{COMMENT}
情话已封尘 2024-07-29 04:45:29

如何判断某一行中的文本是操作数还是注释? 是基于上下文吗? 例如,如果操作码是“RSUB”,那么您会知道不需要操作数吗? 然后,您应该根据读取的操作码对操作数执行一些魔法:

if (OPCODE == "RSUB") OPERAND.clear();

How can you tell if text in a certain line is an operand or a comment? Is it based on the context? For example, if the OPCODE is "RSUB", then you would know that there is no OPERAND required? Then you should perform some magic on the OPERAND based on what OPCODE is read:

if (OPCODE == "RSUB") OPERAND.clear();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文