|
|
Invisible wrote:
> - There can be *zero* or more characters before the decimal point. (But
> notice that there must be more than zero characters *in total* before
> and after the decimal point. It's just that there can be zero in either
> place, but not both.)
A quickie C# example ...
Code:
class Program
{
static string[] tokens = new string[] {"0.", ".0", ".", "1.1",
"1.1.1", "1e1", "1x1", "s1", "1s"};
static void Main(string[] args)
{
Regex rx = new
Regex(@"^[+-]?(([0-9]*(\.[0-9]+)?)|([0-9+]\.))(e[0-9]+)?$");
foreach(string t in tokens)
{
if(rx.Match(t).Success)
{
Console.WriteLine("\"{0}\" --> Real", t);
}
else
{
Console.WriteLine("\"{0}\" --> Identifier", t);
}
}
System.Console.ReadKey();
}
Output:
"0." --> Real
".0" --> Real
"." --> Identifier
"1.1" --> Real
"1.1.1" --> Identifier
"1e1" --> Real
"1x1" --> Identifier
"s1" --> Identifier
"1s" --> Identifier
The relavent portion is this regex:
^[+-]?(([0-9]*(\.[0-9]+)?)|([0-9+]\.))(e[0-9]+)?$
Which does what Warp describes, but also takes care of the case with .0
and 0.
> - The "e" can also be "E".
you can easily replace the e for exponent with [eE], which allows either
lower or upper case. :)
> - The exponent is an integer, not a real.
>
> See? Not as easy as it looks, is it? Gotta pay careful attention to
> *exactly* what the manual says is and isn't permissible.
>
>> In an actual BNF-style parser the rule probably becomes simpler because
>> the repetition can be removed.
>
> It would be *really nice* if the reference manual included a BNF syntax
> diagram... :-S
>
> Between the rules for splitting up tokens, the tricky rules for escaping
> things in strings, and characters that have multiple meanings depending
> on context, it's really quite hard!
>
> E.g., "<" is the start of a string, "<~" is the start of another string,
> and "<<" is an ordinary name object. Go figure. Similarly, "[" is
> classified as a "delimiter character", yet it's also the *name* of an
> operator (and a name is a sequence of "regular characters" - that is,
> can't contain delimiters).
>
> It all gets confusing very fast...
--
~Mike
Post a reply to this message
|
|