StringOfBytes : byte NBytes { $$.value = $2.value; $2.length = $1.value } NBytes : {- empty -} { $$.value = []; unless $$.length == 0 error } | byte NBytes { $$.value = $1.value : $2.value; $2.length = $$.length - 1 }

StringOfBytes

NBytes

NBytes

length

NBytes

StringOfBytes : byte TakeN { $$.value = $2.value; $2.length = $1.value } TakeN : (take $$.length INPUTSTREAM) { $$ = $1 }

take

length

... and I'm just too much of an idiot to see it, so maybe one of you smart people can help me.I'm writing a parser for a context-sensitive grammar in Haskell (for a language that I didn't design). At the moment I'm using Happy but I'm completely open to using parsec, uuagc, whatever will get the job done. Here's my problem: All tokens of this language are bytes. One of the nonterminals has, as its right-hand side, a byte (which evaluates to an integer value -- I have an attribute rule for this) followed by N bytes, where N is whatever that first byte evaluated to. The usual attribute-grammar way to handle this seems to look a bit like this (using an inherited attribute, and OBTW this is pseudocode):Unfortunately, in most cases there's more content past the end of the, and the way this rule is written,will happily keep shifting tokens (bytes) until it runs out of input and finally reduces. This is obviously bad.If I had some way to forceto reduce whenever theof the nestedreaches 0, that would solve it, but I don't see a way to impose a precedence on an attribute rule. But that's not even a very Haskelly way to do it; what I'd really like to do is more like:...because, I mean, that's whatis for, lazily grabbing N elements of a list. How to do this, though, is not immediately obvious. Having one of my token types be a partial function that I then somehow bind to the appropriatevalue would also be cool, but again I don't see how to do this.I'm thinking monadic parsing might be a solution, since monadic parsing lets you keep track of state and do various context-dependent things ... but I'm at a loss as to how to actually do this. I've pored through the sample code that ships with Happy, but lack sufficient clue to figure out what to do with it. If anyone can provide some guidance with this, I'd be extremely grateful.