Beispiel #1
0
        internal ActiveLabel(GrammarSequence sequence, int index = 0)
        {
            _sequence = sequence;
            _index    = index;

            var sequenceTerms = sequence.Terms;

            CurrentTerm  = sequenceTerms.Skip(index).First();
            WillComplete = sequenceTerms.Count() == index + 1;

            var currentTerminal = CurrentTerm as Terminal;

            if (currentTerminal != null)
            {
                TerminalLabel = new TerminalLabel(currentTerminal, sequence);
            }

            if (WillComplete)
            {
                CompleteLabel = new CompleteLabel(_sequence);
            }
            else
            {
                NextInChain = new ActiveLabel(sequence, index + 1);
            }
        }
Beispiel #2
0
        internal ActiveEdge(ActiveEdge extendedEdge, CompleteEdge extendingEdge)
            : base(extendedEdge.StartContext, extendingEdge.EndContext)
        {
            ExtendingEdge = extendingEdge;

            ExtendedEdge = extendedEdge;
            Label        = ExtendedEdge.Label.NextInChain;
        }
Beispiel #3
0
        private IEnumerable <ActiveLabel> getTrasintions(ActiveLabel label)
        {
            var transitions = new HashSet <ActiveLabel>();

            fillTransitions(label, transitions);

            return(transitions);
        }
Beispiel #4
0
        /// <summary>
        /// Creates parser, from given grammar
        /// (Accepts all CFG forms)
        /// </summary>
        /// <param name="grammar">Grammar specifing parsing rules</param>
        public Parser(GrammarBase grammar)
        {
            _grammar = grammar;
            grammar.Build();
            var nonTerminals = grammar.CollectNonTerminals(grammar.Root);

            //prepare edges
            foreach (var nonTerm in nonTerminals)
            {
                foreach (var sequence in nonTerm.Rule.Sequences)
                {
                    var activeLabel = new ActiveLabel(sequence);
                    _activeChains.Add(sequence, activeLabel);
                }
            }

            buildEdgeTransitions();

            //set root transitions
            _rootTransitions = getRootTransitions().ToArray();
        }
Beispiel #5
0
        private void fillTransitions(ActiveLabel label, HashSet <ActiveLabel> transitions)
        {
            var transitionNonTerm = label.CurrentTerm as NonTerminal;

            if (transitionNonTerm == null)
            {
                //transitions are processed only on non terminals
                return;
            }

            foreach (var sequence in transitionNonTerm.Rule.Sequences)
            {
                var transition = _activeChains[sequence];

                if (transitions.Add(transition))
                {
                    //recursive add transitions of added edge
                    fillTransitions(transition, transitions);
                }
            }
        }
Beispiel #6
0
 /// <summary>
 /// Creates self edge
 /// </summary>
 /// <param name="label"></param>
 /// <param name="extendedEdge"></param>
 internal ActiveEdge(ActiveLabel label, ActiveEdge extendedEdge)
     : base(extendedEdge.EndContext, extendedEdge.EndContext)
 {
     Label = label;
     //       ExtendedEdge = extendedEdge;
 }
Beispiel #7
0
 internal ActiveEdge(ActiveLabel label, SourceContext startContext, SourceContext endContext)
     : base(startContext, endContext)
 {
     Label = label;
 }