public void AddReduceShiftPattern(AssociativityEnum assoc, SymbolChunk <SYMBOL_ENUM> inputChunk, SYMBOL_ENUM reduceProduction,
                                   IEnumerable <SYMBOL_ENUM> stackOperators, SYMBOL_ENUM shiftProduction, params SYMBOL_ENUM[] restShiftProductions)
 {
     addPatternMatch(SymbolPrecedence.ModeEnum.ShiftReduceConflict, assoc,
                     inputChunk,
                     shiftProductions: restShiftProductions.Concat(shiftProduction).ToArray(),
                     reduceProductions: new SYMBOL_ENUM[] { reduceProduction },
                     stackOperators: stackOperators);
 }
Beispiel #2
0
        public Precedence(SymbolCoordinates coords, AssociativityEnum associativity, IEnumerable <PrecedenceWord> usedTokens)
        {
            this.Coords        = coords;
            this.Associativity = associativity;
            this.usedTokens    = usedTokens.ToArray();

            // each entry is its own micro group
            this.PriorityGroupStart = true;
            this.PriorityGroupEnd   = true;
        }
 public void AddReduceReducePattern(AssociativityEnum assoc,
                                    SymbolChunk <SYMBOL_ENUM> inputChunk,
                                    SYMBOL_ENUM production, params SYMBOL_ENUM[] restProductions)
 {
     addPatternMatch(SymbolPrecedence.ModeEnum.ReduceReduceConflict,
                     assoc, inputChunk,
                     shiftProductions: new SYMBOL_ENUM[] { },
                     reduceProductions: production.Concat(restProductions).ToArray(),
                     stackOperators: null);
 }
        /// <summary>
        /// Add from lower to highest, symbols in single call are equal
        /// </summary>
        public void AddOperator(AssociativityEnum assoc, params SYMBOL_ENUM[] opSymbols)
        {
            if (priorityGroupCounter == 0)
            {
                ++runningPriority;
            }

            foreach (SYMBOL_ENUM op_symbol in opSymbols)
            {
                addEntry(this.operators, new SymbolPrecedence <SYMBOL_ENUM>(SymbolPrecedence.ModeEnum.BasicOperatorSearch)
                {
                    Symbols       = SymbolChunk.Create(op_symbol),
                    Associativity = assoc,
                    Priority      = runningPriority
                }, symbolsRep);
            }
        }
Beispiel #5
0
        public static Precedence Create(SymbolCoordinates coords, string mode, string associativity, IEnumerable <string> inputSymbols,
                                        IEnumerable <PrecedenceWord> conflictProds)
        {
            precedenceMode prec = getPrec(mode);

            AssociativityEnum assoc = getAssoc(associativity);

            switch (prec)
            {
            case precedenceMode.Operator: return(new OperatorPrecedence(coords, assoc, inputSymbols.Select(it => new PrecedenceWord(it)), conflictProds));

            case precedenceMode.ReduceShift: return(new ReduceShiftPrecedence(coords, assoc, inputSymbols.Select(it => new PrecedenceWord(it)), conflictProds));

            case precedenceMode.ReduceReduce: return(new ReduceReducePrecedence(coords, assoc, inputSymbols.Select(it => new PrecedenceWord(it)), conflictProds));
            }

            throw ParseControlException.NewAndRun("Not recognized precedence");
        }
Beispiel #6
0
        public ReduceReducePrecedence(SymbolCoordinates coords, AssociativityEnum associativity,
                                      IEnumerable <PrecedenceWord> inputTokens,
                                      IEnumerable <PrecedenceWord> conflictTokens)
            : base(coords, associativity, inputTokens.Concat(conflictTokens))
        {
            try
            {
                InputSet      = inputTokens.ToArray();
                ReduceSymbols = conflictTokens.ToArray();

                if (ReduceSymbols.Count() < 2)
                {
                    throw new Exception();
                }
            }
            catch
            {
                throw ParseControlException.NewAndRun("Incorrect reduce/reduce precedence rule");
            }
        }
        private void addPatternMatch(SymbolPrecedence.ModeEnum mode, AssociativityEnum assoc, SymbolChunk <SYMBOL_ENUM> inputChunk,
                                     SYMBOL_ENUM[] shiftProductions,
                                     SYMBOL_ENUM[] reduceProductions,
                                     IEnumerable <SYMBOL_ENUM> stackOperators)
        {
            if (priorityGroupCounter == 0)
            {
                ++runningPriority;
            }

            var entry = new SymbolPrecedence <SYMBOL_ENUM>(mode)
            {
                Symbols           = inputChunk,
                Associativity     = assoc,
                Priority          = runningPriority,
                ShiftProductions  = new HashSet <SYMBOL_ENUM>(shiftProductions),
                ReduceProductions = new HashSet <SYMBOL_ENUM>(reduceProductions),
                StackOperators    = new HashSet <SYMBOL_ENUM>(stackOperators ?? new SYMBOL_ENUM[] { })
            };

            addEntry(this.patterns, entry, symbolsRep);
        }
