1

我正在使用 yecc 来解析我的标记化 asm-like 代码。在提供类似代码"MOV [1], [2]\nJMP hello"和词法分析之后,这就是我得到的回应。

[{:opcode, 1, :MOV}, {:register, 1, 1}, {:",", 1}, {:register, 1, 2},
  {:opcode, 2, :JMP}, {:identifer, 2, :hello}]

当我解析这个时,我得到

[%{operation: [:MOV, [:REGISTER, 1], [:REGISTER, 2]]},
  %{operation: [:JMP, [:CONST, :hello]]}]

但我希望每个操作都有行号,以便在代码中进一步获得有意义的错误。

所以我将解析器更改为:

Nonterminals
code statement operation value.

Terminals
label identifer integer ',' opcode register address address_in_register line_number.

Rootsymbol code.

code -> line_number statement      : [{get_line('$1'), '$2'}].
code -> line_number statement code : [{get_line('$1'), '$2'} | '$3'].
%code -> statement      : ['$1'].
%code -> statement code : ['$1' | '$2'].

statement -> label     : #{'label' => label('$1')}.
statement -> operation : #{'operation' => '$1'}.

operation -> opcode value ',' value : [operation('$1'), '$2', '$4'].
operation -> opcode value           : [operation('$1'), '$2'].
operation -> opcode identifer       : [operation('$1'), value('$2')].
operation -> opcode                 : [operation('$1')].

value -> integer  : value('$1').
value -> register : value('$1').
value -> address  : value('$1').
value -> address_in_register : value('$1').

Erlang code.
get_line({_, Line, _})                 -> Line.

operation({opcode, _, OpcodeName})     -> OpcodeName.

label({label, _, Value})               -> Value.

value({identifer, _, Value})           -> ['CONST', Value];
value({integer, _, Value})             -> ['CONST', Value];
value({register, _, Value})            -> ['REGISTER', Value];
value({address, _, Value})             -> ['ADDRESS', Value];
value({address_in_register, _, Value}) -> ['ADDRESS_IN_REGISTER', Value].

(评论code是旧的,工作规则)

现在我得到 {:error, {1, :assembler_parser, ['syntax error before: ', ['\'MOV\'']]}}

提供相同的输入后。如何解决这个问题?

4

1 回答 1

1

我的建议是将行号保留在标记中,而不是作为单独的标记,然后更改构建操作的方式。

所以我建议这样做:

operation -> opcode value ',' value : [operation('$1'), line('$1'), '$2', '$4'].
operation -> opcode value           : [operation('$1'), line('$1'), '$2'].
operation -> opcode identifer       : [operation('$1'), line('$1'), value('$2')].
operation -> opcode                 : [operation('$1'), line('$1')].

line({_, Line, _}) -> Line.

如果你想镜像 Elixir AST,甚至可以这样:

operation -> opcode value ',' value : {operation('$1'), meta('$1'), ['$2', '$4']}.
operation -> opcode value           : {operation('$1'), meta('$1'), ['$2']}.
operation -> opcode identifer       : {operation('$1'), meta('$1'), [value('$2')]}.
operation -> opcode                 : {operation('$1'), meta('$1'), []}.

meta({_, Line, _}) -> [{line, Line}].
于 2018-01-24T08:21:39.300 回答