我试图创建一个方法,它根据正则表达式检查一个字符串并返回一个寄存器类型(mips)。问题是,我似乎无法创建正确的正则表达式。 请看看并提出建议。由于正则表达式解析器的问题
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:现在一切工作正常,但不包括我的记忆正则表达式
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;
}
什么部分回事?你在哪里不匹配。注意,我相信你需要在临时寄存器匹配表达式 – pstrjds 2012-03-09 18:15:07
中转义'$'我还有一个问题(因为我写MIPS汇编已经有多年了),你的源代码是一个完整的指令吗?您的匹配k个寄存器的表达式是空白的?你的匹配“记忆”表达式也是一个空白的正则表达式。你可以给一些'source'的例子吗? – pstrjds 2012-03-09 18:23:11
@pstrjds参考内存例如:lw $ t7,248($ t2) – 2012-03-09 19:46:55