检查word文档中语句的逻辑

本文关键字:语句 word 文档 检查 | 更新日期: 2023-09-27 18:00:26

如何使用c#检查word文档中的IF、Then、Else和End IF条件

请给我推荐一些代码。。。或者帮我理清逻辑。提前谢谢。。

Stack stackWords = new Stack();
string[] words = SplitWords(sLine);
foreach(string s in words)
{
    if(s.Contains("IF") || s.Contains("ELSE") ||  (sLine.Contains("THEN")) || (sLine.Contains("ENDIF")))
    {
        for (; i < words.Length; i++)
        {
            if (words[i] == "IF" || words[i] == "ELSE" ||
            words[i] == "ELSEIF" || words[i] == "THEN" || words[i] == "END IF")
            {
                stackWords.Push(words[i]);
            }
        }
       // stackWords.Push(s);
    }
  //  MessageBox.Show(s);
}

检查word文档中语句的逻辑

根据@Evelie的回答,这里有一个进行检查的工作示例程序(我相信我在验证方法中涵盖了一些情况,我还添加了一些对序列中错误关键字的跟踪)。

备注:如果这有帮助,请将@Evelie的答案标记为答案。我只是为了给你留下一个完整的印象而贴出来的。

class Program
{
    static void Main(string[] args)
    {
        Stack stackWords = new Stack();
        var app = new Microsoft.Office.Interop.Word.Application();
        var file = Environment.CurrentDirectory + @"'doc.docx";
        var document = app.Documents.Open(file);
        try
        {
            int count = document.Words.Count;
            for (int i = 1; i <= count; i++)
            {
                string text = document.Words[i].Text.Trim().ToUpper();
                Vals val;
                if(Enum.TryParse<Vals>(text, out val))
                {
                    Console.WriteLine("Word {0} = {1}", i, text);
                    if (stackWords.Count > 0)
                    {
                        var peeked = (Vals)stackWords.Peek();
                        if (IsValidFollower(peeked, val))
                        {
                            stackWords.Push(val);
                        }
                        else
                        {
                            throw new Exception(String.Format("Exception occured at word {0}. {1} was not expected after {2}",i, val, peeked));
                        }
                    }
                    else
                    {
                        stackWords.Push(val);
                    }
                }
            }
            Console.WriteLine("Syntax sequence is valid");
        }
        catch (Exception ex)
        {
            Console.WriteLine("Syntax sequence is invalid");
            Console.WriteLine(ex.Message);
        }
        finally
        {
            document.Close();
            app.Quit();
        }
    }
    public enum Vals
    {
        IF,
        THEN,
        ENDIF,
        ELSE,
        ELSEIF
    }
    public static bool IsValidFollower(Vals val1, Vals val2)
    {
        if (val1 == Vals.IF)
            return val2 == Vals.THEN;
        if (val1 == Vals.THEN)
            return val2 == Vals.ENDIF || val2 == Vals.ELSEIF || val2 == Vals.ELSE;
        if (val1 == Vals.ENDIF)
            return val2 == Vals.IF;
        if (val1 == Vals.ELSE)
            return val2 == Vals.ENDIF;
        if (val1 == Vals.ELSEIF)
            return val2 == Vals.THEN;
        return false;
    }

获取自己和带有值的枚举。比字符串更容易处理。

    public enum Vals
    {
        IF,
        THEN,
        ENDIF,
        ELSE,
    }

我们有一种方法来比较下一个值是否与当前值正确。

    public static bool IsValidFollower(Vals val1, Vals val2)
    {
        if (val1 == Vals.IF)
            return val2 == Vals.THEN;
        if (val1 == Vals.THEN)
            return val2 == Vals.ENDIF;
        if (val1 == Vals.ENDIF)
            return val2 == Vals.IF || val2 == Vals.ELSE;
        if (val1 == Vals.ELSE)
            return val2 == Vals.THEN;
        return false;
    }

然后我们使用一个普通的列表来代替堆栈。(只是个人喜好,我不希望它们按相反的顺序排列)

        List<Vals> ListWords = new List<Vals>();
        foreach (string str in s.Split(' '))
        {
            if (str.Contains("ENDIF"))
                ListWords.Add(Vals.ENDIF);
            else if (str.Contains("ELSE"))
                ListWords.Add(Vals.ELSE);
            else if (str.Contains("THEN"))
                ListWords.Add(Vals.THEN);
            else if (str.Contains("IF"))
                ListWords.Add(Vals.IF);
        }

当我们拥有所有值的列表时。我们检查它们在逻辑上是否有效。

        bool valid = true;
        for (int i = ListWords.Count - 1; i +1< ListWords.Count; i++)
        {
            if (!IsValidFollower(ListWords[i], ListWords[i + 1]))
            {
                valid = false;
                break;
            }
        }