public void RegisterPunctuation(params string[] symbols) { foreach (string symbol in symbols) { SymbolTerminal term = SymbolTerminal.GetSymbol(symbol); term.SetOption(TermOptions.IsPunctuation); } }
}//method public void RegisterBracePair(string openBrace, string closeBrace) { SymbolTerminal openS = SymbolTerminal.GetSymbol(openBrace); SymbolTerminal closeS = SymbolTerminal.GetSymbol(closeBrace); openS.SetOption(TermOptions.IsOpenBrace); openS.IsPairFor = closeS; closeS.SetOption(TermOptions.IsCloseBrace); closeS.IsPairFor = openS; }
public void RegisterOperators(int precedence, Associativity associativity, params string[] opSymbols) { foreach (string op in opSymbols) { SymbolTerminal opSymbol = SymbolTerminal.GetSymbol(op); opSymbol.SetOption(TermOptions.IsOperator, true); opSymbol.Precedence = precedence; opSymbol.Associativity = associativity; } }//method
} //method #endregion #region Check for shift-reduce conflicts private void CheckActionConflicts() { StringDictionary errorTable = new StringDictionary(); foreach (ParserState state in Data.States) { foreach (ActionRecord action in state.Actions.Values) { //1. Pure shift if (action.NewState != null && action.ReduceProductions.Count == 0) { continue; //ActionType is shift by default } //2. Pure reduce if (action.NewState == null && action.ReduceProductions.Count == 1) { action.ActionType = ParserActionType.Reduce; continue; } //3. Shift-reduce conflict if (action.NewState != null && action.ReduceProductions.Count > 0) { //it might be an operation, with resolution by precedence/associativity SymbolTerminal opTerm = SymbolTerminal.GetSymbol(action.Key); if (opTerm != null && opTerm.IsSet(TermOptions.IsOperator)) { action.ActionType = ParserActionType.Operator; } else { AddErrorForInput(errorTable, action.Key, "Shift-reduce conflict in state {0}, reduce production: {1}", state, action.ReduceProductions[0]); //NOTE: don't do "continue" here, we need to proceed to reduce-reduce conflict check } //if...else } //if action.... //4. Reduce-reduce conflicts if (action.ReduceProductions.Count > 1) { AddErrorForInput(errorTable, action.Key, "Reduce-reduce conflict in state {0} in productions: {1} ; {2}", state, action.ReduceProductions[0], action.ReduceProductions[1]); } } //foreach action } //foreach state //copy errors to Errors collection; In errorTable keys are error messages, values are inputs for this message foreach (string msg in errorTable.Keys) { Data.Errors.Add(msg + " on inputs: " + errorTable[msg]); } }//methods
public static SymbolTerminal GetSymbol(string symbol, string name) { SymbolTerminal term; if (_symbols.TryGetValue(symbol, out term)) { //rename symbol if name is provided explicitly (different from symbol itself) if (name != symbol && term.Name != name) { term.Name = name; } return(term); } string.Intern(symbol); term = new SymbolTerminal(symbol, name); term.SetOption(TermOptions.IsGrammarSymbol, true); _symbols[symbol] = term; return(term); }
public static BnfExpression operator |(string symbol1, BnfTerm term2) { return(Op_Pipe(SymbolTerminal.GetSymbol(symbol1), term2)); }
public static BnfExpression operator |(BnfTerm term1, string symbol2) { return(Op_Pipe(term1, SymbolTerminal.GetSymbol(symbol2))); }
protected static SymbolTerminal Symbol(string symbol, string name) { return(SymbolTerminal.GetSymbol(symbol, name)); }
protected static SymbolTerminal Symbol(string symbol) { return(SymbolTerminal.GetSymbol(symbol)); }