Exemple #1
0
        public void ShouldBuildAutomatonTableFromTwoSequencesUsingAnyTerminal()
        {
            AutomatonTableFactory <char> factory;
            IAutomatonTable <char>       automatonTable;
            LexicalRule          rule1, rule2;
            AutomatonTableParser parser;

            factory = new AutomatonTableFactory <char>();

            rule1 = RuleHelper.BuildRule("A*=a.c;");
            rule2 = RuleHelper.BuildRule("B*=abc;");

            automatonTable = factory.BuildAutomatonTable(SituationCollectionFactoryHelper.BuildSituationCollectionFactory(new LexicalRule[] { rule1, rule2 }), new DistinctInputFactory(new RangeValueProvider()));
            Assert.IsNotNull(automatonTable);

            parser = new AutomatonTableParser(automatonTable);


            Assert.AreEqual(1, parser.ActionCount);
            Assert.IsTrue(parser.Parse(new TerminalInput('a')));
            Assert.AreEqual(3, parser.ActionCount);
            Assert.IsTrue(parser.Parse(new TerminalInput('b')));
            Assert.AreEqual(1, parser.ActionCount);
            Assert.IsTrue(parser.Parse(new TerminalInput('c')));

            parser.Reset();

            Assert.AreEqual(1, parser.ActionCount);
            Assert.IsTrue(parser.Parse(new TerminalInput('a')));
            Assert.AreEqual(3, parser.ActionCount);
            Assert.IsTrue(parser.Parse(new TerminalInput('c')));
            Assert.AreEqual(1, parser.ActionCount);
            Assert.IsTrue(parser.Parse(new TerminalInput('c')));
        }
Exemple #2
0
        public void ShouldBuildStates3()
        {
            AutomatonTableFactory factory;

            State[] states;
            SituationSegmentFactory segmentFactory;
            ISituation rootSituation;


            segmentFactory = new SituationSegmentFactory();

            rootSituation = segmentFactory.BuildRootSituation(
                new Rule("A", Parse.Character('a').Then(Parse.Character('b').ThenCharacter('b').ThenCharacter('b').Perhaps()).ThenCharacter('c')),
                new Rule("B", Parse.Character('a').ThenCharacter('b').ThenCharacter('e'))
                );

            factory = new AutomatonTableFactory();
            states  = factory.BuildStates(rootSituation);

            Assert.AreEqual(7, states.Length);
            Assert.AreEqual("A", ParseStates(states, 'a', 'b', 'b', 'b', 'c'));
            Assert.AreEqual("A", ParseStates(states, 'a', 'c'));
            Assert.ThrowsException <InvalidOperationException>(() => ParseStates(states, 'a', 'b', 'b', 'b', 'e'));
            Assert.AreEqual("B", ParseStates(states, 'a', 'b', 'e'));
        }
Exemple #3
0
        public void ShouldNotBuildStatesAndThrowExceptions()
        {
            AutomatonTableFactory factory;

            factory = new AutomatonTableFactory();
            Assert.ThrowsException <ArgumentNullException>(() => factory.BuildStates(null));
        }
Exemple #4
0
        public static IAutomatonTable <char> BuildAutomatonTable(IEnumerable <string> Rules)
        {
            IAutomatonTableFactory <char>     automatonTableFactory;
            IAutomatonTable <char>            automatonTable;
            SituationCollectionFactory <char> situationCollectionFactory;
            DistinctInputFactory distinctInputFactory;

            distinctInputFactory       = new DistinctInputFactory(new RangeValueProvider());
            automatonTableFactory      = new AutomatonTableFactory <char>( );
            situationCollectionFactory = new SituationCollectionFactory <char>(SituationGraphHelper.BuildSituationGraph(Rules.Select(item => RuleHelper.BuildRule(item)).ToArray()));
            automatonTable             = automatonTableFactory.BuildAutomatonTable(situationCollectionFactory, distinctInputFactory);

            return(automatonTable);
        }
Exemple #5
0
        //private ICharReader reader;

        public Lexer(params IRule[] Rules)
        {
            if (Rules == null)
            {
                throw new ArgumentNullException("Rules");
            }


            IAutomatonTableFactory   automatonTableFactory;
            ISituationSegmentFactory situationSegmentFactory;
            ISituation rootSituation;

            situationSegmentFactory = new SituationSegmentFactory();
            rootSituation           = situationSegmentFactory.BuildRootSituation(Rules);

            automatonTableFactory = new AutomatonTableFactory();
            this.states           = automatonTableFactory.BuildStates(rootSituation);
        }
Exemple #6
0
        public void ShouldBuildAutomatonTableFromExtendedSequence()
        {
            AutomatonTableFactory <char> factory;
            IAutomatonTable <char>       automatonTable;
            LexicalRule          rule;
            Sequence             predicate;
            AutomatonTableParser parser;

            factory = new AutomatonTableFactory <char>( );

            predicate = new Sequence();
            predicate.Items.Add(new Terminal('a'));
            predicate.Items.Add(new Terminal('b'));
            predicate.Items.Add(new Terminal('c'));

            rule = new LexicalRule()
            {
                Name = "rule", IsAxiom = true
            };
            rule.Predicate = new ZeroOrMore()
            {
                Item = predicate
            };

            automatonTable = factory.BuildAutomatonTable(SituationCollectionFactoryHelper.BuildSituationCollectionFactory(rule.AsEnumerable()), new DistinctInputFactory(new RangeValueProvider()));
            Assert.IsNotNull(automatonTable);

            parser = new AutomatonTableParser(automatonTable);

            Assert.AreEqual(1, parser.ActionCount);
            Assert.IsTrue(parser.Parse(new TerminalInput('a')));
            Assert.AreEqual(1, parser.ActionCount);
            Assert.IsTrue(parser.Parse(new TerminalInput('b')));
            Assert.AreEqual(1, parser.ActionCount);
            Assert.IsTrue(parser.Parse(new TerminalInput('c')));
            Assert.AreEqual(1, parser.ActionCount);
            Assert.IsTrue(parser.Parse(new TerminalInput('a')));
        }
