Re: Checking out complex pattern matching

37 views
Skip to first unread message

Nicholas Blumhardt

unread,
Jul 8, 2011, 8:35:48 PM7/8/11
to sprache...@googlegroups.com
Hi Simon,

I think you may be better off by doing the simplest unambiguous parse possible. You can check that the identifier is semantically valid (right size numbers etc.) after parsing. This will also let you provide better error messages (trying to make the parser super-strict will just disallow input, rather than giving good descriptions of what is semantically wrong.)

Hope this helps,
Nick

On 7 July 2011 19:56, Simon Woods <simonwo...@gmail.com> wrote:
Sorry in advance for bad formatting ...

So I played around a bit more and ended up with something else - which
requires rather more rules. Am I overcomplicating it

       Public Shared ReadOnly OneDigit As Parser(Of IEnumerable(Of
Char)) = Sprache.Parse.Digit.Once
       Public Shared ReadOnly TwoDigits As Parser(Of IEnumerable(Of
Char)) = From a In OneDigit

From b In Sprache.Parse.Digit.Once

Select a.Concat(b)
       Public Shared ReadOnly ThreeDigits As Parser(Of IEnumerable(Of
Char)) = From a In TwoDigits

From b In Sprache.Parse.Digit.Once

Select a.Concat(b)

       Public Shared ReadOnly KeyIndexRange As Parser(Of
IEnumerable(Of Char)) = ThreeDigits.Or(TwoDigits).Or(OneDigit).End

       Public Shared ReadOnly Key1 As Parser(Of IEnumerable(Of Char))
= From a In Sprache.Parse.Char("M"c).Once

From b In KeyIndexRange

Select a.Concat(b)

       Public Shared ReadOnly Key2 As Parser(Of IEnumerable(Of Char))
= From a In Key1

From b In Sprache.Parse.Char("E"c).Once

From c In KeyIndexRange

Select a.Concat(b).Concat(c)

       Public Shared ReadOnly Key3 As Parser(Of IEnumerable(Of Char))
= From a In Key2

From b In Sprache.Parse.Char("I"c).Once

From c In KeyIndexRange

Select a.Concat(b).Concat(c)

       Public Shared ReadOnly DataItemKey As Parser(Of String) =
Key3.End.Text

Many thx in advance

S

On Jul 7, 6:30 am, SWo777 <simonwoods2...@gmail.com> wrote:
> Hi
>
> Just getting into Sprache .. and loving it ..  but am wondering if I
> can just check out my understanding
>
> I am wanting to parse a particular character pattern to identify a key
> in a string. The pattern can be
>
> from M1E1I1 up to M999E999I999
>
> I have created a parser for this as follows
>
> Public ReadOnly Key As Parser(Of String) =
>                           From a In Parse.Char("M"c).Once
>                           From b In Parse.Digit.AtLeastOnce
>                           From c In Parse.Char("E"c).Once
>                           From d In Parse.Digit.AtLeastOnce
>                           From e In Parse.Char("I"c).Once
>                           From f In Parse.Digit.AtLeastOnce
>                           Select New
> String(a.Concat(b).Concat(c).Concat(d).Concat(e).Concat(f).ToArray)
>
> but obviously it is not tight enough.
>
> I be grateful if you could just confirm am going in the right
> direction, or whether there is a better way to perform such a pattern
> match.
>
> Also, I was wondering whether infact it would be going against the
> grain to have a RegEx type parser/combinator
>
> eg something like
>
> Public ReadOnly Key As Parser(Of String) = Parse.Regex("^M[1-9][0-9]
> {0,2}E[1-9][0-9]{0,2}I[1-9][0-9]{0,2}").Once
>
> Though I'm not sure whether that could end up being self
> contradictory, eg any subsequent combinator may be contradicted by the
> content of the regex itself?
>
> Many thx again
>
> Simon

Simon Woods

unread,
Jul 9, 2011, 2:17:13 AM7/9/11
to sprache
Thx Nick ... so in this instance, would you check for the pattern

[CharDigits.AtLeastOnce][CharDigits.AtLeastOnce]
[CharDigits.AtLeastOnce]

or perhaps

[MDigits.AtLeastOnce][EDigits.AtLeastOnce][IDigits.AtLeastOnce]

and then have a validation call when the Select is done?

On Jul 9, 1:35 am, Nicholas Blumhardt <nicholas.blumha...@gmail.com>
wrote:
> Hi Simon,
>
> I think you may be better off by doing the simplest unambiguous parse
> possible. You can check that the identifier is semantically valid (right
> size numbers etc.) after parsing. This will also let you provide better
> error messages (trying to make the parser super-strict will just disallow
> input, rather than giving good descriptions of what is semantically wrong.)
>
> Hope this helps,
> Nick
>
Reply all
Reply to author
Forward
0 new messages