private Nonterminal Map(NonterminalExpr rule) { if (!Nonterminals.TryGetValue(rule, out var mapped)) { mapped = new Nonterminal(rule.Name); Nonterminals.Add(rule, mapped); foreach (var chain in rule.Body) { var body = new List <Symbol>(); foreach (var symbol in chain) { switch (symbol) { case TerminalExpr terminal: body.Add(Map(terminal)); break; case NonterminalExpr nonterminal: body.Add(Map(nonterminal)); break; } } Productions.Add(new Production(mapped, body)); } } return(mapped); }
public IList <EbnfMessage> DeclareImplicitTerminals() { var result = new List <EbnfMessage>(); var terms = new HashSet <EbnfExpression>(); var done = new HashSet <EbnfExpression>(); foreach (var prod in Productions) { _VisitFetchTerminals(prod.Value.Expression, terms); if (prod.Value.Expression.IsTerminal) { done.Add(prod.Value.Expression); } } foreach (var term in terms) { if (!done.Contains(term)) { var prod = new EbnfProduction(); prod.Expression = ((ICloneable)term).Clone() as EbnfExpression; var newId = _GetImplicitTermId(); Productions.Add(newId, prod); result.Add(new EbnfMessage(EbnfErrorLevel.Message, -1, "Terminal was implicitly declared.", term.Line, term.Column, term.Position)); } } return(result); }
public void Bind <TResultNode>(ParserProduction parserProduction) where TResultNode : TActualNode { parserProduction.Name = typeof(TResultNode).Name; parserProduction.Binding = ConstructorParserProductionBinding.Bind <TResultNode>(); Productions.Add(parserProduction); }
public void Add(string tokenId, object prodObj) { Productions.Add(new KeyValuePair <string, object>(tokenId, prodObj)); if (!ProductionsByTokenId.TryGetValue(tokenId, out List <object> prodObjs)) { ProductionsByTokenId.Add(tokenId, prodObjs = new List <object>()); } prodObjs.Add(prodObj); }
public void BindReturn <TResultNode, TBaseNode>(Grammar <TToken, TBaseNode> .ParserRule <TResultNode> rule) where TResultNode : TBaseNode where TBaseNode : class { var transition = new ParserProduction(new ParserEntry[] { rule }) { Name = typeof(TResultNode).Name, Binding = ConstructorParserProductionBinding.Bind <TResultNode>(), Unwrap = true }; Productions.Add(transition); }
public void Build(string[] Input) { try { Start = Input[0][0]; foreach (string s in Input) { if (s == string.Empty) { continue; } string[] tp = s.Split(new String[] { "→" }, StringSplitOptions.None); if (tp.Length != 2) { throw new Exception("产生式应该有且仅有一个→"); } if (tp[0].Length != 1 || !isVn(tp[0][0])) { throw new Exception("产生式左边应该是一个非终结符"); } char left = tp[0][0]; char empty = System.Configuration.ConfigurationManager.AppSettings["Empty"][0]; Vn.Add(left); string[] right = tp[1].Split('|'); for (int i = 0; i < right.Count(); ++i) { string part = right[i]; foreach (char c in part) { if (isVn(c)) { Vn.Add(c); } else { Vt.Add(c); } } Production p = new Production(left, part); if (!ProductionsMap.ContainsKey(p)) { Productions.Add(p); ProductionsMap.Add(p, ProductionsMap.Count() + 1); _ProductionsMap.Add(_ProductionsMap.Count() + 1, p); } } } } catch (Exception) { throw new Exception("输入不合法\n"); } }
private void PopulateMissingProductionsRecursively(ProductionModel production, ISet <INonTerminal> visited) { if (visited.Add(production.LeftHandSide.NonTerminal)) { Productions.Add(production); foreach (var alteration in production.Alterations) { for (var s = 0; s < alteration.Symbols.Count; s++) { var symbol = alteration.Symbols[s]; if (symbol.ModelType == SymbolModelType.Production) { PopulateMissingProductionsRecursively(symbol as ProductionModel, visited); } } } } }
public void ReadFromFile(string filePath) { int number = 1; string[] lines = System.IO.File.ReadAllLines(@filePath); foreach (string line in lines) { string[] parts = line.Split("-"); Nonterminals.Add(parts[0]); string[] fileProductions = parts[1].Split("|"); var havePoductions = Productions.TryGetValue(parts[0], out var productions); if (!havePoductions) { productions = new List <string>(); } foreach (string production in fileProductions) { productions.Add(production); ProductionNumber.Add(Tuple.Create(parts[0], production), number); number++; } Productions.Add(parts[0], productions); } }
public void BindFactory <TArg1, TArg2, TResult>(SyntaxBindFunc <TSyntaxFactory, TArg1, TArg2, TResult> expression, ParserProduction parserProduction) { parserProduction.Binding = SyntaxFactoryParserProductionBinding.Bind <TNode, TSyntaxFactory>(expression); Productions.Add(parserProduction); }
public void BindFactory <TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TArg9, TArg10, TArg11, TArg12, TArg13, TArg14, TArg15, TArg16, TArg17, TArg18, TArg19, TArg20, TArg21, TArg22, TArg23, TArg24, TArg25, TArg26, TArg27, TArg28, TArg29, TArg30, TArg31, TArg32, TResult>(SyntaxBindFunc <TSyntaxFactory, TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TArg9, TArg10, TArg11, TArg12, TArg13, TArg14, TArg15, TArg16, TArg17, TArg18, TArg19, TArg20, TArg21, TArg22, TArg23, TArg24, TArg25, TArg26, TArg27, TArg28, TArg29, TArg30, TArg31, TArg32, TResult> expression, ParserProduction parserProduction) { parserProduction.Binding = SyntaxFactoryParserProductionBinding.Bind <TNode, TSyntaxFactory>(expression); Productions.Add(parserProduction); }
private MarkdownSpec(IEnumerable <Tuple <string, MarkdownDocument> > sources) { Sources = sources; // (1) Add sections into the dictionary string url = "", title = ""; // (2) Turn all the antlr code blocks into a grammar var sbantlr = new StringBuilder(); foreach (var src in sources) { var reporter = new Reporter(src.Item1); var filename = Path.GetFileName(src.Item1); var md = src.Item2; foreach (var mdp in md.Paragraphs) { reporter.CurrentParagraph = mdp; reporter.CurrentSection = null; if (mdp.IsHeading) { try { var sr = new SectionRef(mdp as MarkdownParagraph.Heading, filename); if (Sections.Any(s => s.Url == sr.Url)) { reporter.Error("MD02", $"Duplicate section title {sr.Url}"); } else { Sections.Add(sr); url = sr.Url; title = sr.Title; reporter.CurrentSection = sr; } } catch (Exception ex) { reporter.Error("MD03", ex.Message); // constructor of SectionRef might throw } } else if (mdp.IsCodeBlock) { var mdc = mdp as MarkdownParagraph.CodeBlock; string code = mdc.code, lang = mdc.language; if (lang != "antlr") { continue; } var g = Antlr.ReadString(code, ""); Productions.Add(new ProductionRef(code, g.Productions)); foreach (var p in g.Productions) { p.Link = url; p.LinkName = title; if (p.Name != null && Grammar.Productions.Any(dupe => dupe.Name == p.Name)) { reporter.Warning("MD04", $"Duplicate grammar for {p.Name}"); } Grammar.Productions.Add(p); } } } } }
public void AddRule(Production production) { Productions.Add(production); }
public void AddProduction(Production production) { Productions.Add(production); }
public virtual CnfGrammar Load(string[] specs) { var splitter = new Regex("\\s+"); var binaries = new Dictionary <string, IList <CnfProduction> >(); Func <string, string> trim = s => s.Trim(new[] { '\t', ' ' }); Func <string, string[]> split = s => splitter.Split(s); Productions.Clear(); for (var at = 0; at < specs.Length; at++) { var spec = specs[at]; string line; if (!(line = trim(spec)).StartsWith("#")) { var separatorIndex = line.IndexOf("->"); bool arrow; separatorIndex = (arrow = (separatorIndex > 0)) ? separatorIndex : line.IndexOf(":"); var sides = separatorIndex > 0 ? new[] { line.Substring(0, separatorIndex), line.Substring(separatorIndex + (arrow ? 2 : 1)) } : null; if (sides != null) { var lhs = trim(sides[0]); var rhs = trim(sides[1]); var commentMark = rhs.IndexOf('#'); rhs = trim(commentMark >= 0 ? rhs.Substring(0, commentMark) : rhs); var normalizedRhs = split(rhs); var isBinaryRhs = normalizedRhs.Length > 1; var binLeft = normalizedRhs[0]; var binRight = isBinaryRhs ? normalizedRhs[1] : null; var reduction = new CnfProduction(lhs, binLeft, binRight, at); var binaryKey = isBinaryRhs ? string.Concat(binLeft, ' ', binRight) : null; var reduceKey = ReductionKey(binaryKey ?? reduction.Rhs, lhs); if (Productions.Count < 1) { Start = reduction; } Productions.Add(reduceKey, reduction); if (binaryKey != null) { IList <CnfProduction> reductions; if (!binaries.TryGetValue(binaryKey, out reductions)) { binaries.Add(binaryKey, new List <CnfProduction>()); } binaries[binaryKey].Add(reduction); } } } } Binaries = binaries.Values.SelectMany(productions => productions).ToArray(); Lexicon = new CnfLexicon ( Productions. Values. Where(production => production.IsLexical). OrderByDescending(production => !production.IsLiteral ? production.Pattern.Length : production.Literal.Length). ToArray() ); return(this); }
public void BuildDefaultSystem() { ObjectModule om = new ObjectModule('O', 0, 1, GrowthList.LINEAR, ObjectBuilder.OBJECT_PATH + ObjectList.SIMPLE_LEAF); om.scale = Vector3.one * 0.1f; om.rotation = new Vector3(90, 0, 0); ObjectModule om_right = new ObjectModule('O', 0, 1, GrowthList.LINEAR, ObjectBuilder.OBJECT_PATH + ObjectList.SIMPLE_LEAF); om_right.scale = Vector3.one * 0.1f; om_right.rotation = new Vector3(90, 180, 0); om_right.jointed = true; JointModule jm = new JointModule('{', 0, 1, GrowthList.LINEAR, JointBuilder.JOINT_PATH + JointModule.HINGE_JOINT); jm.jointSpringSpring = 10f; jm.jointSpringDamper = 99f; jm.jointLimitMin = -10; jm.jointLimitMax = 10; ObjectModule om_right_z = new ObjectModule('O', 0, 1, GrowthList.LOGISTIC, ObjectBuilder.OBJECT_PATH + ObjectList.SIMPLE_LEAF); om_right_z.scale = Vector3.one * 0.1f; om_right_z.rotation = new Vector3(90, 90, 0); JointModule jmc = new JointModule('{', 0, 1, GrowthList.LINEAR, JointBuilder.JOINT_PATH + JointModule.CHARACTER_JOINT); jmc.mass = 1; jmc.jointSpringSpring = 10f; jmc.jointSpringDamper = 99f; jmc.jointLimitMin = -10; jmc.jointLimitMax = 10; jmc.twistLimit1 = -10; jmc.twistLimit2 = 10; jmc.twistSpringSpring = 10f; jmc.twistSpringDamper = 99f; LineModule ln = new LineModule('F', 0, 1, GrowthList.EXPONENTIAL); ln.LineWidth = 0.1f; ln.LineLength = 0.1f; ln.jointed = true; ObjectModule om_z = new ObjectModule('O', 0, 1, GrowthList.LINEAR, ObjectBuilder.OBJECT_PATH + ObjectList.SIMPLE_LEAF); om_z.scale = Vector3.one * 0.1f; om_z.rotation = new Vector3(90, -90, 0); List <SystemModule> lm = new List <SystemModule>(); lm.Add(jmc.CopyModule()); lm.Add(ln.CopyModule()); lm.Add(new SystemModule('[', 0, 1, GrowthList.NON_DEVELOPMENTAL)); lm.Add(jm.CopyModule()); lm.Add(new RotationModule('+', 0, 1, GrowthList.LINEAR, new Vector3(1, 0, 0), 45f)); lm.Add(om_right.CopyModule()); lm.Add(new SystemModule('}', 0, 1, GrowthList.NON_DEVELOPMENTAL)); lm.Add(new SystemModule(']', 0, 1, GrowthList.NON_DEVELOPMENTAL)); lm.Add(new SystemModule('1', 0, 1, GrowthList.NON_DEVELOPMENTAL)); lm.Add(new SystemModule('}', 0, 1, GrowthList.NON_DEVELOPMENTAL)); Productions.Add('1', lm); }