Yes. If basic assembler.
No. If you're going to implement an assembler that supports constants and symbols which can be combined into expressions. And expressions can easily be parsed for example[0]:
〈primary expression〉->〈constant〉|〈symbol〉|〈local operand〉|@|
(〈expression〉)|〈unary operator〉〈primary expression〉
〈term〉->〈primary expression〉|〈term〉〈strong operator〉〈primary expression〉
〈expression〉->〈term〉|〈expression〉〈weak operator〉〈term〉
〈unary operator〉-> + | − | ~ | $ | &
〈strong operator〉-> * | / | // | % | << | >> | &
〈weak operator〉-> + | − | | | ^
Your code might be hard to read if you'll not create a parse tree for it.
Re:Parsing RISC-V assembly
by tom_mellior on Tuesday October 27, 2020 @01:44PM
(#24909863)
> Huh? Parse tree for assembly?
Why not? The structure you need to represent something like
lea eax, [eax + eax * 4]
is tree-shaped. It's not as general as many other parse trees, since you don't have arbitrary nesting. But it is a tree-shaped thing that represents a parse, and if you need a name for it, "parse tree" is reasonable. The list containing all the labels/instructions/whatnot in the program is also a trivial tree.(I'm aware that RISC-V addressing modes are probably simpler than the x86 example above.)
It's assembly, not anything like C, C++, etc. It's simple. You're supposed to be able to handle it with regexes.
> If you are looking for a hand-written lexer and parser for RISC-V assembly that builds a parse tree
Huh? Parse tree for assembly?
> For example, "xor xor, xor, xor, xor" will be parsed just fine, despite it not being legal RISC-V assembly.
Then ... why parse it??????
See, this is what happens when you refer to assembling as compiling. They are not the same.