Example #1
0
 public void ReadWithValidData( ISet<LR0Item> set, Symbol symbol, RestrictedStartSymbolGrammar grammar, ISet<LR0Item> expected )
 {
     var actual = LR0CanonicalSets.Read(set, symbol, grammar);
     Assert.NotNull(actual);
     Assert.True(LR0CanonicalSets.AreEqual(actual, expected));
 }
 public ForeignSymbolInRuleException( Rule rule, Symbol symbol )
 {
     this.Rule = rule;
     this.Symbol = symbol;
 }
Example #3
0
        private void AddRhsItem( Symbol symbol )
        {
            int index = this.rhs.Count;
            this.rhs.Add(symbol);

            // ComboBox
            var combobox = new ComboBox();
            this.LoadItemsIntoComboBox(combobox, this.grammaticals);
            this.LoadItemsIntoComboBox(combobox, this.terminals);
            combobox.DropDownStyle = ComboBoxStyle.DropDownList;
            combobox.Margin = new Padding(0);
            combobox.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
            combobox.DrawMode = DrawMode.OwnerDrawFixed;
            combobox.DrawItem += this.Symbol_ComboBox_DrawItem;
            if( symbol != null )
            {
                if( symbol is GrammaticalSymbol )
                {
                    int ind = this.grammaticals.IndexOf((GrammaticalSymbol)symbol);
                    if( ind == -1 )
                        this.rhs[index] = null;
                    else
                        combobox.SelectedIndex = ind;
                }
                else
                {
                    int ind = this.terminals.IndexOf((TerminalSymbol)symbol);
                    if( ind == -1 )
                        this.rhs[index] = null;
                    else
                        combobox.SelectedIndex = this.grammaticals.Count + ind;
                }
            }
            combobox.SelectedIndexChanged += ( s_, e_ ) =>
                {
                    int index_ = this.Rhs_TablePanel.Controls.IndexOf(combobox);

                    if( combobox.SelectedIndex == -1 )
                        this.rhs[index_] = null;
                    else if( combobox.SelectedIndex < this.grammaticals.Count )
                        this.rhs[index_] = this.grammaticals[combobox.SelectedIndex];
                    else
                        this.rhs[index_] = this.terminals[combobox.SelectedIndex - this.grammaticals.Count];

                    this.UpdateRuleString();
                };

            // Add a new row only if there is no more left. (Must only happen with the first item!)
            if( this.Rhs_TablePanel.RowCount == this.rhs.Count )
            {
                this.Rhs_TablePanel.RowCount += 1;
                this.Rhs_TablePanel.RowStyles.Add(new RowStyle(SizeType.AutoSize));
            }
            this.Rhs_TablePanel.Controls.Add(combobox);

            this.UpdateRuleString();
        }
        private SyntacticAnalysis.Grammar BuildGrammar()
        {
            // Generate terminals
            var terminals = new Dictionary<string, TerminalSymbol>(this.terminals.Count);
            foreach( string tr_name in this.terminals.Keys )
                terminals.Add(tr_name, new TerminalSymbol(tr_name));

            // Generate grammaticals
            var grammaticals = new Dictionary<string, GrammaticalSymbol>(this.grammaticals.Count);
            foreach( string gr_name in this.grammaticals.Keys )
                grammaticals.Add(gr_name, new GrammaticalSymbol(gr_name));

            // Generate rules
            var rules = new List<SyntacticAnalysis.Rule>(this.rules.Count);
            foreach( var rule in this.rules )
            {
                var rhs = new Symbol[rule.Rhs.Count];
                for( int i = 0; i < rule.Rhs.Count; ++i )
                {
                    GrammaticalSymbol gr;
                    if( grammaticals.TryGetValue(rule.Rhs[i], out gr) )
                    {
                        rhs[i] = gr;
                    }
                    else
                    {
                        rhs[i] = terminals[rule.Rhs[i]];
                    }
                }

                rules.Add(new SyntacticAnalysis.Rule(
                    grammaticals[rule.Lhs],
                    rhs));
            }

            return new SyntacticAnalysis.Grammar(terminals.Values, grammaticals.Values, rules);
        }
Example #5
0
 /// <summary>
 /// Returns an index unique among symbols from the same grammar.
 /// (terminals first, grammaticals second)
 /// </summary>
 /// <param name="symbol"></param>
 /// <returns>The index of the given symbol, <c>-1</c> if symbol not part of this grammar.</returns>
 public int GlobalIndexOf( Symbol symbol )
 {
     if( symbol is TerminalSymbol )
         return this.Terminals.IndexOf((TerminalSymbol)symbol);
     else
         return this.Terminals.Count + this.Grammaticals.IndexOf((GrammaticalSymbol)symbol);
 }
Example #6
0
 /// <summary>
 /// Tells if this symbol is considered equal to another one.
 /// </summary>
 /// <param name="other">The other symbol to compare to.</param>
 /// <returns><c>true</c> if they are equal, <c>false</c> otherwise.</returns>
 public bool Equals( Symbol other )
 {
     return other != null && this.GetType() == other.GetType() && this.Name == other.Name;
 }