How to get unsorted TreeNode

Apr 18 at 9:49 AM
Edited Apr 20 at 6:20 AM
Hi, I have created the next easy grammar:
RegexBasedTerminal anyText = new RegexBasedTerminal("anyText", "[^;]*;");

NonTerminal commands = new NonTerminal("commands"),
command = new NonTerminal("command"),
insertStatement = new NonTerminal("insertStatement"),
updateStatement = new NonTerminal("updateStatement"),
deleteStatement = new NonTerminal("deleteStatement"),
createStatement = new NonTerminal("createStatement"),
dropStatement = new NonTerminal("dropStatement"),
selectStatement = new NonTerminal("selectStatement"),
commitStatement = new NonTerminal("commitStatement");

commands.Rule = command | command + commands;
command.Rule = insertStatement | updateStatement | deleteStatement 
    | createStatement | dropStatement | selectStatement | commitStatement;
insertStatement.Rule = ToTerm("INSERT", typeof(BinaryOperationNode)) + anyText;
updateStatement.Rule = ToTerm("UPDATE") + anyText;
deleteStatement.Rule = ToTerm("DELETE") + anyText;
createStatement.Rule = ToTerm("CREATE") + anyText;
dropStatement.Rule = ToTerm("DROP") + anyText;
selectStatement.Rule = ToTerm("SELECT") + anyText;
commitStatement.Rule = ToTerm("COMMIT") + anyText;

this.Root = commands;
After, that, I am getting the result with:
DsnGram dsnGram = new DsnGram();
LanguageData languageData = new LanguageData(dsnGram);
Parser parserCommands = new Parser(languageData);
ParseTree parseTree = parserCommands.Parse(File.ReadAllText(sysinLseqFn));
ParseTreeNode dsnCommands = parseTree.Root;
Where sysinLseqFn contains:

DROP TABLE TESTJ;
CREATE TABLE TESTJ (F1 CHAR(3) not null);
INSERT INTO TESTJ VALUES('AAA');
SELECT * FROM TESTJ;

The problem is that in the TreeNode, the first statement is "CREATE .." instead of "DROP ..." because the TreeNode is sorted:

[DEBUG] commands
[DEBUG] command
[DEBUG] createStatement
[DEBUG] CREATE (Keyword)
[DEBUG] TABLE TESTJ (F1 CHAR(3) not null); (anyText)
[DEBUG] commands
[DEBUG] command
[DEBUG] dropStatement
[DEBUG] DROP (Keyword)
[DEBUG] TABLE TESTJ; (anyText)
[DEBUG] commands
[DEBUG] command
[DEBUG] dropStatement
[DEBUG] DROP (Keyword)
[DEBUG] TABLE TESTJ; (anyText)
[DEBUG] commands
[DEBUG] command
[DEBUG] insertStatement
[DEBUG] INSERT (Keyword)
[DEBUG] INTO TESTJ VALUES('AAA'); (anyText)
[DEBUG] commands
[DEBUG] command
[DEBUG] selectStatement
[DEBUG] SELECT (Keyword)
[DEBUG] * FROM TESTJ; (anyText)

How could I get the TreeNode unsorted?

Thanks in advance!
Coordinator
Apr 19 at 10:33 PM
Somehow it shows empty question for me... but I did receive it email notification, so reproducing it:

Hi, I have created the next easy grammar:
    RegexBasedTerminal anyText = new RegexBasedTerminal("anyText", "[^;]*;");

    NonTerminal commands = new NonTerminal("commands"),
    command = new NonTerminal("command"),
    insertStatement = new NonTerminal("insertStatement"),
    updateStatement = new NonTerminal("updateStatement"),
    deleteStatement = new NonTerminal("deleteStatement"),
    createStatement = new NonTerminal("createStatement"),
    dropStatement = new NonTerminal("dropStatement"),
    selectStatement = new NonTerminal("selectStatement"),
    commitStatement = new NonTerminal("commitStatement");

    commands.Rule = command | command + commands;
    command.Rule = insertStatement | updateStatement | deleteStatement 
        | createStatement | dropStatement | selectStatement | commitStatement;
    insertStatement.Rule = ToTerm("INSERT", typeof(BinaryOperationNode)) + anyText;
    updateStatement.Rule = ToTerm("UPDATE") + anyText;
    deleteStatement.Rule = ToTerm("DELETE") + anyText;
    createStatement.Rule = ToTerm("CREATE") + anyText;
    dropStatement.Rule = ToTerm("DROP") + anyText;
    selectStatement.Rule = ToTerm("SELECT") + anyText;
    commitStatement.Rule = ToTerm("COMMIT") + anyText;

    this.Root = commands;
After, that, I am getting the result with:
    DsnGram dsnGram = new DsnGram();
    LanguageData languageData = new LanguageData(dsnGram);
    Parser parserCommands = new Parser(languageData);
    ParseTree parseTree = parserCommands.Parse(File.ReadAllText(sysinLseqFn));
    ParseTreeNode dsnCommands = parseTree.Root;
Where sysinLseqFn contains:

DROP TABLE TESTJ;
CREATE TABLE TESTJ (F1 CHAR(3) not null);
INSERT INTO TESTJ VALUES('AAA');
SELECT * FROM TESTJ;

The problem is that in the TreeNode, the first statement is "CREATE .." instead of "DROP ..." because the TreeNode is sorted:

[DEBUG] commands
[DEBUG] command
[DEBUG] createStatement
[DEBUG] CREATE (Keyword)
[DEBUG] TABLE TESTJ (F1 CHAR(3) not null); (anyText)
[DEBUG] commands
[DEBUG] command
[DEBUG] dropStatement
[DEBUG] DROP (Keyword)
[DEBUG] TABLE TESTJ; (anyText)
[DEBUG] commands
[DEBUG] command
[DEBUG] dropStatement
[DEBUG] DROP (Keyword)
[DEBUG] TABLE TESTJ; (anyText)
[DEBUG] commands
[DEBUG] command
[DEBUG] insertStatement
[DEBUG] INSERT (Keyword)
[DEBUG] INTO TESTJ VALUES('AAA'); (anyText)
[DEBUG] commands
[DEBUG] command
[DEBUG] selectStatement
[DEBUG] SELECT (Keyword)
[DEBUG] * FROM TESTJ; (anyText)

How could I get the TreeNode unsorted?

Thanks in advance!
Coordinator
Apr 19 at 10:37 PM
I think you should try using MakePlusList for commands nonterminal - in this case it would produce flat list, and it should be in proper order
Apr 20 at 6:21 AM
Thanks Rivantsov! It solved the problem.