Q: How would I match something before anything else? (ANTLR4) Currently I'm implementing labels into my code. It works really well already and backward labels are working. (Backward label = Label has to be defined before I can jump to it) Now I want to make "forward" label. A short explanation what I mean (I illustrate it in my code)
!Test:
LUI A0, 0x144D
JAL 0x8033D548

!Main:
ADDIU T0, T1, 0x0001
BEQ T0, T1, !Test ; Branch if T0 == T1

The above works already, as test was defined before and therefore I can jump "backwards" from it. However, the following won't work (yet):
!Test:
LUI A0, 0x144D
JAL 0x8033D548
NOP
BEQ T0, T1, !Main // Talking about this one here. Forward Label Jump.

!Main:
ADDIU T0, T1, 0x0001
BEQ T0, T1, !Test // Branch if T0 == T1, Backward Label Jump

Basically I would've two ways: I either write a whole new function for forward labels or I find out how I can match "label definitions" (!Labelname:) before matching anything else. I believe the 2nd method would be the best and shortest method. So, I'm asking if it's possible to match my labels BEFORE anything else?
Here is my grammar:
mips32code : instruction+                                       # StartPlace
           ;

instruction : OPCODE_ITYPE rt COMMA rs COMMA VALUE imm              # ITypeInstruction
            | OPCODE_LUITYPE rt COMMA VALUE imm                     # LuiTypeInstruction
            | LABELD NAME LABELDEF                                  # LabelDefinitionType
            ;

imm         : instruction # immformat
            | INT    # immvalue
            ;

rs          : instruction # rsexpr
            | REG    # rsreg
            ;

rt          : instruction # rtexpr
            | REG    # rtreg
            ;


OPCODE_ITYPE
  : [aA] [dD] [dD] [iI] // ADDI
  | [dD] [aA] [dD] [dD] [iI] // DADDI
  | [dD] [aA] [dD] [dD] [iI] [uU] // DADDIU
  | [aA] [dD] [dD] [iI] [uU] // ADDIU
  | [oO] [rR] [iI] // ORI
  | [xX] [oO] [rR] [iI] // XORI
  | [sS] [lL] [tT] [iI] // SLTI
  | [sS] [lL] [tT] [iI] [uU] // SLTIU
  | [aA] [nN] [dD] [iI] // ANDI
  ;

OPCODE_LUITYPE
  : [lL] [uU] [iI] // LUI
  ;

REG
: [rR] '0'
| [aA] [tT]
| [vV] [01]
| [aA] [0-3]
| [tT] [0-9]
| [sS] [0-8]
| [kK] [01]
| [gG] [aA]
| [sS] [pP]
| [fF] [pP]
| [rR] [aA]
;

LABELD : '!' ;
LABELDEF : ':' ;
NAME  : [A-Za-z]+;
INT   : [0-9A-Fa-f]+;
VALUE : ('$'|'0x')+;
COMMA : ',' ;


A: Parse the input twice. Your first parse, which could be a listener that only implements the enterLabelDefinitionType(...) method, "collects" the labels. Then you parse it a seconds time and pass the result from the first parse to it.

A: What you are trying to do is to do semantic work in the syntactic phase. You shouldn't do that. Let the parser do what it is made for, to parse your input and give you the structure. Then write an additional phase (the semantic phase) to examine this structure and validate all labels. Here you can give meaningful errors instead of generic "cannot match any predicted input..." errors from the syntactic phase.
