A parser that can handle partial programs (was Re: Now, to try again...)

[email protected] ("Bradley M. Kuhn") Sun, 17 Dec 2000 12:41:13 -0500
Newsgroups perl.perl6.internals.api.parser
Message-ID <[email protected]>
--S6B99WLZuskppeJo
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

Nicholas Clark <[email protected]> wrote:

> Something I though of:
> If you're trying to write an interactive perl inputer - either a perl shell
> or just the command prompt on the debugger it would be useful if you
> could tell the parser that the chunk of source you're giving it may be
> incomplete.

I really like this idea, although I am unsure of how we might implement it.
I don't recall any of the compiler texts I have read over the years talking
about formal methods for writing such a "partial programs are acceptable"
parser.


Lisp-like languages handle this, but s-expressions are so trivial to parse
that it's no help to simply "follow" their example.


Perhaps, since we are doing recursive decent, we could have a user-definable
error function.  If all possible errors have well-defined types, the caller
could define an error function that informs rather than croaks and could
prompt like this:
 
foo$ print "Hello world <RET>
foo (unclosed double-quote)$

Inside, this would be something like (sorry for writing in pseudo-Perl,
instead of pseudo-C):

$PROMPT = "foo$";
$interp = new Perl::Language::Interpeter();
$done = 0;

my $handleError = sub ($) {
    my($errorID) = @_;
    if (! $errorID) {
        $interp->Eval($parseTree); $done = 1;
    }
    if ($errorID == $UNCLOSED_DOUBLE_QUOTE) {
          $PROMPT = "foo (unclosed double-quote)$";
    }
    [...]
}

while (! $done)  {
   print "$PROMPT ";
   $string .= <STDIN>; chomp $string;
   $parseTree = $interp->Parse($string, $handleError);
}


-- 
Bradley M. Kuhn  -  http://www.ebb.org/bkuhn

--S6B99WLZuskppeJo
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.4 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQE6PPq553XjJNtBs4cRAr8/AJ9FgsgYEBCqTrwjv57n/tyf6wdgNwCfToG3
LYRWTjc32I7XHpnivmJv0fw=
=K62J
-----END PGP SIGNATURE-----

--S6B99WLZuskppeJo--