Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
8

Where is the mistake in this parser? [closed]

Asked by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

I have begun on a PEG parser.

The goal is to build a grammar out of trees of combinators using sequence and either. I have some mistake in my sequence implementation, because I get false as the output from this instead of an empty stream.

01-- <library>
02 
03-- Forward declare helper
04local wrap
05 
06-- e: function | string
07-- return: Parser (Stream -> Stream?)
08function expect(e)
09    local x = e
10    if type(e) == "string" then
11        x = function(n) return n == e end
12    end
13    return wrap(function(stream)
14        if x( stream:first() ) then
15            return stream:rest(), stream:first()
View all 72 lines...
01-- <client>
02 
03-- string -> boolean
04function isLetter(s)
05    return s:sub(1, 1):lower() ~= s:sub(1, 1):upper()
06end
07 
08local name = expect(isLetter)
09local open, close = expect("<"), expect(">")
10 
11local void = open * name * close
12local tokens = Stream {"<", "img", ">"}
13 
14print( void(tokens) )
15-- I expect this to print `table: ABCDEF`, but I get `false`
16-- </client>

Does anyone else have an idea of where I made a mistake? I regret not testing more incrementally...

0
that needs a bluetaslem answer! oh, wait a minute... unmiss 337 — 9y
0
Try & remake it but more incrementally. Redbullusa 1580 — 9y
0
According to the Studio, you have not defined 'either' on line 46 (This may not be the main problem, but I thought to point it out anyway). :) TheeDeathCaster 2368 — 9y
2
b(stream) = false and as said before, "either" was never defined for __add = either Validark 1580 — 9y
0
I took out the definition of either because it wasn't used meaningfully in this script, forgot it was in the metatable -- that's not the problem. Edit the code to fix that BlueTaslem 18071 — 9y

Locked by Avigant

This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.

Why was this question closed?

1 answer

Log in to vote
4
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

I found the bug -- it was a little mistake in this line:

1local u = a(stream)
2if u then
3    return b(stream)

Looking at it, the definition of local u is suspicious, since u is only used in the condition.

It should have been this:

1local u = a(stream)
2if u then
3    return b(u)

so that the rest of the stream after what a takes it put into b.

0
Yes, good answer, oh, great BlueTaslem! Redbullusa 1580 — 9y
2
I'm calling this one! Look at my other comment and I pointed out the error in "b(stream)" Validark 1580 — 9y
0
wow, he answered his own quesito. User#5978 25 — 9y
Ad