⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lexicalanalyser.cs

📁 编译原理语法分析和词法分析综合实验: 源程序、可执行程序、测试程序文件、程序运行说明文件、实验报告。
💻 CS
字号:
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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -