簡體   English   中英

解析 if-else if 語句算法

[英]Parsing if-else if statement algorithm

我正在嘗試為將構建和執行 SQL 語句的 if-else 類型結構創建一個非常簡單的解析器。

我不是測試執行語句的條件,而是測試條件來構建一個字符串。

語句的一個例子是:

select column1
from
#if(VariableA = Case1)
table1
#else if(VariableA = Case2)
table2
#else
defaultTable
#end

如果 VariableA 等於 Case1,則結果字符串應為: select column1 from table1

一個更復雜的例子是嵌套的 if 語句:

select column1
from
#if(VariableA = Case1)
#if(VariableB = Case3)
    table3
#else
    table4
#else if(VariableA = Case2)
table2
#else
defaultTable
#end

這是我真正遇到麻煩的地方,我想不出正確識別每個 if-else-end 組的好方法。

另外,我不確定跟蹤“else”子句中的字符串是否應評估為真的好方法。

我一直在網上尋找不同類型的解析算法,所有這些算法看起來都非常抽象和復雜。

對於這個非計算機科學專業,有什么好的起點建議嗎?

我編寫了一個簡單的解析器,並根據您提供的示例對其進行了測試。 如果您想了解有關解析的更多信息,我建議您閱讀 Niklaus Wirth 的Compiler Construction

第一步總是以適當的方式寫下您的語言的語法。 我選擇了EBNF,非常簡單易懂。

| 分離備選方案。

[]包含選項。

{}表示重復(零次、一次或多次)。

()組表達式(此處未使用)。

此描述不完整,但我提供的鏈接對其進行了更詳細的描述。

EBNF 語法

LineSequence = { TextLine | IfStatement }.
TextLine     = <string>.
IfStatement  = IfLine LineSequence { ElseIfLine LineSequence } [ ElseLine LineSequence ] EndLine.
IfLine       = "#if" "(" Condition ")".
ElseLine     = "#else".
ElseIfLine   = "#else" "if" "(" Condition ")".
EndLine      = "#end".
Condition    = Identifier "=" Identifier.
Identifier   = <letter_or_underline> { <letter_or_underline> | <digit> }.

