Beispiel #1
0
        public void Union()
        {
            //begin
            //x = (find 'a(b|c)' in abc.txt) union (find 'bc in abc.txt)
            //end

            SymbolTable table = new SymbolTable();

            MiniRE miniRe = new MiniRE();

            StatementList sl = new StatementList();
            miniRe.StatementList = sl;

            Statement statement = new Statement();
            statement.Id = new StringNode("x");
            sl.Statement = statement;

            AssignmentStatement as1 = new AssignmentStatement();
            statement.AssignmentStatement = as1;

            Exp exp = new Exp();
            as1.Exp = exp;

            #region First find

            Term t1 = new Term();
            exp.Term = t1;

            Regex r1 = new Regex();
            r1.Pattern = "([a-zA-Z])*ment([a-zA-Z])*";
            t1.Regex = r1;

            Filename file1 = new Filename();
            file1.Path = "../../../TestFiles/file1.txt";
            t1.Filename = file1;

            #endregion

            ExpTail expTail = new ExpTail();
            exp.Tail = expTail;

            BinOp bop = new BinOp();
            expTail.Binop = bop;

            StringNode operation = new StringNode("union");
            bop.Operation = operation;

            #region Second find

            Term t2 = new Term();
            expTail.Term = t2;

            ExpTail expTail2 = new ExpTail();
            expTail.Tail = expTail2;

            Regex r2 = new Regex();
            r2.Pattern = "([a-zA-Z])*(a|A)([a-zA-Z])*";
            t2.Regex = r2;

            Filename file2 = new Filename();
            file2.Path = "../../../TestFiles/file2.txt";
            t2.Filename = file2;

            #endregion

            miniRe.Execute(table);
            StringMatchList matches = (StringMatchList) table["x"];

            Assert.AreEqual(8, matches.Length);

            String expected = "{\"mentor\"<'../../../TestFiles/file1.txt', 1>" +
                "\"filament\"<'../../../TestFiles/file1.txt', 1>" +
                "\"argument\"<'../../../TestFiles/file1.txt', 1>" +
                "\"argumentative\"<'../../../TestFiles/file1.txt', 1><'../../../TestFiles/file2.txt', 1>" +
                "\"Tournament\"<'../../../TestFiles/file1.txt', 1>" +
                "\"Argument\"<'../../../TestFiles/file2.txt', 1>" +
                "\"predicament\"<'../../../TestFiles/file2.txt', 1>" +
                "\"apple\"<'../../../TestFiles/file2.txt', 1>" +
            "}";

            Assert.AreEqual(expected, matches.ToString());
        }