Exemple #7
0
        public void ShouldBuildAutomatonTableFromBasicSequence()
        {
            AutomatonTableFactory <char> factory;
            IAutomatonTable <char>       automatonTable;
            LexicalRule          rule;
            AutomatonTableParser parser;

            factory = new AutomatonTableFactory <char>(  );

            rule = RuleHelper.BuildRule("A*=abc;");

            automatonTable = factory.BuildAutomatonTable(SituationCollectionFactoryHelper.BuildSituationCollectionFactory(rule.AsEnumerable()), new DistinctInputFactory(new RangeValueProvider()));
            Assert.IsNotNull(automatonTable);

            parser = new AutomatonTableParser(automatonTable);

            Assert.AreEqual(1, parser.ActionCount);
            Assert.IsTrue(parser.Parse(new TerminalInput('a')));
            Assert.AreEqual(1, parser.ActionCount);
            Assert.IsTrue(parser.Parse(new TerminalInput('b')));
            Assert.AreEqual(1, parser.ActionCount);
            Assert.IsTrue(parser.Parse(new TerminalInput('c')));
        }
Exemple #8
0
        public void ShouldBuildStates()
        {
            AutomatonTableFactory factory;

            State[] states;
            SituationSegmentFactory segmentFactory;
            ISituation rootSituation;


            segmentFactory = new SituationSegmentFactory();

            rootSituation = segmentFactory.BuildRootSituation(
                new Rule("A", Parse.Character('a').ThenCharacter('b').ThenCharacter('c')),
                new Rule("B", Parse.Character('a').ThenCharacter('b').ThenAnyCharacter())
                );

            factory = new AutomatonTableFactory();
            states  = factory.BuildStates(rootSituation);

            Assert.AreEqual(5, states.Length);
            Assert.AreEqual("A", ParseStates(states, 'a', 'b', 'c'));
            Assert.AreEqual("B", ParseStates(states, 'a', 'b', 'd'));
        }
Exemple #9
0
        //private static char[] alphabet = new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 's', 't', '*' };

        public MainWindow()
        {
            InitializeComponent();

            lexicalAutomatonTableFactory  = new AutomatonTableFactory <char>();
            syntaxicAutomatonTableFactory = new AutomatonTableFactory <FSMLib.SyntaxicAnalysis.Token>();

            views = new ObservableCollection <GraphView>();
            tabControl.ItemsSource = views;

            CreateLexicalView(@"S*=\$;",
                              @"E=\\$;",
                              @"NE=a;",
                              @"L*={NE}|{E};",
                              @"S*={L}+;");

            /*@"Symbol*=\[|\]|\{|\}|\.|\+|\*|\?|\||\;|\=|\!|\\;",
             *      @"EscapedChar=\\.;",
             *      @"NonEscapedChar=!\[|!\]|!\{|!\}|!\.|!\+|!\*|!\?|!\||!\;|!\=|!\!|!\\;",
             *      @"Letter*={NonEscapedChar}|{EscapedChar};",
             *      @"String*={Letter}+;",*/

            CreateSyntaxicView(@"A*=<C,a>;");
            //@"A*=[a-c]e;",
            //@"B*=[b-d]e;");


            CreateLexicalView(@"Symbol *=\[|\]|\{|\}|\.|\+|\*|\?|\||\;|\=|\!|\\;");

            CreateLexicalView(@"Terminal =[a-z]|[A-Z];",
                              @"NonAxiomRuleName={Terminal}+;",
                              @"AxiomRuleName={Terminal}+\*;",
                              @"RuleName*={NonAxiomRuleName}|{AxiomRuleName};");


            CreateLexicalView("A*=ab{C}*;", "C=c;");
            CreateLexicalView("A*=ab{B};", "B={C};", "C={D};", "D=c;");
            CreateLexicalView("A*=a.c;", "B*=abc;");

            CreateLexicalView(
                @"A*=[a-c]e;",
                @"B*=[b-d]e;");

            CreateLexicalView(
                @"L=[a-b]|[c-d];",
                @"N={L}+;",
                @"A={L}+\*;",
                @"R*={N}|{A};");

            CreateLexicalView("A*=a{S}a;", "S={S}b;", "S=c;");

            CreateLexicalView("A=abc;", "B=def;", "E*={A}|{B};");
            CreateLexicalView("A*=a{B}{C};", "B={C};", "C=b;");
            CreateLexicalView("A*=a{BCD}e;", "BCD=b{C}d;", "C=c;");

            CreateLexicalView("A*=a{B}c;", "B={C};", "C=b;");
            CreateLexicalView("A*=a{B}d;", "B=bc?;");
            CreateLexicalView("A*=a{S}a;", "S={S}b;", "S=c;");

            CreateLexicalView("A*=abcde;");
            CreateLexicalView("A*=a{S}a;", "S=st;");
            CreateLexicalView("A*=a{B}a;", "B=b{A}b;");            //*/
        }