解析器嚴格遵循語法,即將重復轉換為循環,將替代轉換為 if-else 語句,依此類推。

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace Example.SqlPreprocessor
{
    class Parser
    {
        enum Symbol
        {
            None,
            LPar,
            RPar,
            Equals,
            Text,
            NumberIf,
            If,
            NumberElse,
            NumberEnd,
            Identifier
        }

        List<string> _input; // Raw SQL with preprocessor directives.
        int _currentLineIndex = 0;

        // Simulates variables used in conditions
        Dictionary<string, string> _variableValues = new Dictionary<string, string> { 
            { "VariableA", "Case1" },
            { "VariableB", "CaseX" }
        };

        Symbol _sy; // Current symbol.
        string _string; // Identifier or text line;
        Queue<string> _textQueue = new Queue<string>(); // Buffered text parts of a single line.
        int _lineNo; // Current line number for error messages.
        string _line; // Current line for error messages.

        /// <summary>
        /// Get the next line from the input.
        /// </summary>
        /// <returns>Input line or null if no more lines are available.</returns>
        string GetLine()
        {
            if (_currentLineIndex >= _input.Count) {
                return null;
            }
            _line = _input[_currentLineIndex++];
            _lineNo = _currentLineIndex;
            return _line;
        }

        /// <summary>
        /// Get the next symbol from the input stream and stores it in _sy.
        /// </summary>
        void GetSy()
        {
            string s;
            if (_textQueue.Count > 0) { // Buffered text parts available, use one from these.
                s = _textQueue.Dequeue();
                switch (s.ToLower()) {
                    case "(":
                        _sy = Symbol.LPar;
                        break;
                    case ")":
                        _sy = Symbol.RPar;
                        break;
                    case "=":
                        _sy = Symbol.Equals;
                        break;
                    case "if":
                        _sy = Symbol.If;
                        break;
                    default:
                        _sy = Symbol.Identifier;
                        _string = s;
                        break;
                }
                return;
            }

            // Get next line from input.
            s = GetLine();
            if (s == null) {
                _sy = Symbol.None;
                return;
            }

            s = s.Trim(' ', '\t');
            if (s[0] == '#') { // We have a preprocessor directive.
                // Split the line in order to be able get its symbols.
                string[] parts = Regex.Split(s, @"\b|[^#_a-zA-Z0-9()=]");
                // parts[0] = #
                // parts[1] = if, else, end
                switch (parts[1].ToLower()) {
                    case "if":
                        _sy = Symbol.NumberIf;
                        break;
                    case "else":
                        _sy = Symbol.NumberElse;
                        break;
                    case "end":
                        _sy = Symbol.NumberEnd;
                        break;
                    default:
                        Error("Invalid symbol #{0}", parts[1]);
                        break;
                }

                // Store the remaining parts for later.
                for (int i = 2; i < parts.Length; i++) {
                    string part = parts[i].Trim(' ', '\t');
                    if (part != "") {
                        _textQueue.Enqueue(part);
                    }
                }
            } else { // We have an ordinary SQL text line.
                _sy = Symbol.Text;
                _string = s;
            }
        }

        void Error(string message, params  object[] args)
        {
            // Make sure parsing stops here
            _sy = Symbol.None;
            _textQueue.Clear();
            _input.Clear();

            message = String.Format(message, args) +
                      String.Format(" in line {0}\r\n\r\n{1}", _lineNo, _line);
            Output("------");
            Output(message);
            MessageBox.Show(message, "Error");
        }

        /// <summary>
        /// Writes the processed line to a (simulated) output stream.
        /// </summary>
        /// <param name="line">Line to be written to output</param>
        void Output(string line)
        {
            Console.WriteLine(line);
        }

        /// <summary>
        /// Starts the parsing process.
        /// </summary>
        public void Parse()
        {
            // Simulate an input stream.
            _input = new List<string> {
                "select column1",
                "from",
                "#if(VariableA = Case1)",
                "    #if(VariableB = Case3)",
                "        table3",
                "    #else",
                "        table4",
                "    #end",
                "#else if(VariableA = Case2)",
                "    table2",
                "#else",
                "    defaultTable",
                "#end"
            };

            // Clear previous parsing
            _textQueue.Clear();
            _currentLineIndex = 0;

            // Get first symbol and start parsing
            GetSy();
            if (LineSequence(true)) { // Finished parsing successfully.
                //TODO: Do something with the generated SQL
            } else { // Error encountered.
                Output("*** ABORTED ***");
            }
        }

        // The following methods parse according the the EBNF syntax.

        bool LineSequence(bool writeOutput)
        {   
            // EBNF:  LineSequence = { TextLine | IfStatement }.
            while (_sy == Symbol.Text || _sy == Symbol.NumberIf) {
                if (_sy == Symbol.Text) {
                    if (!TextLine(writeOutput)) {
                        return false;
                    }
                } else { // _sy == Symbol.NumberIf
                    if (!IfStatement(writeOutput)) {
                        return false;
                    }
                }
            }
            return true;
        }

        bool TextLine(bool writeOutput)
        {
            // EBNF:  TextLine = <string>.
            if (writeOutput) {
                Output(_string);
            }
            GetSy();
            return true;
        }

        bool IfStatement(bool writeOutput)
        {
            // EBNF:  IfStatement = IfLine LineSequence { ElseIfLine LineSequence } [ ElseLine LineSequence ] EndLine.
            bool result;
            if (IfLine(out result) && LineSequence(writeOutput && result)) {
                writeOutput &= !result; // Only one section can produce an output.
                while (_sy == Symbol.NumberElse) {
                    GetSy();
                    if (_sy == Symbol.If) { // We have an #else if
                        if (!ElseIfLine(out result)) {
                            return false;
                        }
                        if (!LineSequence(writeOutput && result)) {
                            return false;
                        }
                        writeOutput &= !result; // Only one section can produce an output.
                    } else { // We have a simple #else
                        if (!LineSequence(writeOutput)) {
                            return false;
                        }
                        break; // We can have only one #else statement.
                    }
                }
                if (_sy != Symbol.NumberEnd) {
                    Error("'#end' expected");
                    return false;
                }
                GetSy();
                return true;
            }
            return false;
        }

        bool IfLine(out bool result)
        {
            // EBNF:  IfLine = "#if" "(" Condition ")".
            result = false;
            GetSy();
            if (_sy != Symbol.LPar) {
                Error("'(' expected");
                return false;
            }
            GetSy();
            if (!Condition(out result)) {
                return false;
            }
            if (_sy != Symbol.RPar) {
                Error("')' expected");
                return false;
            }
            GetSy();
            return true;
        }

        private bool Condition(out bool result)
        {
            // EBNF:  Condition = Identifier "=" Identifier.
            string variable;
            string expectedValue;
            string variableValue;

            result = false;
            // Identifier "=" Identifier
            if (_sy != Symbol.Identifier) {
                Error("Identifier expected");
                return false;
            }
            variable = _string; // The first identifier is a variable.
            GetSy();
            if (_sy != Symbol.Equals) {
                Error("'=' expected");
                return false;
            }
            GetSy();
            if (_sy != Symbol.Identifier) {
                Error("Value expected");
                return false;
            }
            expectedValue = _string;  // The second identifier is a value.

            // Search the variable
            if (_variableValues.TryGetValue(variable, out variableValue)) {
                result = variableValue == expectedValue; // Perform the comparison.
            } else {
                Error("Variable '{0}' not found", variable);
                return false;
            }

            GetSy();
            return true;
        }

        bool ElseIfLine(out bool result)
        {
            // EBNF:  ElseIfLine = "#else" "if" "(" Condition ")".
            result = false;
            GetSy(); // "#else" already processed here, we are only called if the symbol is "if"
            if (_sy != Symbol.LPar) {
                Error("'(' expected");
                return false;
            }
            GetSy();
            if (!Condition(out result)) {
                return false;
            }
            if (_sy != Symbol.RPar) {
                Error("')' expected");
                return false;
            }
            GetSy();
            return true;
        }
    }
}

