在CUP中:如何使某些内容可选以进行解析? [英] In CUP: How to make something optional to parse?

查看:103
本文介绍了在CUP中:如何使某些内容可选以进行解析?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

     PROC_DECL -> "proc" [ "ret" TYPE ] NAME
                  "(" [ PARAM_DECL { "," PARAM_DECL } ] ")"
                  "{" { DECL } { STMT } "}"

这是过程声明的语法.

您怎么说"ret" TYPE是可选的,而不会出现多种情况?

How do you say that the "ret" TYPE is optional without making multiple cases?

推荐答案

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

Use another production, say ret_stmt, which can be either empty or contain a single return statement so in your .cup file you will have this productions:

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

您可以对参数声明使用类似的方法,并添加生产param_list.

You can use a similar approach with parameters declaration, adding the production param_list.

这篇关于在CUP中:如何使某些内容可选以进行解析?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