Beispiel #2
0
        public Node doParse()
        {
            //% is epsilon.

            Stack<string> tokens = new Stack<string>();
            tokens.Push("$");
            tokens.Push("<MiniRE-program>");

            Stack<Node> nodes = new Stack<Node>();
            Node miniRE = new Node();

            while (tokens.Count != 0)
            {
                string topElem = tokens.Pop();
                string nextToken = scanner.peekToken();

                if (nextToken == "$")
                {
                    if (topElem == "$")
                        break;
                    else
                        throw new RDParser.SyntaxError("All tokens not popped off stack");
                }

                if (topElem.Contains('<') && topElem.Contains('>'))
                {
                    //This is a non-term, we need to look up and replace.
                    List<string> newrule;
                    try
                    {
                        if (isID(nextToken))
                            newrule = parsetable.getRuleMatchingToken(topElem, "ID");
                        else if(isREGEX(nextToken))
                            newrule = parsetable.getRuleMatchingToken(topElem, "REGEX");
                        else if(isASCIISTR(nextToken))
                            newrule = parsetable.getRuleMatchingToken(topElem, "ASCII-STR");
                        else
                            newrule = parsetable.getRuleMatchingToken(topElem, nextToken);
                    }
                    catch (KeyNotFoundException e)
                    {
                        throw new RDParser.SyntaxError("Invalid token in position.");
                    }
                    //Take the existing rule, create its node, add to top elem on stack, pop top elem if full.

                    Node newElem = GetNodeFromRuleName(topElem);
                    if (!(newrule.Count == 0 && newrule[0] == "%"))
                    {
                        if (nodes.Count != 0 && nodes.Peek().IsFull)
                            nodes.Pop();
                        if (nodes.Count != 0)
                            nodes.Peek().Nodes.Add(newElem);

                        if (nodes.Count != 0 && nodes.Peek().IsFull && nodes.Peek() is MiniRE)
                        {
                            miniRE = nodes.Pop();
                        }
                        else if (nodes.Count != 0 && nodes.Peek().IsFull)
                            nodes.Pop();

                        if (!newElem.IsFull && newrule[0] != "%")
                        {
                            nodes.Push(newElem);
                        }
                        newrule = new List<string>(newrule);
                        newrule.Reverse();

                        if (newrule.Count > 0 && newrule[0] != "%")
                        {
                            foreach (string s in newrule)
                            {
                                tokens.Push(s);
                            }
                        }
                    }
                    else
                    {
                        StringNode sn = new StringNode();
                        sn.Token = "%";
                        newElem.Nodes.Add(sn);
                        nodes.Peek().Nodes.Add(newElem);

                        if (nodes.Peek().IsFull && nodes.Peek() is MiniRE)
                            miniRE = nodes.Pop();
                    }

                }
                else
                {
                    nextToken = scanner.getToken();
                    //Is a terminal; must match top of stack, possibly do stuff with AST depending on token.
                    if (topElem == nextToken)
                    {
                        switch (nextToken)
                        {
                            case "maxfreqstring":
                                AssignmentStatement xyz = (AssignmentStatement)nodes.Peek();
                                xyz.Type = AssignmentStatementType.MaxFreqString;
                                break;
                            case "#":
                                AssignmentStatement xyza = (AssignmentStatement)nodes.Peek();
                                xyza.Type = AssignmentStatementType.Length;
                                break;
                            case "replace":
                                OtherStatement abc = (OtherStatement)nodes.Peek();
                                abc.Mode = OtherStatementMode.Replace;
                                break;
                            case "print":
                                OtherStatement bcd = (OtherStatement)nodes.Peek();
                                bcd.Mode = OtherStatementMode.Print;
                                break;
                            case "recursivereplace":
                                OtherStatement cde = (OtherStatement)nodes.Peek();
                                cde.Mode = OtherStatementMode.RecursiveReplace;
                                break;
                            case "diff":
                                BinOp op1 = (BinOp)nodes.Peek();
                                StringNode n1 = new StringNode();
                                n1.Token = "diff";
                                op1.Nodes.Add(n1);
                                break;
                            case "union":
                                BinOp op2 = (BinOp)nodes.Peek();
                                StringNode n2 = new StringNode();
                                n2.Token = "union";
                                op2.Nodes.Add(n2);
                                break;
                            case "inters":
                                BinOp op3 = (BinOp)nodes.Peek();
                                StringNode n3 = new StringNode();
                                n3.Token = "inters";
                                op3.Nodes.Add(n3);
                                break;
                            default:
                                break;
                        }
                    }
                    else if (topElem == "REGEX" && nextToken[0] == '\'' && nextToken[nextToken.Length - 1] == '\'')
                    {
                        Regex re = new Regex();
                        re.Pattern = nextToken.Substring(1, nextToken.Length - 2);
                        nodes.Peek().Nodes.Add(re);
                        if (nodes.Peek().IsFull)
                            nodes.Pop();
                    }
                    else if (topElem == "ASCII-STR" && nextToken[0] == '"' && nextToken[nextToken.Length - 1] == '"')
                    {
                        Node top = nodes.Peek();
                        if (top is OtherStatement)
                        {
                            OtherStatement nd = (OtherStatement)top;
                            nd.ReplaceText = nextToken.Substring(1, nextToken.Length-2);
                        }
                        else if (top is Filename)
                        {
                            Filename fn = (Filename)top;
                            fn.Path = nextToken.Substring(1, nextToken.Length-2);
                        }
                        if (top.IsFull)
                            nodes.Pop();
                    }
                    else if (topElem == "ID")
                    {
                        StringNode sn = new StringNode();
                        sn.Token = nextToken;
                        nodes.Peek().Nodes.Add(sn);
                        if (nodes.Peek().IsFull)
                            nodes.Pop();
                    }
                    else
                    {
                        throw new RDParser.SyntaxError("Token did not match top of LL(1) Parse Stack.");
                    }
                }
            }

            return miniRE;
        }