請注意,嵌套的 if 語句以一種非常自然的方式自動處理。 首先,語法是遞歸表達的。 LineSequence可以包含IfStatment s,而IfStatment s 包含LineSequence s。 其次,這導致語法處理方法以遞歸方式相互調用。 因此,語法元素的嵌套被轉換為遞歸方法調用。

看看反諷

Irony 是一個用於在 .NET 平台上實現語言的開發工具包。 與大多數現有的 yacc/lex 風格的解決方案不同,Irony 不使用任何掃描器或解析器代碼從以專門元語言編寫的語法規范生成。 在 Irony 中,目標語言語法直接在 c# 中編碼,使用運算符重載來表達語法結構。 Irony 的掃描器和解析器模塊使用編碼為 c# 類的語法來控制解析過程。 有關 c# 類中的語法定義示例以及在工作解析器中使用它的示例,請參閱表達式語法示例。

我建議您使用現有的代碼生成器,例如... C# 或 T4 模板或 ASP.NET MVC 部分視圖。

但是,如果您想自己執行此操作,則需要某種遞歸(或等效的堆棧)。 它可以像這樣工作:

string BuildCode(string str)
{
 foreach(Match ifMatch in Regex.Matches("#if(?<condition>[^\n\r]*)[\r\n]*(?<body>.*?)#endif)
 {
  var condition = ifMatch.Groups["condition"].Value;
  return EvaluateCondition(condition) ? BuildCode(ifMatch.Value) : null;
 }
}

這是偽代碼。 你需要自己考慮清楚。 這也不支持 else 分支,但您可以輕松添加。

這是一個新答案:使用CodeDom編譯 C# 函數。 您可以使用 C# 的全部功能,但將 C# 代碼存儲在數據庫中。 這樣您就不必重新部署。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM