Search code examples
regexstring-matching

MIPS registers regex


Im working in a mips assembly parser, using yacc and lex and I need to recognize the registers in the given instructions. That registers can be:

  • $t0,$t1,...$t31
  • $zero
  • r0,r1,...,r31
  • R0,R1,...,R31

I tried to do the first part ($t0,...,$t31 and R0,...$R31), but it has not worked. My regex is (\$t|R)([1|2][0-9]|3[0-1]|[0-9]). As you can see in the image 1, the regular expression matchs R5 in the R56 register, and this is not desired. How can I solve this problem?

Regex test from RegExr


Solution

  • I would use:

    ((?:\$t|[rR])(?:[12]?[0-9]|3[01]))\b|(\$zero)
    

    to match:

    • $t or R or r followed by a number from 0 to 32 (thanks to the word boundary \b)
    • $zero

    See live demo.