//Pipe/Alternative internal static BnfExpression Op_Pipe(BnfTerm term1, BnfTerm term2) { //Check term1 and see if we can use it as result, simply adding term2 as operand BnfExpression expr1 = term1 as BnfExpression; if (expr1 == null) //either not expression at all, or Pipe-type expression (count > 1) { expr1 = new BnfExpression(term1); } //Check term2; if it is an expression and is simple sequence (Data.Count == 1) then add this sequence directly to expr1 BnfExpression expr2 = term2 as BnfExpression; //1. term2 is a simple expression if (expr2 != null && expr2.Data.Count == 1) // if it is simple sequence (plus operation), add it directly { expr1.Data.Add(expr2.Data[0]); return(expr1); } //2. term2 is not a simple expression expr1.Data.Add(new BnfTermList()); //add a list for a new OR element (new "plus" sequence) expr1.Data[expr1.Data.Count - 1].Add(term2); // and put term2 there if it is not Empty pseudo-element return(expr1); }
private void CollectAllElementsRecursive(BnfTerm element) { //Terminal Terminal term = element as Terminal; // Do not add pseudo terminals defined as static singletons in Grammar class (Empty, Eof, etc) // We will never see these terminals in the input stream. // Filter them by type - their type is exactly "Terminal", not derived class. if (term != null && !Data.Terminals.Contains(term) && term.GetType() != typeof(Terminal)) { Data.Terminals.Add(term); return; } //NonTerminal NonTerminal nt = element as NonTerminal; if (nt == null || Data.NonTerminals.Contains(nt)) { return; } if (nt.Name == null) { if (nt.Rule != null && !string.IsNullOrEmpty(nt.Rule.Name)) { nt.Name = nt.Rule.Name; } else { nt.Name = "NT" + (_unnamedCount++); } } Data.NonTerminals.Add(nt); if (nt.Rule == null) { AddError("Non-terminal {0} has uninitialized Rule property.", nt.Name); Data.AnalysisCanceled = true; return; } //check all child elements foreach (BnfTermList elemList in nt.Rule.Data) { for (int i = 0; i < elemList.Count; i++) { BnfTerm child = elemList[i]; if (child == null) { AddError("Rule for NonTerminal {0} contains null as an operand in position {1} in one of productions.", nt, i); continue; //for i loop } //Check for nested expression - convert to non-terminal BnfExpression expr = child as BnfExpression; if (expr != null) { child = new NonTerminal(null, expr); elemList[i] = child; } CollectAllElementsRecursive(child); } } }//method
protected static BnfTerm WithQ(BnfExpression expression) { return(ToElement(expression).Q()); }
public NonTerminal(string name, BnfExpression expression) : this(name) { _rule = expression; }
protected static BnfTerm ToElement(BnfExpression expression) { string name = expression.ToString(); return(new NonTerminal(name, expression)); }