Beispiel #3
0
        public void Replace()
        {
            SymbolTable table = new SymbolTable();

            MiniRE miniRe = new MiniRE();

            StatementList sl = new StatementList();
            miniRe.StatementList = sl;

            Statement statement = new Statement();
            statement.Id = new StringNode("x");
            sl.Statement = statement;

            StatementListTail tail = new StatementListTail();
            sl.Tail = tail;

            Statement s2 = new Statement();
            s2.Id = new StringNode("y");
            tail.Statement = s2;

            StatementListTail tail2 = new StatementListTail();
            tail.Tail = tail2;

            #region Statement 1
            AssignmentStatement asStmt = new AssignmentStatement();
            statement.AssignmentStatement = asStmt;

            Exp exp = new Exp();
            asStmt.Exp = exp;

            Term term = new Term();
            exp.Term = term;

            Regex regex = new Regex();
            regex.Pattern = "a";
            term.Regex = regex;

            Filename filename = new Filename();
            filename.Path = "../../../TestFiles/file1.txt";
            term.Filename = filename;
            #endregion

            #region Statement 2

            AssignmentStatement as2 = new AssignmentStatement();
            as2.Type = AssignmentStatementType.Length;
            s2.AssignmentStatement = as2;

            Exp exp2 = new Exp();
            exp2.Id = new StringNode("x");
            as2.Exp = exp2;

            #endregion

            #region Statement 3

            Statement s3 = new Statement();
            tail2.Statement = s3;

            OtherStatement os = new OtherStatement();
            os.Mode = OtherStatementMode.Replace;
            os.ReplaceText = "b";
            s3.OtherStatement = os;

            Regex regex3 = new Regex();
            regex3.Pattern = "(a|A)";
            os.Regex = regex3;

            Filenames filenames = new Filenames();
            os.Filenames = filenames;

            Filename filename2 = new Filename();
            filename2.Path = "../../../TestFiles/file2.txt";
            filenames.Filename = filename2;

            Filename destimation = new Filename();
            destimation.Path = "../../../TestFiles/file3.txt";
            filenames.Destimation = destimation;

            #endregion

            miniRe.Execute(table);

            String expected = "brgument brgumentbtive predicbment mentoring bpple";
            StringBuilder actual = new StringBuilder();
            using(FileStream fs = new FileStream("../../../TestFiles/file3.txt", FileMode.Open))
            {
                using(StreamReader sr = new StreamReader(fs))
                {
                    actual.Append(sr.ReadToEnd());
                }
            }
            Assert.AreEqual(expected, actual.ToString());
        }
Beispiel #4
0
        public void RecursiveReplace()
        {
            SymbolTable table = new SymbolTable();

            MiniRE miniRe = new MiniRE();

            StatementList sl = new StatementList();
            miniRe.StatementList = sl;

            Statement statement = new Statement();
            sl.Statement = statement;

            OtherStatement os = new OtherStatement();
            os.Mode = OtherStatementMode.RecursiveReplace;
            statement.OtherStatement = os;

            Regex regex = new Regex();
            regex.Pattern = "abc";
            os.Regex = regex;

            Filenames filenames = new Filenames();
            os.Filenames = filenames;

            Filename source = new Filename();
            source.Path = "../../../TestFiles/abc.txt";
            filenames.Filename = source;

            Filename dest = new Filename();
            dest.Path = "../../../TestFiles/abc_output.txt";
            filenames.Destimation = dest;

            miniRe.Execute(table);

            String expected = "bbc  a  bc  a";
            StringBuilder actual = new StringBuilder();
            using (FileStream fs = new FileStream("../../../TestFiles/abc_output.txt", FileMode.Open))
            {
                using (StreamReader sr = new StreamReader(fs))
                {
                    actual.Append(sr.ReadToEnd());
                }
            }
            Assert.AreEqual(expected, actual.ToString());
        }
