Tuesday, June 30, 2015

Predictions (Nearly) there

Well,

After reaching LL(1) status earlier, I've been working on making sure the predictions are accurate.  After a lot of reworking they appear to be getting closer.

A preview is below:


private int _Predict2OnMultExp(int laDepth)
{
    int exitLADepth = -1;
    int currentLADepth = laDepth;
    int exitState = -1;
    this._state = 20;
PredictMultiTargetState_20:
    switch (this._symbolStream.LookAhead(currentLADepth))
    {
        case SCReaderSymbols.SCRMultExpRecursive:
            exitState = this._state = 21;
            exitLADepth = ++currentLADepth;
            switch (this._symbolStream.LookAhead(currentLADepth))
            {
                case SCReaderSymbols.SCTOperators_Divide:
                case SCReaderSymbols.SCTOperators_Modulus:
                case SCReaderSymbols.SCTOperators_Multiply:
                    exitState = this._state = 25;
                    exitLADepth = ++currentLADepth;
                    goto PredictMultiTargetState_25;
                default:
                    this._symbolStream.PopNone();
                    return 12;
            }
        case SCReaderSymbols.SCRPrimExp:
            exitState = this._state = 22;
            exitLADepth = ++currentLADepth;
            switch (this._symbolStream.LookAhead(currentLADepth))
            {
                case SCReaderSymbols.SCTOperators_Divide:
                case SCReaderSymbols.SCTOperators_Modulus:
                case SCReaderSymbols.SCTOperators_Multiply:
                    exitState = this._state = 25;
                    exitLADepth = ++currentLADepth;
                    goto PredictMultiTargetState_25;
                default:
                    this._symbolStream.PopNone();
                    return 11;
            }
        case SCReaderSymbols.SCRMultExp:
            exitState = this._state = 24;
            exitLADepth = ++currentLADepth;
            return 12;
        case SCReaderSymbols.SCTIdentifier:
        case SCReaderSymbols.SCTNumber:
            exitState = this._state = 23;
            exitLADepth = ++currentLADepth;
            return 11;
        default:
            goto DecisionPoint;
    }
PredictMultiTargetState_25:
    return 12;
DecisionPoint:
    return 0;
}

I still have a lot of work necessary to cleanup decision results. But you'll notice the rule above is a left recursive rule. In testing (by hand writing it) it's very possible to generate a recursive descent parser that does reductions that handles left recursion. I just need to test further to see if it applies to all variations of left-recursion.

No comments: