Antarr Byrd
Antarr Byrd

Reputation: 26131

Trouble with regular expressions parser

Im trying to create a method that checks a string against a regular express and returns a register type(mips). The problem is that I can't seem to be able to create the correct regex. Please take a look and make suggestions. Thanks

 public static RegisterType CheckRegex(this string source)
        {
            var tempMatch = new Regex("$t0|$t1|$t2|$t3|$t4|$t5|$t6|$t7|$t8|$t9|").Match(source);  //$t0 - $t9
            if(tempMatch.Length == source.Length)
                return RegisterType.Temporary;
            var storeMatch = new Regex(@"(^\$s)+[0-9]").Match(source);  //$s0 - $s9
            if (storeMatch.Length == source.Length)
                return RegisterType.Store;
            var reservedMatch = new Regex(@"").Match(source);            //$k0 - $k1
            if (reservedMatch.Length == source.Length)
                return RegisterType.OSReserved;
            var constantMatch = new Regex(@"0-9").Match(source);        //Any integer
            if (constantMatch.Length == source.Length)
                return RegisterType.Constant;
            var memoryMatch = new Regex("").Match(source);
            if (memoryMatch.Length == source.Length)
                return RegisterType.Memory;

            return RegisterType.Invalid;
        }

UPDATE: Everything is working now ,excluding my Memory Regex

public static RegisterType GetRegisterType(this string source)
        {
            if (Regex.IsMatch(source, @"\$t[0-9]"))
                return RegisterType.Temporary; // $t0 - $t9
            if (Regex.IsMatch(source, @"\$s[0-9]"))
                return RegisterType.Store; // $s0 - $s9
            if (Regex.IsMatch(source, @"\$k[0-1]"))
                return RegisterType.OSReserved; // $k0 - $k1
            if (Regex.IsMatch(source, @"[-+]?\b\d+\b"))
                return RegisterType.Constant;
            if (Regex.IsMatch(source, @"\$zero"))
                return RegisterType.Special;
            if (Regex.IsMatch(source, @"[a-zA-Z0-9]+\b\:"))
                return RegisterType.Label;
            if (Regex.IsMatch(source, @"\d+\b\(\$[s-t]\b[0-9])"))
                return RegisterType.Memory;
            return RegisterType.Invalid;

        }

Upvotes: 1

Views: 95

Answers (3)

pstrjds
pstrjds

Reputation: 17438

If your source is just a register/memory location, you could probably simplify this thing down to something like this:

public static RegisterType CheckRegex(this string source)
{
    if (Regex.IsMatch(@"\$\t\d")) return RegisterType.Temporary; // $t0 - $t9
    if (Regex.IsMatch(@"\$\s\d")) return RegisterType.Store; // $s0 - $s9
    if (Regex.IsMatch(@"\$\k\[0-1]")) return RegisterType.OSReserved; // $k0 - $k1
    if (Regex.IsMatch(source, @"\d")) return RegisterType.Constant;
    // Don't remember the pattern for Memory, if you post an update I can update this

    return RegisterType.Invalid;
}

Upvotes: 1

Lance U. Matthews
Lance U. Matthews

Reputation: 16612

As others have said, you need to escape the dollar signs in "$t0|$t1|$t2|$t3|$t4|$t5|$t6|$t7|$t8|$t9|" by prefixing them with a backslash. Also, you can write that more concisely as @"\$t[0-9]". That will match a dollar sign followed by 't' followed by a single digit. You've got a trailing pipe character followed by nothing, as well, that can be removed.

Upvotes: 3

ebutusov
ebutusov

Reputation: 573

$ is a special character in regular expression, matches at the end of the line. If you want to match $ literal, use escaping (\$)

Upvotes: 3

Related Questions