Beispiel #8
0
 // in case of operator precedence -- it is just a list of tokens, not conflict resolution really
 // so we don't have do anything special
 public OperatorPrecedence(SymbolCoordinates coords, AssociativityEnum associativity,
                           IEnumerable <PrecedenceWord> inputTokens,
                           IEnumerable <PrecedenceWord> conflictTokens)
     : base(coords, associativity, inputTokens.Concat(conflictTokens))
 {
 }
Beispiel #9
0
        internal static IEnumerable <Precedence> CreateOperators(SymbolCoordinates coords, string mode, IEnumerable <Tuple <string, string> > __opPairs,
                                                                 // it can be single and then serves as both reduce production and shift production
                                                                 // or it can be many, but then remember to put reduce production as first one (exactly as in RS rule)
                                                                 IEnumerable <string> __conflictProds)
        {
            if (!__conflictProds.Any())
            {
                throw ParseControlException.NewAndRun("There has to be at least one production given for operator precedence.");
            }

            string reduce_prod = __conflictProds.First();
            IEnumerable <string> shift_prod = __conflictProds.Count() == 1 ? new [] { __conflictProds.Single() }: __conflictProds.Skip(1).ToArray();

            precedenceMode prec = getPrec(mode);

            if (prec != precedenceMode.ReduceShift)
            {
                throw ParseControlException.NewAndRun("Only shift-reduce mode is supported with this syntax.");
            }

            Tuple <string, string>[] op_pairs = __opPairs.ToArray();
            var result = new List <Precedence>();

            for (int i = 0; i < op_pairs.Length; ++i)
            {
                string input_sym = op_pairs[i].Item2;

                var group = new List <Precedence>();

                {
                    // same vs same
                    // here we go as given associativity, because priority is the same (of course)
                    AssociativityEnum assoc = getAssoc(op_pairs[i].Item1);
                    group.Add(new ReduceShiftPrecedence(coords, assoc,
                                                        new[] { new PrecedenceWord(input_sym) },
                                                        PrecedenceWord.Create(reduce_prod, input_sym).Concat(shift_prod.Select(it => new PrecedenceWord(it))))
                    {
                        PriorityGroupEnd = false, PriorityGroupStart = false
                    });
                }

                if (i > 0)
                {
                    // higher priority means that if master is in input we shift, and when it is on stack we reduce
                    group.Add(new ReduceShiftPrecedence(coords, AssociativityEnum.Reduce,
                                                        op_pairs.Take(i).Select(it => new PrecedenceWord(it.Item2)),
                                                        PrecedenceWord.Create(reduce_prod, input_sym).Concat(shift_prod.Select(it => new PrecedenceWord(it))))
                    {
                        PriorityGroupEnd = false, PriorityGroupStart = false
                    });
                    group.Add(new ReduceShiftPrecedence(coords, AssociativityEnum.Shift,
                                                        new[] { new PrecedenceWord(input_sym) },
                                                        new PrecedenceWord(reduce_prod, op_pairs.Take(i).Select(it => it.Item2)).Concat(shift_prod.Select(it => new PrecedenceWord(it))))
                    {
                        PriorityGroupEnd = false, PriorityGroupStart = false
                    });
                }

                group.First().PriorityGroupStart = true;
                group.Last().PriorityGroupEnd    = true;

                result.AddRange(group);
            }

            return(result);
        }
 public Operator(Func <double, double, double> execute, byte priority, AssociativityEnum associativity = AssociativityEnum.Left)
 {
     Priority      = priority;
     Associativity = associativity;
     Execute       = execute;
 }
 public void AddReduceShiftPattern(AssociativityEnum assoc, SymbolChunk <SYMBOL_ENUM> inputChunk, SYMBOL_ENUM reduceProduction,
                                   SYMBOL_ENUM shiftProduction, params SYMBOL_ENUM[] restShiftProductions)
 {
     AddReduceShiftPattern(assoc, inputChunk, reduceProduction, null, shiftProduction, restShiftProductions);
 }
 public void AddReduceShiftPattern(AssociativityEnum assoc, SYMBOL_ENUM inputSymbol, SYMBOL_ENUM reduceProduction,
                                   IEnumerable <SYMBOL_ENUM> stackOperators, SYMBOL_ENUM shiftProduction, params SYMBOL_ENUM[] restShiftProductions)
 {
     AddReduceShiftPattern(assoc, SymbolChunk.Create(inputSymbol), reduceProduction, stackOperators, shiftProduction, restShiftProductions);
 }
 public void AddReduceReducePattern(AssociativityEnum assoc, SYMBOL_ENUM inputSymbol, SYMBOL_ENUM production, params SYMBOL_ENUM[] restProductions)
 {
     AddReduceReducePattern(assoc, SymbolChunk.Create(inputSymbol), production, restProductions);
 }