lexicalanalyser.cs

来自「编译原理语法分析和词法分析综合实验: 源程序、可执行程序、测试程序文件、程序运行」· CS 代码 · 共 74 行

CS
74
字号
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace Lane.Compiler.WordAnalyser.Core
{
    public class LexicalAnalyser
    {
        private string source;
        private Regex re = new Regex(Properties.Resources.CRegex, RegexOptions.IgnorePatternWhitespace);
        private List<Token> tokens = new List<Token>();

        public string Source 
        { 
            get { return source; }
            set { SetSource(value); }
        }
        public Token[] Tokens
        {
            get { return tokens.ToArray(); }
        }

        public LexicalAnalyser() { }

        private void SetSource(string value)
        {
            // Prepare
            source = value;
            tokens.Clear();

            // Lexical Analysis
            Token t = null;
            Match m = re.Match(source);
            while (m.Success)
            {
                t = NewToken(m);
                System.Diagnostics.Trace.WriteLineIf(t == null, m);                
                tokens.Add(t);
                m = m.NextMatch();
            }        
        }

        private Token NewToken(Match match)
        {
            string gname = null;
            if ((gname = GetGroupName(match.Groups)) != null)
            {
                string group = gname;
                string value = match.Value;
                int index = match.Index;
                Token t = new Token(group, value, index);
                return t;
            }
            else
                return null;
        }

        private string GetGroupName(GroupCollection gc)
        {
            int i = 0;
            foreach (string s in re.GetGroupNames())
            {
                Group g = gc[s];
                if (int.TryParse(s, out i))
                    continue;
                if (g.Success)
                    return s;
            }
            return null;
        }
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?