<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                ## **模式的定義與特點** 給分析對象定義一個語言,并定義該語言的文法表示,再設計一個解析器來解釋語言中的句子。也就是說,用編譯語言的方式來分析應用中的實例。這種模式實現了文法表達式處理的接口,該接口解釋一個特定的上下文。 >[danger] 這里提到的文法和句子的概念同編譯原理中的描述相同,“文法”指語言的語法規則,而“句子”是語言集中的元素。例如,漢語中的句子有很多,“我是中國人”是其中的一個句子,可以用一棵語法樹來直觀地描述語言中的句子。 角色: 環境角色(PlayContent):定義解釋規則的全局信息。 抽象解釋器(Empress):定義了部分解釋具體實現,封裝了一些由具體解釋器實現的接口。 具體解釋器(MusicNote):實現抽象解釋器的接口,進行具體的解釋執行。 定義語言的文法,并建立一個解釋器解釋該語言中的句子。每個用過 字典的童鞋都懂滴。 **好處:** 1. 擴展性好,靈活性大 。由于在解釋器模式中使用類來表示語言的文法規則,因此可以通過繼承等機制來改變或擴展文法。 2. 容易實現。在語法樹中的每個表達式節點類都是相似的,所以實現其文法較為容易。 **弊端:** 可能難以維護復雜的文法 1. 執行效率較低。解釋器模式中通常使用大量的循環和遞歸調用,當要解釋的句子較復雜時,其運行速度很慢,且代碼的調試過程也比較麻煩。 2. 會引起類膨脹。解釋器模式中的每條規則至少需要定義一個類,當包含的文法規則很多時,類的個數將急劇增加,導致系統**難以管理與維護**。 3. 可應用的場景比較少。在軟件開發中,需要定義語言文法的應用實例非常少,所以這種模式很少被使用到。 **應用場景:** 1. 當語言的文法較為簡單,且執行效率不是關鍵問題時。 2. 當問題重復出現,且可以用一種簡單的語言來進行表達時。 3. 當一個語言需要解釋執行,并且語言中的句子可以表示為一個抽象語法樹的時候,如 XML 文檔解釋。 4. 用于成對或者一對多的需求中 ## **模式的結構** 1. 抽象表達式(Abstract Expression)角色:定義解釋器的接口,約定解釋器的解釋操作,主要包含解釋方法 interpret()。 2. 終結符表達式(Terminal??? Expression)角色:是抽象表達式的子類,用來實現文法中與終結符相關的操作,文法中的每一個終結符都有一個具體終結表達式與之相對應。 3. 非終結符表達式(Nonterminal Expression)角色:也是抽象表達式的子類,用來實現文法中與非終結符相關的操作,文法中的每條規則都對應于一個非終結符表達式。 4. 環境(Context)角色:通常包含各個解釋器需要的數據或是公共的功能,一般用來傳遞被所有解釋器共享的數據,后面的解釋器可以從這里獲取這些值。 5. 客戶端(Client):主要任務是將需要分析的句子或表達式轉換成使用解釋器對象描述的抽象語法樹,然后調用解釋器的解釋方法,當然也可以通過環境角色間接訪問解釋器的解釋方法。 解釋器模式的結構圖如圖 ![](https://img.kancloud.cn/09/20/09204895da7ab5ad3b6f18caf1c5a0f9_710x349.png) ``` //抽象表達式類 interface AbstractExpression { public function interpret(String $info); //解釋方法 } //終結符表達式類 class TerminalExpression implements AbstractExpression { public function interpret(String $info) { //對終結符表達式的處理 } } //非終結符表達式類 class NonterminalExpression implements AbstractExpression { //exp: AbstractExpression private $exp1; private $exp2; public function interpret(String $info) { //非對終結符表達式的處理 } } //環境類 class Context { private $exp; public function __construct() { //數據初始化 } public function operation(String $info) { //調用相關表達式類的解釋方法 } } ``` 例子:“韶粵通”公交車卡的讀卡器程序,韶關或者廣州的老人、婦女、兒童免費,其余2元 ![](https://img.kancloud.cn/99/ed/99ed73820e00fdd9a8c77183fc1bc430_731x374.png) 文法規則: ~~~ <expression> ::= <city>的<person> <city> ::= 韶關|廣州 <person> ::= 老人|婦女|兒童 ~~~ 代碼 ``` //抽象表達式類 interface Expression { public function interpret(String $info); } //終結符表達式類 class TerminalExpression implements Expression { private $set=array(); public function __construct(array $data) { foreach ($data as $key => $value) { $this->set[]=$data[$key]; } } public function interpret(String $info) { if(in_array($info, $this->set)) { return true; } return false; } } //非終結符表達式類 class AndExpression implements Expression { private $city=null;//Expression private $person=null;//Expression public function __construct(Expression $city,Expression $person) { $this->city=$city; $this->person=$person; } public function interpret(String $info) { $s=explode('的',$info); return $this->city->interpret($s[0]) && $this->person->interpret($s[1]); } } //環境類 class Context { private $citys=array("韶關","廣州"); private $persons=array("老人","婦女","兒童"); private $cityPerson;//Expression public function __construct() { $city = new TerminalExpression($this->citys); $person = new TerminalExpression($this->persons); $this->cityPerson = new AndExpression($city,$person); } public function freeRide(String $info) { $ok=$this->cityPerson->interpret($info);//"韶關的老人" if($ok){ print_r("您是".$info.",您本次乘車免費!"); } else { print_r($info.",您不是免費人員,本次乘車扣費2元!"); } } } class InterpreterPatternDemo { public static function main() { $bus=new Context(); $bus->freeRide("韶關的老人"); $bus->freeRide("韶關的年輕人"); $bus->freeRide("廣州的婦女"); $bus->freeRide("廣州的兒童"); $bus->freeRide("山東的兒童"); } } InterpreterPatternDemo::main(); ``` 例子2 ~~~ class Expression { //抽象表示 function interpreter($str) { return $str; } } class ExpressionNum extends Expression { //表示數字 function interpreter($str) { switch($str) { case "0": return "零"; case "1": return "一"; case "2": return "二"; case "3": return "三"; case "4": return "四"; case "5": return "五"; case "6": return "六"; case "7": return "七"; case "8": return "八"; case "9": return "九"; } } } class ExpressionCharater extends Expression { //表示字符 function interpreter($str) { return strtoupper($str); } } class Interpreter { //解釋器 function execute($string) { $expression = null; for($i = 0;$i<strlen($string);$i++) { $temp = $string[$i]; switch(true) { case is_numeric($temp): $expression = new ExpressionNum(); break; default: $expression = new ExpressionCharater(); } echo $expression->interpreter($temp); echo "<br>"; } } } //client $obj = new Interpreter(); $obj->execute("123s45abc"); /* 輸出: 一 二 三 S 四 五 A B C */ ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看