Bottom-Up-Parser: When to apply which reduction rule?
Let's take the following context-free grammar:
G = ( {Sum, Product, Number}, {decimal representations of a numbers, +,
*}, P, Sum)
Being P:
Sum ¨ Sum + Product
Sum ¨ Product
Product ¨ Product * Number
Product ¨ Number
Number ¨ decimal representation of a number
I am trying to parse expressions produced by this grammar with a
bottom-up-parser and a look-ahead-buffer (LAB) of length 1 (which
supposingly should do without guessing and back-tracking).
Now, given a stack and a LAB, there are often several possibilities of how
to reduce the stack or whether to reduce it at all or push another token.
Currently I use this decision tree:
If any top n tokens of the stack plus the LAB are the begining of the
right side of a rule, I push the next token onto the stack.
Otherwise, I reduce the maximum number of tokens on top of the stack. I.e.
if it is possible to reduce the topmost item and at the same time it is
possible to reduce the three top most items, I do the latter.
If no such reduction is possible, I push another token onto the stack.
Rinse and repeat.
This, seems (!) to work, but it requires an awful amount of rule
searching, finding matching prefixes, etc. No way this can run in O(NM).
What is the standard (and possibly only sensible) approach to decide
whether to reduce or push, and in case of reduction, which reduction to
apply?
Thank you in advance for your comments and answers.
No comments:
Post a Comment