Exemple #1
0
        public FSM Union(FSM other)
        {
            this.CheckNotStale("this");
            this.stale = true;
            other.CheckNotStale("other");
            other.stale = true;
            //let's use no epsilon transitions and merge instead,
            //since it's pretty simple
            FSM ret = new FSM();

            ret.initialState.SupplementWithTransitionsFromOtherNode(this.initialState);
            ret.initialState.SupplementWithTransitionsFromOtherNode(other.initialState);
            foreach (FSMNode node in this.finalStates)
            {
                ret.finalStates.Add(node);
            }
            foreach (FSMNode node in other.finalStates)
            {
                ret.finalStates.Add(node);
            }
            return(ret);
        }
Exemple #2
0
 private static IRecognizerFSA QuotePatternRecognizer(string begin, string end)
 {
     return(FSM.ForConstantStringPattern(begin).Concatenation(FSM.ForPredicate(AnyChar).Closure())
            .Concatenation(FSM.ForConstantStringPattern(end)).AsMinRecognizerFSA());
 }
Exemple #3
0
        public static void Main(string[] args)
        {
            //some text to lex
            StringBuilder sb = new StringBuilder();

            AppendLine("for  in   while   //this is a comment", sb);
            AppendLine("   ", sb);
            AppendLine("/*", sb);
            AppendLine("multi", sb);
            AppendLine("line", sb);
            AppendLine("comment", sb);
            AppendLine("*/", sb);
            AppendLine("fofds \"afdsafd\" whdsafdsa", sb);

            //let's turn the text into a list of char/int pairs
            IList <CharIntPair> cips = new List <CharIntPair> ();
            int i = 0;

            foreach (char c in sb.ToString())
            {
                cips.Add(new CharIntPair(c, i));
                ++i;
            }

            //to lex, we need a list of token types
            IList <TokenType <CharIntPairLexerAdapter> > types =
                new List <TokenType <CharIntPairLexerAdapter> >();


            //add a token type for a stretch of whitespace
            types.Add(new TokenType <CharIntPairLexerAdapter>(
                          WhitespaceRecognizer(), delegate(IList <CharIntPairLexerAdapter> ciplas) {
                //add up numbers associated with objects
                int sum = 0;
                foreach (CharIntPairLexerAdapter cipla in ciplas)
                {
                    sum += cipla.cip.i;
                }
                Console.WriteLine("Got " + ciplas.Count + " chars of whitespace with sum " + sum);
            }
                          ));

            //add some keywords
            types.Add(new TokenType <CharIntPairLexerAdapter>(
                          FSM.ForConstantStringPattern("for").AsRecognizerFSA(), delegate(IList <CharIntPairLexerAdapter> ciplas) {
                Console.WriteLine("Got for at index " + ciplas[0].cip.i);
            }
                          ));
            types.Add(new TokenType <CharIntPairLexerAdapter>(
                          FSM.ForConstantStringPattern("in").AsRecognizerFSA(), delegate(IList <CharIntPairLexerAdapter> ciplas) {
                Console.WriteLine("Got in at index " + ciplas[0].cip.i);
            }
                          ));
            types.Add(new TokenType <CharIntPairLexerAdapter> (
                          FSM.ForConstantStringPattern("while").AsRecognizerFSA(), delegate(IList <CharIntPairLexerAdapter> ciplas) {
                Console.WriteLine("Got while at index " + ciplas [0].cip.i);
            }
                          ));

            //add a token type for a single line comment
            types.Add(new TokenType <CharIntPairLexerAdapter>(
                          QuotePatternRecognizer("//", System.Environment.NewLine), delegate(IList <CharIntPairLexerAdapter> ciplas) {
                StringBuilder sb2 = new StringBuilder();
                foreach (CharIntPairLexerAdapter cipla in ciplas)
                {
                    sb2.Append(cipla.cip.c);
                }
                Console.WriteLine("Got single line comment:");
                Console.Write(sb2.ToString());
            }
                          ));

            //add a token type for a multiline comment
            types.Add(new TokenType <CharIntPairLexerAdapter>(
                          QuotePatternRecognizer("/*", "*/"), delegate(IList <CharIntPairLexerAdapter> ciplas) {
                StringBuilder sb2 = new StringBuilder();
                foreach (CharIntPairLexerAdapter cipla in ciplas)
                {
                    sb2.Append(cipla.cip.c);
                }
                Console.WriteLine("Got multiline comment:");
                Console.WriteLine(sb2.ToString());
            }
                          ));

            //add a token type for a string literal
            types.Add(new TokenType <CharIntPairLexerAdapter>(
                          QuotePatternRecognizer("\"", "\""), delegate(IList <CharIntPairLexerAdapter> ciplas) {
                StringBuilder sb2 = new StringBuilder();
                foreach (CharIntPairLexerAdapter cipla in ciplas)
                {
                    sb2.Append(cipla.cip.c);
                }
                Console.WriteLine("Got string literal:");
                Console.WriteLine(sb2.ToString());
            }
                          ));

            LexicalAnalyzer <CharIntPairLexerAdapter> lexer = new LexicalAnalyzer <CharIntPairLexerAdapter>(
                types,
                delegate(CharIntPairLexerAdapter cipla) {
                Console.WriteLine("Got unhandlable char " + cipla.cip.c + " with number " + cipla.cip.i);
            }
                );

            foreach (CharIntPair cip in cips)
            {
                lexer.ProcessItem(new CharIntPairLexerAdapter(cip));
            }
            lexer.ProcessEOF();
        }
Exemple #4
0
 private static IRecognizerFSA WhitespaceRecognizer()
 {
     return(FSM.ForPredicate(Whitespace).Closure().AsRecognizerFSA());
 }