JavaCC parser from Assembly Language to Machine Code -instruction separation problem
HY.I'm trying to make a parser using JavaCC (an assembler) to transform from assembly code (Microcontroller 8051) to Machine COde.I have read about the javaCC grammar and the way it is structured but i have a dilemma.For example I have the ADD
instruction:
`ADD A,Rn` or `ADD A,@Ri`
and for each of them i have a Machine code (hexa code)ex: ADD A,R0
translates to 28H .
And also i can have the MOV
instruction :
MOV A,Rn
or MOV A,@Ri
but i aloso have MOV data_addr,Rn
and MOV R6,#data
and so on .
Now my problem is how do i make t开发者_StackOverflow社区his difference between 2 instructions.Supose i define my tokens like this:
Token{
<IN_MOV :"mov">
|<IN_ADD:"add"
}
i couldn't define functions for each token a function to specify a specific behavior because i have many instructions.To say that token.image==.equals("mov"), then go on one direction to the specific behaviour
it is a little much , don't you think?....so i`m pretty much stuck.I don't know wich way to go .
Thx for the help.!It seems you expect too much from the lexer. The lexer is a finite state machine, while the parser is not.
So the lexer should produce tokens for the instructions (MOV
, ADD
, ...) and tokens for the operands. The lexer should not try to be too clever and expect specific operands for specific instructions.
Now the parser can expect specific combinations of instructions and operands. For example, you can accept only @
operands with the MOV
instruction, so that any other operand will cause a parse exception.
If you need to further validate the combination of instructions and operands, you have to do it in the code of the productions. For example, you can treat two identical operands as an error for some instructions; this is very difficult to express in a production but trivial in code.
If you need to validate even further, for example by detecting invalid sequences of instructions, then you will have to maintain a state across the productions, or even build an AST and process it after the parsing is complete.
See this complete assembly language grammar for lots of examples of the kinds of things you need to write in your parser for assembler code.
精彩评论