M语法和不止一种有效的解释

发布于 2024-07-21 01:44:13 字数 1939 浏览 5 评论 0原文

我认为我可能遇到的是悬而未决的其他问题,但我不太确定。 我负责编写的语言需要支持多行 if 语句以及单行 ie

if(conditions){
   print "multi";
   print "line";
}

if(conditions)
  print "single line"

Which 在我的语言中对应

if conditions then begin
end

if conditions then

我还需要添加对 else-if 和 else 的支持,两者都使用与上面有开始和结束。 我花了一段时间试图让它工作,并且已经接近了,但是根据我现在使用的语法,我收到了“此输入有多个有效解释...”错误。 这是我正在使用的 MGrammar 代码:

syntax sIf = tIf conditions:Common.List(sBooleanExpression) tThen sBegin statements:sStatements2 (tEnd | tEnd2) next:Common.List(sElseIf)?
              => If{Conditions{conditions}, Body{statements}, Next{next}}
              | tIf conditions:Common.List(sBooleanExpression) tThen statement:sSingleStatement next:Common.List(sElseIf)?
              => SingleIf{Conditions{conditions}, Body{statement}, Next{next}}
              ;

syntax sElseIf = tElseIf conditions:Common.List(sBooleanExpression) tThen sBegin statements:sStatements2 (tEnd | tEnd2) next:Common.List(sElseIf)?
                => ElseIf{Conditions{conditions}, Body{statements}, Next{next}}
                | tElseIf conditions:Common.List(sBooleanExpression) tThen statement:sSingleStatement next:Common.List(sElseIf)?
                => ElseIf{Conditions{conditions}, Body{statement}, Next{next}}
                | e:sElse => e;


syntax sElse         = tElse tThen sBegin es:sStatements2 (tEnd | tEnd2) => Else{Body{es}}
                     | tElse statement:sSingleStatement => Else{Body{statement}}
                     ;

这可能是第 10 个版本,因为我尝试了各种方法来使 if/else if/else 结构正常工作。

基本上,我的 Main 得到一个语句的 StatementList,sIf 是其中之一。 sIf 尝试查找多行或单行 if 语句,后跟 sElseIf ,这是可选的(您不必有 else)。

我认为问题是正则表达式编译器可能会将 else if 视为 sElse 后跟 sIf 语句,而不是 sElseIf 语句。 在预览模式下,它实际上完全按照我想要的方式绘制树,因此,如果有一种方法可以在我在 C# 应用程序中解析此消息时忽略此消息,那么我想这也会起作用。

I think what I may have is the dangling else problem, but I'm not really sure. The language I'm tasked with writing needs to support multi-line if statements as well as single line i.e.

if(conditions){
   print "multi";
   print "line";
}

and

if(conditions)
  print "single line"

Which in my language corresponds to

if conditions then begin
end

and

if conditions then

I also need to add in support for else-if's and else's, both using the same syntax as above with the begin's and end's. I've spent a while on trying to get this to work and I've come close, but with the grammar I'm using right now I'm getting the "this input has more than one valid interpretation..." error. Here's the MGrammar code I'm using:

syntax sIf = tIf conditions:Common.List(sBooleanExpression) tThen sBegin statements:sStatements2 (tEnd | tEnd2) next:Common.List(sElseIf)?
              => If{Conditions{conditions}, Body{statements}, Next{next}}
              | tIf conditions:Common.List(sBooleanExpression) tThen statement:sSingleStatement next:Common.List(sElseIf)?
              => SingleIf{Conditions{conditions}, Body{statement}, Next{next}}
              ;

syntax sElseIf = tElseIf conditions:Common.List(sBooleanExpression) tThen sBegin statements:sStatements2 (tEnd | tEnd2) next:Common.List(sElseIf)?
                => ElseIf{Conditions{conditions}, Body{statements}, Next{next}}
                | tElseIf conditions:Common.List(sBooleanExpression) tThen statement:sSingleStatement next:Common.List(sElseIf)?
                => ElseIf{Conditions{conditions}, Body{statement}, Next{next}}
                | e:sElse => e;


syntax sElse         = tElse tThen sBegin es:sStatements2 (tEnd | tEnd2) => Else{Body{es}}
                     | tElse statement:sSingleStatement => Else{Body{statement}}
                     ;

This is probably the 10th version of this, as I've tried various things to get the if/else if/else structure to work.

Basically, my Main gets a StatementList of Statements, sIf being one of them. sIf tries to find either the multi-line or the single line if statement followed by an sElseIf which is optional(you don't have to have an else).

I think the problem is the regex compiler may be seeing the else if as an sElse followed by an sIf statement, instead of an sElseIf statement. In preview mode it actually draws the tree exactly how I want it, so if there's a way to ignore this message when I parse it in my C# app then that would work too I guess.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文