编写Z80汇编器-编写ASM词法并使用合成来构建解析树? [英] Writing a Z80 assembler - lexing ASM and building a parse tree using composition?

查看:195
本文介绍了编写Z80汇编器-编写ASM词法并使用合成来构建解析树?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对编写汇编程序的概念很陌生,即使在阅读了大量材料后,我仍然很难将脑袋围绕着几个概念.

I'm very new to the concept of writing an assembler and even after reading a great deal of material, I'm still having difficulties wrapping my head around a couple of concepts.

  1. 实际上将源文件分解为令牌的过程是什么?我相信这个过程称为词法化,我在高低两下搜索了一个有意义的真实代码示例,但是我找不到一个非常受欢迎的简单代码示例;)

  1. What is the process to actually break up a source file into tokens? I believe this process is called lexing, and I've searched high and low for a real code examples that make sense, but I can't find a thing so simple code examples very welcome ;)

解析时,是否需要在树上向上或向下传递信息?我问的原因如下:

When parsing, does information ever need to be passed up or down the tree? The reason I ask is as follows, take:

LD BC,nn

一旦被标记化(???),它就需要变成下面的解析树

It needs to be turned into the following parse tree once tokenized(???)

  ___ LD ___
  |        |
 BC        nn

现在,遍历此树时,它需要产生以下机器代码:

Now, when this tree is traversed it needs to produce the following machine code:

01 n n

如果指示是:<​​/p>

If the instruction had been:

LD DE,nn

然后输出将是:

11 n n

这意味着它提出了一个问题,LD节点根据操作数返回的内容是否有所不同,还是操作数返回的内容?以及如何实现呢?如果时间允许,更简单的代码示例将非常有用.

Meaning that it raises the question, does the LD node return something different based on the operand or is it the operand that returns something? And how is this achieved? More simple code examples would be excellent if time permits.

我最想在这里学习一些原始过程,而不是查看现有的高级工具,因此请记住这一点,然后再将我发送到 Flex .

I'm most interested in learning some of the raw processes here rather than looking at advanced existing tools so please bear that in mind before sending me to Yacc or Flex.

推荐答案

好吧,您真正想要的树的结构 对寄存器和存储器进行操作的指令 涉及偏移位移和索引寄存器的寻址模式 看起来像这样:

Well, the structure of the tree you really want for an instruction that operates on a register and an memory addressing mode involing an offset displacement and an index register would look like this:

    INSTRUCTION-----+
    |      |        |
  OPCODE  REG     OPERAND
                  |     |
                OFFSET  INDEXREG

是的,您想在树上上下传递值. 正式指定这种值传递的方法称为 属性语法",然后为您装饰语法 带有值传递的语言(在您的情况下为汇编语法) 以及这些值的计算.要了解更多背景, 请参见关于属性语法的维基百科.

And yes, you want want to pass values up and down the tree. A method for formally specifying such value passing is called "attribute grammars", and you decorate the grammar for your langauge (in your case, your assembler syntax) with the value-passing and the computations over those values. For more background, see Wikipedia on attribute grammars.

在您问的相关问题中,我讨论了 工具, DMS , 处理表达式语法和构建树.作为 语言操作工具,DMS上下完全一样 树信息流问题.这不应该让你感到惊讶, 作为高端语言操作工具,它可以处理 直接进行语法计算.

In a related question you asked, I discussed a tool, DMS, which handles expression grammars and building trees. As language manipulation tool, DMS faces exactly these same up-and-down the tree information flows issues. It shouldn't surprise you, that as a high-end language manipulation tool, it can handle attribute grammar computations directly.

这篇关于编写Z80汇编器-编写ASM词法并使用合成来构建解析树?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