Exemplo n.º 1
0
 public static bool TryParse(string input, out TableCellAction action)
 {
     try
     {
         action = Parse(input);
         return(true);
     }
     catch (Exception)
     {
         action = TableCellAction.Error;
         return(false);
     }
 }
Exemplo n.º 2
0
        public LR1ParsingTableCell(string input)
        {
            Action = LR1ParsingTableEntryActionExtensions.Parse(input);
            switch (Action)
            {
            case TableCellAction.Shift:
            case TableCellAction.Reduce:
                TargetState = int.Parse(input.Substring(1));
                break;

            default:
                TargetState = 0;
                break;
            }
        }
Exemplo n.º 3
0
        private string TranslateAction(TableCellAction act)
        {
            switch (act)
            {
            case TableCellAction.Shift:
                return("s");

            case TableCellAction.Reduce:
                return("r");

            case TableCellAction.Accept:
                return("acc");

            case TableCellAction.Error:
                return("err");

            default:
                return("");
            }
        }
Exemplo n.º 4
0
        public override object Parse(IEnumerable <Token <string> > tokens, string input)
        {
            var _input = tokens.Concat(new [] { baseToken });

            stateStack.Clear();
            stateStack.Push(baseToken);
            stateStack.Push((uint)0);
            Queue <Token <string> > _i = new Queue <Token <string> >(_input);
            var  a      = _i.Dequeue();
            uint s      = 0;
            var  result = table[a.TokenType][(int)s];

            while (true)
            {
                //object __input = stateStack.Peek();
                //Console.WriteLine("typeof input is {0}", __input.GetType());
                s      = (uint)stateStack.Peek();
                result = table[a.TokenType][(int)s];
                IEnumerable <uint> encoding = enc.Decode(result);
                TableCellAction    action   = (TableCellAction)encoding.ElementAt(0);
                uint targetRule             = encoding.ElementAt(1);
                uint targetProduction       = encoding.ElementAt(2);
                switch (action)
                {
                case TableCellAction.Shift:
                    stateStack.Push(a);
                    stateStack.Push(targetProduction);
                    a = _i.Dequeue();
                    break;

                case TableCellAction.Reduce:
                    var      rule      = TargetGrammar.LookupRule(Encode(targetRule, targetProduction));
                    var      firstRule = rule[0];
                    int      count     = firstRule.Count; //not the rule's length but the rule's first production's length
                    object[] elements  = new object[count];
                    for (int i = 0; i < count; i++)
                    {
                        stateStack.Pop(); //targetState
                        elements[i] = stateStack.Pop();
                    }
                    var element = (uint)stateStack.Peek();
                    //we just do the goto anyway
                    var next     = enc.Decode(table[rule.Name][(int)element]);
                    var quantity = firstRule.Rule(elements.Reverse().ToArray());
                    stateStack.Push(quantity);
                    stateStack.Push(next.ElementAt(2));
                    break;

                case TableCellAction.Accept:
                    if (!SupressMessages)
                    {
                        Console.WriteLine("State Stack Contents before accept");
                        foreach (var v in stateStack)
                        {
                            Console.WriteLine(v);
                        }
                    }
                    stateStack.Pop();
                    return(onAccept(new object[] { stateStack.Pop() }));

                case TableCellAction.Error:
                    Console.WriteLine("Error Occurred at position {0}, {1}", a.Start, a.Value);
                    Console.WriteLine("{0}", input.Substring(0, a.Start + 1));
                    return(false);
                }
            }
        }
        protected override void MakeTable()
        {
            List <LookaheadRule> Ij = new List <LookaheadRule>();
            int state = 0;

            for (int k = 0; k < cPrime.Count; k++)
            {
                var Ii = cPrime[k];
                for (int q = 0; q < Ii.Count; q++)
                {
                    var rule = Ii[q];
                    for (int b = 0; b < rule.Count; b++)
                    {
                        var prod = rule[b];
                        if (!prod.HasNext)
                        {
                            if (prod.Target.Equals(initial[0].Target))
                            {
                                MakeTable_SetAcceptState(state, TerminateSymbol);
                            }
                            else
                            {
                                Encoding        rev    = TargetGrammar.ReverseLookup(rule.Name, prod);
                                TableCellAction action = MakeTable_ExtractAction(state, rule.LookaheadSymbol);
                                switch (action)
                                {
                                case TableCellAction.Accept:
                                    if (!SupressMessages && MakeTable_Reduce_Condition(rev,
                                                                                       state, rule.LookaheadSymbol))
                                    {
                                        Console.WriteLine("WTF Mate, attempt to overwrite accept state");
                                    }
                                    break;

                                case TableCellAction.Reduce:
                                    if (!SupressMessages &&
                                        MakeTable_Reduce_Condition(rev, state, rule.LookaheadSymbol))
                                    {
                                        Console.WriteLine("Reduce/Reduce Conflict detected with rule {0}", rule.Name);
                                    }
                                    break;

                                case TableCellAction.Shift:
                                    if (!SupressMessages &&
                                        MakeTable_Reduce_Condition(rev, state, rule.LookaheadSymbol))
                                    {
                                        Console.WriteLine("Shift/Reduce Conflict detected with rule {0}", rule.Name);
                                    }
                                    break;

                                case TableCellAction.Goto:
                                    throw new Exception("This is an error! You shouldn't have gotten here!!!!");

                                case TableCellAction.Error:
                                default:
                                    MakeTable_SetReduceState(state, rule.LookaheadSymbol, rev);
                                    break;
                                }
                            }
                        }
                        else
                        {
                            var a = prod.Current;
                            if (TargetGrammar.IsTerminalSymbol(a))
                            {
                                Ij.AddRange(ComputeGoto(Ii, a));
                                int targetState = GetIndex(Ij);
                                Ij.Clear();
                                var action = MakeTable_ExtractAction(state, a);
                                //var original = _st[a];
                                switch (action)
                                {
                                case TableCellAction.Shift:
                                    if (!SupressMessages &&
                                        MakeTable_Shift_Condition(targetState, state, a))
                                    {
                                        Console.WriteLine("Shift/Shift Conflict detected with rule {0}", rule.Name);
                                    }
                                    break;

                                case TableCellAction.Reduce:
                                    if (!SupressMessages &&
                                        MakeTable_Shift_Condition(targetState, state, a))
                                    {
                                        Console.WriteLine("Reduce/Shift Conflict detected with rule {0}", rule.Name);
                                    }
                                    break;

                                case TableCellAction.Accept:
                                    if (!SupressMessages &&
                                        MakeTable_Shift_Condition(targetState, state, a))
                                    {
                                        Console.WriteLine("WTF Mate, attempt to overwrite accept state");
                                    }
                                    break;

                                case TableCellAction.Goto:
                                    throw new Exception("This is an error! You shouldn't have gotten here!!!!");

                                case TableCellAction.Error:
                                default:
                                    MakeTable_SetShiftState(state, a, targetState);
                                    //	_st[a] = new LR1ParsingTableCell(
                                    //			LR1ParsingTableEntryAction.Shift, targetState);
                                    break;
                                }
                            }
                        }
                    }
                }
                state++;
            }
#if DEBUG
            Console.WriteLine("Make Table Took {0}", DateTime.Now - start);
#endif
        }
Exemplo n.º 6
0
 public LR1ParsingTableCell(TableCellAction action,
                            int targetState)
 {
     Action      = action;
     TargetState = targetState;
 }
Exemplo n.º 7
0
 public LR1ParsingTableCell(TableCellAction action)
     : this(action, 0)
 {
 }