Matching similar terms in ANTLR without capturing difference

130 views Asked by At

As part of the nand2tetris challenge I'm trying to write a parser using ANTLR to generate machine code, having already implemented it using regex.

However, I'm struggling to work out how to use ANTLR effectively. A subset of the problem is below.

(Some) Valid instructions

 M=D
 D=M
 M=D+1
 D;JMP
 0;JMP

A (partial) Regex

(?<assignment>(?<destination>[ADM])=)?(?<computation>[ADM+10])(?<condition>;(?<jump>JMP))?

A (partial) grammar

command
     : assignment '=' computation
     | computation ';' condition
     | assignment '=' computation ';' condition
     ;

assignment
     : ASSIGNMENT
     ;

computation
     : OPERATION
     ;

condition
     : CONDITION
     ;

ASSIGNMENT
     : DESTINATION
     ;

CONDITION
     : JUMP
     ;

DESTINATION
     : 'A'
     | 'D'
     | ...etc
     ;

OPERATION
     : 'A'
     | 'D'
     | 'A+D'
     | ... etc
     ;

JUMP
     : JMP
     | JLE
     | etc...
     ;

Now, as you can see, the lexer will get mixed up between what is an computation and what is an assignment, as both could be 'A'...

However, if I change the ASSIGNMENT to

ASSIGNMENT
  : DESTINATION '='
  ;

and command to

command
  : assignment computation
  | etc...

then assignment picks up the equals sign.

So, I am trying to match under two tokens (FOO and FOO=) in different contexts, but I'm not interested in the =, only the FOO.

Am I barking up the wrong tree entirely with the current approach?

0

There are 0 answers