1

我正在开发一个野牛程序,需要有一个允许它识别任何东西的最后一个选项。很像 else if...

谢谢

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*/
      }
4

1 回答 1

1

附加在标记非终端中识别出任何命令令牌后要运行的逻辑,如下所示。请注意, ' 产生式的右侧marker不匹配任何标记。

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,PLUS等,那么您将必须拼出您的词法分析器可以在command. 这不一定是一个好主意,原因有几个。

于 2012-11-16T14:45:14.787 回答