<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>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                在你的詞法分析器中,你可能想要維護一些狀態。這可能包括模式設置,符號表和其他細節。例如,假設你想要跟蹤`NUMBER`標記的出現個數。 一種方法是維護一個全局變量: ~~~ num_count = 0 def t_NUMBER(t): r'\d+' global num_count num_count += 1 t.value = int(t.value) return t ~~~ 如果你不喜歡全局變量,另一個記錄信息的地方是lexer對象內部。可以通過當前標記的lexer屬性訪問: ~~~ def t_NUMBER(t): r'\d+' t.lexer.num_count += 1 # Note use of lexer attribute t.value = int(t.value) return t lexer = lex.lex() lexer.num_count = 0 # Set the initial count ~~~ 上面這樣做的優點是當同時存在多個lexer實例的情況下,簡單易行。不過這看上去似乎是嚴重違反了面向對象的封裝原則。lexer的內部屬性(除了lineno)都是以lex開頭命名的(lexdata、lexpos)。因此,只要不以lex開頭來命名屬性就很安全的。 如果你不喜歡給lexer對象賦值,你可以自定義你的lexer類型,就像前面看到的那樣: ~~~ class MyLexer: ... def t_NUMBER(self,t): r'\d+' self.num_count += 1 t.value = int(t.value) return t def build(self, **kwargs): self.lexer = lex.lex(object=self,**kwargs) def __init__(self): self.num_count = 0 ~~~ 如果你的應用會創建很多lexer的實例,并且需要維護很多狀態,上面的類可能是最容易管理的。 狀態也可以用閉包來管理,比如,在Python3中: ~~~ def MyLexer(): num_count = 0 ... def t_NUMBER(t): r'\d+' nonlocal num_count num_count += 1 t.value = int(t.value) return t ... ~~~
                  <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>

                              哎呀哎呀视频在线观看