using System.IO; using UnityEngine; public class MyParser { private GOLD.Parser parser = new GOLD.Parser(); private enum SymbolIndex { @Eof = 0, // (EOF) @Error = 1, // (Error) @Whitespace = 2, // Whitespace @Minus = 3, // '-' @Minusminus = 4, // '--' @Exclam = 5, // '!' @Exclameq = 6, // '!=' @Quote = 7, // '"' @Percent = 8, // '%' @Percenteq = 9, // '%=' @Amp = 10, // '&' @Ampamp = 11, // '&&' @Ampeq = 12, // '&=' @Lparen = 13, // '(' @Rparen = 14, // ')' @Times = 15, // '*' @Timeseq = 16, // '*=' @Comma = 17, // ',' @Div = 18, // '/' @Diveq = 19, // '/=' @Colon = 20, // ':' @Question = 21, // '?' @Backslash = 22, // '\' @Caret = 23, // '^' @Careteq = 24, // '^=' @Pipe = 25, // '|' @Pipepipe = 26, // '||' @Pipeeq = 27, // '|=' @Tilde = 28, // '~' @Plus = 29, // '+' @Plusplus = 30, // '++' @Pluseq = 31, // '+=' @Lt = 32, // '<' @Ltlt = 33, // '<<' @Ltlteq = 34, // '<<=' @Lteq = 35, // '<=' @Eq = 36, // '=' @Minuseq = 37, // '-=' @Eqeq = 38, // '==' @Gt = 39, // '>' @Minusgt = 40, // '->' @Gteq = 41, // '>=' @Gtgt = 42, // '>>' @Gtgteq = 43, // '>>=' @A = 44, // A @Charliteral = 45, // CharLiteral @Decliteral = 46, // DecLiteral @F = 47, // F @False = 48, // false @Hexliteral = 49, // HexLiteral @Identifier = 50, // Identifier @Realliteral = 51, // RealLiteral @Stringliteral = 52, // StringLiteral @True = 53, // true @Addexp = 54, // @Andexp = 55, // @Args = 56, // @Assignop = 57, // @Compareexp = 58, // @Conditionalexp = 59, // @Equalityexp = 60, // @Expression = 61, // @Expressionlist = 62, // @Formalparams = 63, // @Formalparamslist = 64, // @Literal = 65, // @Logicalandexp = 66, // @Logicalorexp = 67, // @Logicalxorexp = 68, // @Match = 69, // @Matchleftcontext = 70, // @Matchprime = 71, // @Matchrightcontext = 72, // @Multexp = 73, // @Opname = 74, // @Opsleft = 75, // @Opsleftlist = 76, // @Opsright = 77, // @Opsrightlist = 78, // @Orexp = 79, // @Primary = 80, // @Primaryexp = 81, // @Replace = 82, // @Rule = 83, // @Shiftexp = 84, // @Unaryexp = 85 // } private enum ProductionIndex { @Assignop_Eq = 0, // ::= '=' @Assignop_Minusgt = 1, // ::= '->' @Literal_True = 2, // ::= true @Literal_False = 3, // ::= false @Literal_Decliteral = 4, // ::= DecLiteral @Literal_Hexliteral = 5, // ::= HexLiteral @Literal_Realliteral = 6, // ::= RealLiteral @Literal_Charliteral = 7, // ::= CharLiteral @Literal_Stringliteral = 8, // ::= StringLiteral @Opname_Plus = 9, // ::= '+' @Opname_Minus = 10, // ::= '-' @Opname_Amp = 11, // ::= '&' @Opname_Caret = 12, // ::= '^' @Opname_Backslash = 13, // ::= '\' @Opname_Div = 14, // ::= '/' @Opname_Pipe = 15, // ::= '|' @Opname_Exclam = 16, // ::= '!' @Opname_Quote = 17, // ::= '"' @Opname_A = 18, // ::= A @Opname_F = 19, // ::= F @Opsrightlist = 20, // ::= @Opsrightlist2 = 21, // ::= @Opsright = 22, // ::= @Opsright2 = 23, // ::= @Expressionlist = 24, // ::= @Expressionlist_Comma = 25, // ::= ',' @Expression_Eq = 26, // ::= '=' @Expression_Pluseq = 27, // ::= '+=' @Expression_Minuseq = 28, // ::= '-=' @Expression_Timeseq = 29, // ::= '*=' @Expression_Diveq = 30, // ::= '/=' @Expression_Careteq = 31, // ::= '^=' @Expression_Ampeq = 32, // ::= '&=' @Expression_Pipeeq = 33, // ::= '|=' @Expression_Percenteq = 34, // ::= '%=' @Expression_Ltlteq = 35, // ::= '<<=' @Expression_Gtgteq = 36, // ::= '>>=' @Expression = 37, // ::= @Conditionalexp_Question_Colon = 38, // ::= '?' ':' @Conditionalexp = 39, // ::= @Orexp_Pipepipe = 40, // ::= '||' @Orexp = 41, // ::= @Andexp_Ampamp = 42, // ::= '&&' @Andexp = 43, // ::= @Logicalorexp_Pipe = 44, // ::= '|' @Logicalorexp = 45, // ::= @Logicalxorexp_Caret = 46, // ::= '^' @Logicalxorexp = 47, // ::= @Logicalandexp_Amp = 48, // ::= '&' @Logicalandexp = 49, // ::= @Equalityexp_Eqeq = 50, // ::= '==' @Equalityexp_Exclameq = 51, // ::= '!=' @Equalityexp = 52, // ::= @Compareexp_Lt = 53, // ::= '<' @Compareexp_Gt = 54, // ::= '>' @Compareexp_Lteq = 55, // ::= '<=' @Compareexp_Gteq = 56, // ::= '>=' @Compareexp = 57, // ::= @Shiftexp_Ltlt = 58, // ::= '<<' @Shiftexp_Gtgt = 59, // ::= '>>' @Shiftexp = 60, // ::= @Addexp_Plus = 61, // ::= '+' @Addexp_Minus = 62, // ::= '-' @Addexp = 63, // ::= @Multexp_Times = 64, // ::= '*' @Multexp_Div = 65, // ::= '/' @Multexp_Percent = 66, // ::= '%' @Multexp = 67, // ::= @Unaryexp_Exclam = 68, // ::= '!' @Unaryexp_Tilde = 69, // ::= '~' @Unaryexp_Minus = 70, // ::= '-' @Unaryexp_Plusplus = 71, // ::= '++' @Unaryexp_Minusminus = 72, // ::= '--' @Unaryexp = 73, // ::= @Primaryexp = 74, // ::= @Primaryexp_Lparen_Rparen = 75, // ::= '(' ')' @Primary = 76, // ::= @Primary_Identifier = 77, // ::= Identifier @Args_Lparen_Rparen = 78, // ::= '(' ')' @Formalparamslist_Comma_Identifier = 79, // ::= ',' Identifier @Formalparamslist_Identifier = 80, // ::= Identifier @Formalparams_Lparen_Rparen = 81, // ::= '(' ')' @Opsleft = 82, // ::= @Opsleft2 = 83, // ::= @Opsleftlist = 84, // ::= @Opsleftlist2 = 85, // ::= @Matchleftcontext_Lt = 86, // ::= '<' @Matchprime = 87, // ::= @Matchrightcontext_Gt = 88, // ::= '>' @Match = 89, // ::= @Match2 = 90, // ::= @Match3 = 91, // ::= @Match4 = 92, // ::= @Replace = 93, // ::= @Rule = 94 // ::= } public GOLD.Reduction program; //You might derive a specific object public void Setup() { //This procedure can be called to load the parse tables. The class can //read tables using a BinaryReader. parser.LoadTables(@"C:\Users\l-pro\Desktop\lsystem.egt"); // parser.LoadTables(Path.Combine(Application.StartupPath, "grammar.egt")); } public bool Parse(TextReader reader) { //This procedure starts the GOLD Parser Engine and handles each of the //messages it returns. Each time a reduction is made, you can create new //custom object and reassign the .CurrentReduction property. Otherwise, //the system will use the Reduction object that was returned. // //The resulting tree will be a pure representation of the language //and will be ready to implement. GOLD.ParseMessage response; bool done; //Controls when we leave the loop bool accepted = false; //Was the parse successful? parser.Open(reader); parser.TrimReductions = false; //Please read about this feature before enabling done = false; while (!done) { response = parser.Parse(); switch (response) { case GOLD.ParseMessage.LexicalError: Debug.Log("LexicalError"); //Cannot recognize token done = true; break; case GOLD.ParseMessage.SyntaxError: Debug.Log("SyntaxError"); //Expecting a different token done = true; break; case GOLD.ParseMessage.Reduction: //Create a customized object to store the reduction Debug.Log("Reduction"); //parser.CurrentReduction = CreateNewObject(parser.CurrentReduction as GOLD.Reduction); break; case GOLD.ParseMessage.Accept: Debug.Log("Accept"); //Accepted! program = (GOLD.Reduction) parser.CurrentReduction; //The root node! done = true; accepted = true; break; case GOLD.ParseMessage.TokenRead: Debug.Log("TokenRead"); //You don't have to do anything here. break; case GOLD.ParseMessage.InternalError: Debug.Log("InternalError"); //INTERNAL ERROR! Something is horribly wrong. done = true; break; case GOLD.ParseMessage.NotLoadedError: Debug.Log("NotLoadedError"); //This error occurs if the CGT was not loaded. done = true; break; case GOLD.ParseMessage.GroupError: Debug.Log("GroupError"); //GROUP ERROR! Unexpected end of file done = true; break; } } //while return accepted; } private object CreateNewObject(GOLD.Reduction r) { object result = null; switch( (ProductionIndex) r.Parent.TableIndex()) { case ProductionIndex.Assignop_Eq: Debug.Log(" ::= '='"); break; case ProductionIndex.Assignop_Minusgt: Debug.Log(" ::= '->'"); break; case ProductionIndex.Literal_True: Debug.Log(" ::= true"); break; case ProductionIndex.Literal_False: Debug.Log(" ::= false"); break; case ProductionIndex.Literal_Decliteral: Debug.Log(" ::= DecLiteral: " + r.get_Data(0)); break; case ProductionIndex.Literal_Hexliteral: Debug.Log(" ::= HexLiteral"); break; case ProductionIndex.Literal_Realliteral: Debug.Log(" ::= RealLiteral"); break; case ProductionIndex.Literal_Charliteral: Debug.Log(" ::= CharLiteral"); break; case ProductionIndex.Literal_Stringliteral: Debug.Log(" ::= StringLiteral"); break; case ProductionIndex.Opname_Plus: Debug.Log(" ::= '+'"); break; case ProductionIndex.Opname_Minus: Debug.Log(" ::= '-'"); break; case ProductionIndex.Opname_Amp: Debug.Log(" ::= '&'"); break; case ProductionIndex.Opname_Caret: Debug.Log(" ::= '^'"); break; case ProductionIndex.Opname_Backslash: Debug.Log(" ::= '\'"); break; case ProductionIndex.Opname_Div: Debug.Log(" ::= '/'"); break; case ProductionIndex.Opname_Pipe: Debug.Log(" ::= '|'"); break; case ProductionIndex.Opname_Exclam: Debug.Log(" ::= '!'"); break; case ProductionIndex.Opname_Quote: Debug.Log(" ::= \""); break; case ProductionIndex.Opname_A: Debug.Log(" ::= A"); break; case ProductionIndex.Opname_F: Debug.Log(" ::= F"); break; case ProductionIndex.Opsrightlist: Debug.Log(" ::= "); break; case ProductionIndex.Opsrightlist2: Debug.Log(" ::= "); break; case ProductionIndex.Opsright: Debug.Log(" ::= :" + r.get_Data(0)); break; case ProductionIndex.Opsright2: Debug.Log(" ::= :" +r.get_Data(0)); break; case ProductionIndex.Expressionlist: Debug.Log(" ::= "); break; case ProductionIndex.Expressionlist_Comma: Debug.Log(" ::= ',' "); break; case ProductionIndex.Expression_Eq: Debug.Log(" ::= '=' "); break; case ProductionIndex.Expression_Pluseq: Debug.Log(" ::= '+=' "); break; case ProductionIndex.Expression_Minuseq: Debug.Log(" ::= '-=' "); break; case ProductionIndex.Expression_Timeseq: Debug.Log(" ::= '*=' "); break; case ProductionIndex.Expression_Diveq: Debug.Log(" ::= '/=' "); break; case ProductionIndex.Expression_Careteq: Debug.Log(" ::= '^=' "); break; case ProductionIndex.Expression_Ampeq: Debug.Log(" ::= '&=' "); break; case ProductionIndex.Expression_Pipeeq: Debug.Log(" ::= '|=' "); break; case ProductionIndex.Expression_Percenteq: Debug.Log(" ::= '%=' "); break; case ProductionIndex.Expression_Ltlteq: Debug.Log(" ::= '<<=' "); break; case ProductionIndex.Expression_Gtgteq: Debug.Log(" ::= '>>=' "); break; case ProductionIndex.Expression: Debug.Log(" ::= "); break; case ProductionIndex.Conditionalexp_Question_Colon: Debug.Log(" ::= '?' ':' "); break; case ProductionIndex.Conditionalexp: Debug.Log(" ::= "); break; case ProductionIndex.Orexp_Pipepipe: Debug.Log(" ::= '||' "); break; case ProductionIndex.Orexp: Debug.Log(" ::= "); break; case ProductionIndex.Andexp_Ampamp: Debug.Log(" ::= '&&' "); break; case ProductionIndex.Andexp: Debug.Log(" ::= "); break; case ProductionIndex.Logicalorexp_Pipe: Debug.Log(" ::= '|' "); break; case ProductionIndex.Logicalorexp: Debug.Log(" ::= "); break; case ProductionIndex.Logicalxorexp_Caret: Debug.Log(" ::= '^' "); break; case ProductionIndex.Logicalxorexp: Debug.Log(" ::= "); break; case ProductionIndex.Logicalandexp_Amp: Debug.Log(" ::= '&' "); break; case ProductionIndex.Logicalandexp: Debug.Log(" ::= "); break; case ProductionIndex.Equalityexp_Eqeq: Debug.Log(" ::= '==' "); break; case ProductionIndex.Equalityexp_Exclameq: Debug.Log(" ::= '!=' "); break; case ProductionIndex.Equalityexp: Debug.Log(" ::= "); break; case ProductionIndex.Compareexp_Lt: Debug.Log(" ::= '<' "); break; case ProductionIndex.Compareexp_Gt: Debug.Log(" ::= '>' "); break; case ProductionIndex.Compareexp_Lteq: Debug.Log(" ::= '<=' "); break; case ProductionIndex.Compareexp_Gteq: Debug.Log(" ::= '>=' "); break; case ProductionIndex.Compareexp: Debug.Log(" ::= "); break; case ProductionIndex.Shiftexp_Ltlt: Debug.Log(" ::= '<<' "); break; case ProductionIndex.Shiftexp_Gtgt: Debug.Log(" ::= '>>' "); break; case ProductionIndex.Shiftexp: Debug.Log(" ::= "); break; case ProductionIndex.Addexp_Plus: Debug.Log(" ::= '+' "); break; case ProductionIndex.Addexp_Minus: Debug.Log(" ::= '-' "); break; case ProductionIndex.Addexp: Debug.Log(" ::= "); break; case ProductionIndex.Multexp_Times: Debug.Log(" ::= '*' "); break; case ProductionIndex.Multexp_Div: Debug.Log(" ::= '/' "); break; case ProductionIndex.Multexp_Percent: Debug.Log(" ::= '%' "); break; case ProductionIndex.Multexp: Debug.Log(" ::= "); break; case ProductionIndex.Unaryexp_Exclam: Debug.Log(" ::= '!' "); break; case ProductionIndex.Unaryexp_Tilde: Debug.Log(" ::= '~' "); break; case ProductionIndex.Unaryexp_Minus: Debug.Log(" ::= '-' "); break; case ProductionIndex.Unaryexp_Plusplus: Debug.Log(" ::= '++' "); break; case ProductionIndex.Unaryexp_Minusminus: Debug.Log(" ::= '--' "); break; case ProductionIndex.Unaryexp: Debug.Log(" ::= "); break; case ProductionIndex.Primaryexp: Debug.Log(" ::= "); break; case ProductionIndex.Primaryexp_Lparen_Rparen: Debug.Log(" ::= '(' ')'"); break; case ProductionIndex.Primary: Debug.Log(" ::= "); break; case ProductionIndex.Primary_Identifier: Debug.Log(" ::= Identifier: " + r.get_Data(0)); break; case ProductionIndex.Args_Lparen_Rparen: Debug.Log(" ::= '(' ')'"); break; case ProductionIndex.Formalparamslist_Comma_Identifier: Debug.Log(" ::= ',' Identifier"); break; case ProductionIndex.Formalparamslist_Identifier: Debug.Log("< FormalParamsList> ::= Identifier: " + r.get_Data(0)); break; case ProductionIndex.Formalparams_Lparen_Rparen: Debug.Log("< FormalParams> ::= '(' ')'"); break; case ProductionIndex.Opsleft: Debug.Log(" ::= "); break; case ProductionIndex.Opsleft2: Debug.Log(" ::= "); break; case ProductionIndex.Opsleftlist: Debug.Log(" ::= "); break; case ProductionIndex.Opsleftlist2: Debug.Log(" ::= "); break; case ProductionIndex.Matchleftcontext_Lt: Debug.Log(" ::= '<'"); break; case ProductionIndex.Matchprime: Debug.Log(" ::= "); break; case ProductionIndex.Matchrightcontext_Gt: Debug.Log(" ::= '>' "); break; case ProductionIndex.Match: Debug.Log(" ::= "); break; case ProductionIndex.Match2: Debug.Log(" ::= "); break; case ProductionIndex.Match3: Debug.Log(" ::= "); break; case ProductionIndex.Match4: Debug.Log(" ::= "); break; case ProductionIndex.Replace: Debug.Log(" ::= "); break; case ProductionIndex.Rule: Debug.Log(" ::= "); break; } //switch return result; } }; //MyParser