Search code examples
ebnf

EBNF: prefix and suffix-like operator in assembly code production


I'm trying to write down 6809 assembly in EBNF to write a tree-sitter parser.

I'm stuck on one certain production. In 6809 assembly, you can use a register as an operand and additionally de- or increment it:

LDA 0,X+     ; loads A from X then bumps X by 1
LDD ,Y++     ; loads D from Y then bumps Y by 2
LDA 0,-U     ; decrements U by 1 then loads A from address in U
LDU ,--S     ; decrements S by 2 then loads U from address in S

Mind the "missing" first operand in the second line of code. Here are the productions I wrote:

instruction = opcode, [operand], ["," , register_exp];
...
register_exp = [{operator}], register | register, [{operator}];
register = "X" | "Y" | "U" | etc. ;
operator = "+" | "-";

The problem is register_exp = .... I feel like there could be a more elegant way to define this production. Also, what happens if only a register is given to register_exp?


Solution

  • You probably need

    register_exp = [{operator}], register | register, [{operator}] | register;
    

    to allow register names without operators. Why do you find it not so elegant? Quite descriptive.