public EnhancedLR1Parser(EnhancedGrammar g, string terminateSymbol, EnhancedParsingTable table, SemanticRule r) : base(g, terminateSymbol, r, true, false) { this.table = table; baseToken = new Token <string>(TerminateSymbol, TerminateSymbol, TerminateSymbol.Length); stateStack = new Stack <object>(); initial = new LookaheadRule(TerminateSymbol, TargetGrammar[0]); }
public EnhancedLR1ParsableLanguage( Language l, TypedShakeSelector <string> selector, EnhancedGrammar g, string terminateSymbol, EnhancedParsingTable table, SemanticRule onAccept, Func <Token <string>, bool> shouldKeep) : this(l, selector, new EnhancedLR1Parser(g, terminateSymbol, table, onAccept), shouldKeep) { }
public EnhancedLR1ParsableLanguage( string name, string version, string idType, IEnumerable <Comment> comments, IEnumerable <Symbol> symbols, IEnumerable <RegexSymbol> regexSymbols, IEnumerable <Keyword> keywords, TypedShakeSelector <string> selector, EnhancedGrammar g, string terminateSymbol, SemanticRule onAccept, bool supressMessages, Func <Token <string>, bool> shouldKeep) : this(new Language(name, version, idType, comments, symbols, regexSymbols, keywords), selector, g, terminateSymbol, onAccept, supressMessages, shouldKeep) { }
public EnhancedLR1Parser(EnhancedGrammar g, EnhancedParsingTable table, SemanticRule r) : this(g, DEFAULT_TERMINATE_SYMBOL, table, r) { }
public EnhancedLR1Parser(EnhancedGrammar g) : this(g, DEFAULT_TERMINATE_SYMBOL) { }
public EnhancedLR1Parser(EnhancedGrammar g, string terminateSymbol) : this(g, terminateSymbol, (x) => x[0]) { }
public EnhancedLR1Parser(EnhancedGrammar g, string terminateSymbol, SemanticRule r) : this(g, terminateSymbol, r, false) { }
public EnhancedLR1Parser(EnhancedGrammar g, string terminateSymbol, SemanticRule r, bool supressMessages) : base(g, terminateSymbol, r, supressMessages, true) { }
public OmnicronLanguage() { dataStack = new Stack<object>(); EnhancedGrammar grammar = new EnhancedGrammar { new Rule("S'") { new Production { "Block" }, }, new Rule("Block") { new Production { "Rest", "return" }, }, new Rule("Rest") { new Production { "BlockStatements", }, }, new Rule("BlockStatements" ) { new Production{ "BlockStatements", "BlockStatement" }, new Production{ "BlockStatement" }, }, new Rule("BlockStatement") { new Production { "Word" }, }, new Rule("Word") { new Production { "Action" }, new Production { "Type" }, }, new Rule("Type") { new Production((x) => { Push(int.Parse(x[0].ToString())); return EmptyArray; }) { "int" }, new Production((x) => { Push(long.Parse(x[0].ToString())); return EmptyArray; }) { "long" }, new Production((x) => { Push(float.Parse(x[0].ToString())); return EmptyArray; }) { "float" }, new Production((x) => { Push(double.Parse(x[0].ToString())); return EmptyArray; }) { "double" }, new Production((x) => { Push(x[0].ToString().Replace("\"",string.Empty)); return EmptyArray; }) { "string-literal" }, new Production((x) => { Push(x[0].ToString().Replace("\"",string.Empty)); return EmptyArray; }) { "id" }, new Production((x) => { Push(true); return EmptyArray; }) { "true" }, new Production((x) => { Push(false); return EmptyArray; }) { "false" }, }, new Rule("Action") { new Production(NewItem) { "new" }, new Production(ImbueItem) { "imbue" }, new Production(NewPoint) { "point" }, new Production(NewSize) { "size" }, }, }; language = new EnhancedLR1ParsableLanguage( "Dynamic Form Constructor Language", "1.0.0.0", IdSymbol.DEFAULT_IDENTIFIER, new Comment[] { new Comment("single-line-comment", "--", "\n", "\""), }, new Symbol[] { new Symbol('\n', "Newline", "<new-line>"), new Symbol(' ', "Space", "<space>" ), new Symbol('\t', "Tab", "<tab>"), new Symbol('\r', "Carriage Return", "<cr>"), }, new RegexSymbol[] { new StringLiteral(), new CharacterSymbol(), new GenericFloatingPointNumber("Single Precision Floating Point Number", "[fF]", "single"), new GenericFloatingPointNumber("Double Precision Floating Point Number", "double"), new GenericInteger("Long", "[Ll]", "long"), new GenericInteger("Int", "int"), }, new Keyword[] { new Keyword("new"), new Keyword("imbue"), new Keyword("point"), new Keyword("size"), new Keyword("extract"), new Keyword("true"), new Keyword("false"), new Keyword("return"), }, LexicalExtensions.DefaultTypedStringSelector, grammar, "$", (x) => { object result = dataStack.Pop(); if(result is DynamicForm) { DynamicForm d = (DynamicForm)result; d.ResumeLayout(false); return d; } else { throw new ArgumentException("A form should always be returned from any of these scripts!"); } }, true, IsValid); }
public static void Main(string[] args) { List<string> str = new List<string>(); EnhancedGrammar grammar = new EnhancedGrammar { (Rule)new RuleGenerator("S'") { { "Rest", null }, }, (Rule)new RuleGenerator("Rest") { { "Instructions", null }, }, (Rule)new RuleGenerator("Instructions") { { "Instructions Instruction", (x) => { str.Add(x[1].ToString()); return null; } }, { "Instruction", (x) => { str.Add(x[0].ToString()); return null; } }, }, (Rule)new RuleGenerator("Instruction") { { "Predicate Instruction'", (x) => string.Format(x[1].ToString(), x[0].ToString()) }, { "Instruction'", (x) => string.Format(x[0].ToString(), "p0") }, }, (Rule)new RuleGenerator("Instruction'") { { "id", (x) => string.Format("({0} {1})", x[0].ToString(), "{0}") }, { "id id", (x) => string.Format("({0} {1} {2})", x[0].ToString(), "{0}", x[1].ToString()) }, { "id id , id", (x) => string.Format("({0} {1} {2} {3})", x[0].ToString(), "{0}", x[1].ToString(), x[3].ToString()) }, { "id id = SourceRegisters", (x) => string.Format("({0} {1} {2} {3})", x[0].ToString(), "{0}", x[1].ToString(), x[3].ToString()) }, { "id id , id = SourceRegisters", (x) => string.Format("({0} {1} {2} {3} {4})", x[0].ToString(), "{0}", x[1].ToString(), x[3].ToString(), x[5].ToString()) }, }, (Rule)new RuleGenerator("SourceRegisters") { { "id", (x) => x[0].ToString().Replace(";;","") }, { "id , id", (x) => string.Format("{0} {1}", x[0].ToString(), x[2].ToString()).Replace(";;","") }, { "id , id , id", (x) => string.Format("{0} {1} {2}", x[0].ToString(), x[2].ToString(), x[4].ToString().Replace(";;","")) }, { "id , id , id , id", (x) => string.Format("{0} {1} {2} {3}", x[0].ToString(), x[2].ToString(), x[4].ToString(), x[6].ToString().Replace(";;","")) }, { "id , id , id , id , id", (x) => string.Format("{0} {1} {2} {3} {4}", x[0].ToString(), x[2].ToString(), x[4].ToString(), x[6].ToString(), x[8].ToString().Replace(";;","")) }, }, (Rule)new RuleGenerator("Predicate") { { "( id )", (x) => x[1] }, }, }; EnhancedLR1ParsableLanguage elpl = new EnhancedLR1ParsableLanguage( "ItaniumConverter", "1.0", "\\[?[\\.\\-\\+0-9a-zA-Z_$]([\\.0-9a-zA-Z_$])*\\]?", new Comment[] { }, new Symbol[] { new Symbol('\n', "Newline", "<new-line>"), new Symbol(' ', "Space", "<space>"), new Symbol('\t', "Tab", "<tab>"), new Symbol('\r', "Carriage Return", "<cr>"), }, new RegexSymbol[] { // new GenericInteger("Number", "num"), }, new Keyword[] { new Keyword("="), new Keyword(","), new Keyword("("), new Keyword(")"), }, LexicalExtensions.DefaultTypedStringSelector, grammar, "$", (x) => { StringBuilder sb = new StringBuilder(); foreach(var v in str) sb.AppendLine(v.ToString()); return sb.ToString(); }, true, IsValid); string msg = Console.In.ReadToEnd().Replace(";;","").Replace("\r\n","\n"); var result = elpl.Parse(msg).ToString().Replace("[","{").Replace("]","}"); Console.WriteLine(result); }
public FormConstructionLanguage() { dataStack = new Stack <object>(); EnhancedGrammar grammar = new EnhancedGrammar { new Rule("S'") { new Production { "Block" }, }, new Rule("Block") { new Production { "Rest", "return" }, }, new Rule("Rest") { new Production { "BlockStatements", }, }, new Rule("BlockStatements") { new Production { "BlockStatements", "BlockStatement" }, new Production { "BlockStatement" }, }, new Rule("BlockStatement") { new Production { "Word" }, }, new Rule("Word") { new Production { "Action" }, new Production { "Type" }, }, new Rule("Type") { new Production((x) => { Push(int.Parse(x[0].ToString())); return(EmptyArray); }) { "int" }, new Production((x) => { Push(long.Parse(x[0].ToString())); return(EmptyArray); }) { "long" }, new Production((x) => { Push(float.Parse(x[0].ToString())); return(EmptyArray); }) { "float" }, new Production((x) => { Push(double.Parse(x[0].ToString())); return(EmptyArray); }) { "double" }, new Production((x) => { Push(x[0].ToString().Replace("\"", string.Empty)); return(EmptyArray); }) { "string-literal" }, new Production((x) => { Push(x[0].ToString().Replace("\"", string.Empty)); return(EmptyArray); }) { "id" }, new Production((x) => { Push(true); return(EmptyArray); }) { "true" }, new Production((x) => { Push(false); return(EmptyArray); }) { "false" }, }, new Rule("Action") { new Production(NewItem) { "new" }, new Production(ImbueItem) { "imbue" }, new Production(NewPoint) { "point" }, new Production(NewSize) { "size" }, }, }; language = new EnhancedLR1ParsableLanguage( "Dynamic Form Constructor Language", "1.0.0.0", IdSymbol.DEFAULT_IDENTIFIER, new Comment[] { new Comment("single-line-comment", "--", "\n", "\""), }, new Symbol[] { new Symbol('\n', "Newline", "<new-line>"), new Symbol(' ', "Space", "<space>"), new Symbol('\t', "Tab", "<tab>"), new Symbol('\r', "Carriage Return", "<cr>"), }, new RegexSymbol[] { new StringLiteral(), new CharacterSymbol(), new GenericFloatingPointNumber("Single Precision Floating Point Number", "[fF]", "single"), new GenericFloatingPointNumber("Double Precision Floating Point Number", "double"), new GenericInteger("Long", "[Ll]", "long"), new GenericInteger("Int", "int"), }, new Keyword[] { new Keyword("new"), new Keyword("imbue"), new Keyword("point"), new Keyword("size"), new Keyword("extract"), new Keyword("true"), new Keyword("false"), new Keyword("return"), }, LexicalExtensions.DefaultTypedStringSelector, grammar, "$", (x) => { object result = dataStack.Pop(); if (result is DynamicForm) { DynamicForm d = (DynamicForm)result; d.ResumeLayout(false); return(d); } else { throw new ArgumentException("A form should always be returned from any of these scripts!"); } }, true, IsValid); }