Beispiel #5
0
        public void Print()
        {
            SymbolTable table = new SymbolTable();

            MiniRE miniRe = new MiniRE();

            StatementList sl = new StatementList();
            miniRe.StatementList = sl;

            Statement statement = new Statement();
            statement.Id = new StringNode("x");
            sl.Statement = statement;

            StatementListTail tail = new StatementListTail();
            sl.Tail = tail;

            Statement s2 = new Statement();
            s2.Id = new StringNode("y");
            tail.Statement = s2;

            StatementListTail tail2 = new StatementListTail();
            tail.Tail = tail2;

            #region Statement 1
            AssignmentStatement asStmt = new AssignmentStatement();
            statement.AssignmentStatement = asStmt;

            Exp exp = new Exp();
            asStmt.Exp = exp;

            Term term = new Term();
            exp.Term = term;

            Regex regex = new Regex();
            regex.Pattern = "a";
            term.Regex = regex;

            Filename filename = new Filename();
            filename.Path = "../../../TestFiles/file1.txt";
            term.Filename = filename;
            #endregion

            #region Statement 2

            AssignmentStatement as2 = new AssignmentStatement();
            as2.Type = AssignmentStatementType.Length;
            s2.AssignmentStatement = as2;

            Exp exp2 = new Exp();
            exp2.Id = new StringNode("x");
            as2.Exp = exp2;

            #endregion

            #region Statement 3

            Statement s3 = new Statement();
            tail2.Statement = s3;

            OtherStatement print = new OtherStatement();
            print.Mode = OtherStatementMode.Print;
            s3.OtherStatement = print;

            ExpList expList = new ExpList();
            print.ExpList = expList;

            Exp exp3 = new Exp();
            exp3.Id = new StringNode("y");
            expList.Exp = exp3;

            #endregion

            using (MemoryStream ms = new MemoryStream())
            {
                StreamWriter sw = new StreamWriter(ms);
                Console.SetOut(sw);
                miniRe.Execute(table);
                sw.Flush();

                ms.Position = 0;
                using (StreamReader sr = new StreamReader(ms))
                {
                    String actual = sr.ReadToEnd();
                    Assert.AreEqual(7, Int32.Parse(actual));
                }
            }
        }
Beispiel #6
0
        public void MiniRE()
        {
            SymbolTable table = new SymbolTable();

            MiniRE miniRe = new MiniRE();

            StatementList sl = new StatementList();
            miniRe.StatementList = sl;

            Statement statement = new Statement();
            statement.Id = new StringNode("x");
            sl.Statement = statement;

            StatementListTail tail = new StatementListTail();
            sl.Tail = tail;

            Statement s2 = new Statement();
            s2.Id = new StringNode("y");
            tail.Statement = s2;

            StatementListTail tail2 = new StatementListTail();
            tail.Tail = tail2;

            #region Statement 1
            AssignmentStatement asStmt = new AssignmentStatement();
            statement.AssignmentStatement = asStmt;

            Exp exp = new Exp();
            asStmt.Exp = exp;

            Term term = new Term();
            exp.Term = term;

            Regex regex = new Regex();
            regex.Pattern = "a";
            term.Regex = regex;

            Filename filename = new Filename();
            filename.Path = "../../../TestFiles/file1.txt";
            term.Filename = filename;
            #endregion

            #region Statement 2

            AssignmentStatement as2 = new AssignmentStatement();
            as2.Type = AssignmentStatementType.Length;
            s2.AssignmentStatement = as2;

            Exp exp2 = new Exp();
            exp2.Id = new StringNode("x");
            as2.Exp = exp2;

            #endregion

            miniRe.Execute(table);

            object value = table["x"];

            Assert.AreEqual(7, (int)table["y"]);
        }
Beispiel #7
0
        public void Maxfreqstr()
        {
            SymbolTable table = new SymbolTable();

            MiniRE miniRe = new MiniRE();

            StatementList sl = new StatementList();
            miniRe.StatementList = sl;

            Statement statement = new Statement();
            statement.Id = new StringNode("x");
            sl.Statement = statement;

            AssignmentStatement as1 = new AssignmentStatement();
            statement.AssignmentStatement = as1;

            Exp exp1 = new Exp();
            as1.Exp = exp1;

            Term term = new Term();
            exp1.Term = term;

            Regex r1 = new Regex();
            r1.Pattern = "[a-z]([a-z])*";
            term.Regex = r1;

            Filename filename = new Filename();
            filename.Path = "../../../TestFiles/maxfreq.txt";
            term.Filename = filename;

            StatementListTail tail = new StatementListTail();
            sl.Tail = tail;

            Statement s2 = new Statement();
            s2.Id = new StringNode("y");
            tail.Statement = s2;

            AssignmentStatement as2 = new AssignmentStatement();
            as2.Type = AssignmentStatementType.MaxFreqString;
            as2.Id = new StringNode("x");
            s2.AssignmentStatement = as2;

            miniRe.Execute(table);
            Assert.AreEqual("hey", table["y"]);
        }