2012-11-15 15 views
1

我開發一個程序野牛,需要有一個最後的選擇,允許它承認什麼。就像一個否則,如果...通用識別命令野牛

感謝

commands: F{ 
      t[top++] = 'F'; 
      } 
     |PLUS{ 
      t[top++] = '+'; 
      } 
     |MINUS{ 
      t[topo++] = '-'; 
      } 
     |ACOL { 
      t[top++] = '['; 
      } 
     |FCOL{ 
      t[top++] = ']'; 
      } 
     |POINT{ 
      t[top++] = '.'; 
      } 
     |EQUAL { 
      t[top++] = '='; 
      } 
     | { 
      /* generic command should be here 
      if any of the commands above were found it should run whatever is here*/ 
     } 
+0

有關於此的任何想法? – Arielsp

回答

1

附加要經過任何命令令牌中的標誌物非終端的認可,像下面這樣運行的邏輯。請注意,的製作右側與任何令牌都不匹配。

command_and_marker: command marker; 

command: F 
      { 
      t[top++] = 'F'; 
      } 
     | PLUS 
      { 
      t[top++] = '+'; 
      } 
     | MINUS 
      { 
      t[topo++] = '-'; 
      } 
     | ACOL 
      { 
      t[top++] = '['; 
      } 
     | FCOL 
      { 
      t[top++] = ']'; 
      } 
     | POINT 
      { 
      t[top++] = '.'; 
      } 
     | EQUAL 
      { 
      t[top++] = '='; 
      } 
marker:  { 
      /* generic command should be here 
      if any of the commands above were found it should run whatever is here*/ 
      } 

我已經制定了我的答案來匹配代碼中的註釋,這與您的問題的文本有些不一致。如果你想command匹配任何東西,而不僅僅是F,​​等,那麼你將不得不拼出所有的詞法分析器可以製作產生command的令牌。這不一定是個好主意,原因有幾個。