Example #1
0
        public GrammarTables Build()
        {
            Populate();
            AssignTableIndexes();
            LinkGroupSymbolsToGroup();
            AssignPriorities();

            int count = m_symbols.Count(s => s.Symbol.Type == ESymbolType.Content);

            Console.WriteLine($"The grammar contains a total of {count} formal terminals.");
            DoSemanticAnalysis();

            // create result table.
            var tables = new GrammarTables();

            // copy groups.
            foreach (var group in m_groups)
            {
                tables.Groups.Add(group);
            }

            // copy symbols.
            foreach (var sym in m_symbols)
            {
                tables.Symbols.Add(sym.Symbol);
            }

            // build the DFA table.
            BuildLALR.Build(tables, this);
            BuildDFA.Build(tables, this);

            // return result.
            return(tables);
        }
Example #2
0
        public override Automata CreateAutomata(BuildDFA buildDfa)
        {
            var head = buildDfa.AddState();
            var tail = buildDfa.AddState();

            buildDfa.AddEdge(head, tail, m_characterSet);

            HandleKleen(buildDfa, head, tail);

            return(new Automata
            {
                Head = head,
                Tail = tail
            });
        }
Example #3
0
        protected void HandleKleen(BuildDFA buildDfa, int head, int tail)
        {
            switch (m_kleene)
            {
            case EKleene.ZeroOrMore:
                buildDfa.AddLambdaEdge(head, tail);
                buildDfa.AddLambdaEdge(tail, head);
                break;

            case EKleene.OneOrMore:
                buildDfa.AddLambdaEdge(tail, head);
                break;

            case EKleene.ZeroOrOne:
                buildDfa.AddLambdaEdge(head, tail);
                break;
            }
        }
Example #4
0
        internal Automata CreateAutomata(BuildDFA buildDfa)
        {
            Debug.Assert(m_items.Count > 0);

            var automata = m_items[0].CreateAutomata(buildDfa);
            var head     = automata.Head;
            var tail     = automata.Tail;

            for (var i = 1; i < m_items.Count; ++i)
            {
                automata = m_items[i].CreateAutomata(buildDfa);
                buildDfa.AddLambdaEdge(tail, automata.Head);
                tail = automata.Tail;
            }

            return(new Automata
            {
                Head = head,
                Tail = tail
            });
        }
Example #5
0
 public abstract Automata CreateAutomata(BuildDFA buildDfa);