【问题标题】:In CUP: How to make something optional to parse?在 CUP 中:如何使某些内容可选解析?
【发布时间】:2013-02-10 11:30:06
【问题描述】:
     PROC_DECL -> "proc" [ "ret" TYPE ] NAME
                  "(" [ PARAM_DECL { "," PARAM_DECL } ] ")"
                  "{" { DECL } { STMT } "}"

这是过程声明的语法。

你怎么说"ret" TYPE是可选的,不做多个case?

【问题讨论】:

    标签: parsing compiler-construction jflex


    【解决方案1】:

    使用另一个产生式,例如 ret_stmt,它可以是空的,也可以包含单个 return 语句,因此在您的 .cup 文件中您将拥有以下产生式:

    ret_stmt ::= // empty 
                        {: /*your action for empty return statement*/ :}
                     // Single return statement          
                     | "ret":r TYPE:t
                        {: /*your action for single return statement*/ :}
    
    PROC_DECL ::= "proc":p ret_stmt:r NAME:n
                      "(" param_list:pl ")"
                      "{" { DECL } { STMT } "}"
                       {: /*your action for procedure declaration statement*/ :}
    

    您可以使用与参数声明类似的方法,添加生产参数列表。

    【讨论】:

    猜你喜欢
    • 2013-04-02
    • 1970-01-01
    • 1970-01-01
    • 2021-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-31
    • 2013-06-06
    相关资源
    最近更新 更多