meta data for this page
  •  

This is an old revision of the document!


LOOKBEHIND

The LOOKBEHIND instruction allows you to add conditions at choice points based on scanning back in the call/lookahead stack. The easiest way to describe this is with some actual examples.

Probably the most typical usage will be to guarantee that a production is not re-entrant, i.e. that it is not allowed to nest recursively. This could be expressed as follows:

   [ SCAN ~\...\Foo => Foo ]

First of all, the tilde “~” character that starts the predicate indicates negation. The above predicate indicates that we scan backward in the call stack to see whether we have previously entered a Foo production. If that is not the case (because the condition is negated with the “~”) then we can enter the Foo production. Note that a lookbehind predicate begins with either a backslash “\” or a forward slash “/”. The backslash means that we can backwards towards the root production, and the forward slash means that we are scanning forward from the root.

In the above example, the ellipsis “…” that follows the backslash means that there can be an arbitrary number of intervening productions in the call stack. If, for example, we wrote:

  [ SCAN ~\.\Foo => Foo] 

this would mean that we enter the Foo production only if the parent of the current production (which might or might not be a Foo) is a Foo.

Or alternatively,

[ SCAN \.\Bar ⇒ Foo ]

would mean that we enter the Foo production if the parent of the current production is a Bar. (Note that this predicate does not start with a “~”, so thus is not negated.

So, consider the following predicate:

    [ SCAN /Foo/Bar => Baz ]

This means that we enter the Baz production only if the root production is a Foo and we then entered a Bar.

If the predicate begins with a forward slash, it may end optionally with a backslash. And vice versa. If a predicate begins with a backslash, it may optionally end with a forward slash. For example, the following predicate:

   [ SCAN /Root/.../Foo\ => Bar ]

This means that we check whether the root production, our entry point, was Root and the current production is Foo. The ending backslash means that Foo must be the current production. Note also that the following two predicates are equivalent:

    [ SCAN /Root/.../Foo/...\ => Bar]

and simply:

   [ SCAN /Root/.../Foo => Bar]

Recap

A lookbehind predicate starts optionally with a tilde “~” to indicate negation. The first character after the tilde (or simply the first character if there is no tilde) must be either a backslash or a forward slash. The backslash indicates that we are scanning backwards from the current production and the forward slash is that we are scanning forward from the current production.

A LOOKBEHIND predicate starts optionally with a tilde “~” character and then either a forward slash or a backslash. The backslash means that we are scanning from the current production backwards, while the forward slash