用于解析 IF 语句的 M 语法

发布于 2024-08-04 19:44:24 字数 850 浏览 1 评论 0原文

我正在构建一个 MGrammar 规范来解析一些伪代码以查找特定的信息位。除了 1 个关键元素之外,大部分规范都可以正常工作。

伪代码支持 if-then-else 语法,但我一直无法找到令人满意的解析方法。确切的构造是...

IF 表达式运算符表达式 THEN
声明1
声明2
声明
ELSEIF 表达式 运算符 表达式 THEN
声明1
声明2
声明
其他
声明1
声明2
声明
ENDIF

...其中 Else 和 Elseif 是可选的。

到目前为止我所拥有的是: `语法 语句 = r:ReturnClause => r |
i:IfClause =>我 |
ei:ElseifClause => EI |
e:ElseClause => e |
结束:EndClause =>结束 |
v:表达式=> v ;

语法 IfClause = If 名称:标识符 运算符:运算符 then 语句:语句 => If[名称,运算符,Then[语句]];
语法 ElseifClause = Elseif 名称:标识符 运算符:运算符 then 语句:语句 =>; ElseIf[名称,运算符,Then[语句]];
语法 ElseClause = Else 语句:Statement =>否则[语句];
语法 EndClause = Endif; `

但是,'Then' 和 'Else' 之后的语句不够贪婪,仅捕获解析树中的第一个语句。

有没有人尝试使用 MGrammar 实现 If 语句的解析或有任何建议?

I am building up a MGrammar spec to parse some pseudo code looking for particular bits of information. I have most of the spec working except for 1 cruical element.

The pseudo code supports an if-then-else syntax and I have been unable to find a satisfactory way of parsing it. The exact construct is...

IF expression operator expression THEN
Statement1
Statement2
Statementn
ELSEIF expression operator expression THEN
Statement1
Statement2
Statementn
ELSE
Statement1
Statement2
Statementn
ENDIF

...Where the Else and Elseif are optional.

What I have so far is:
`syntax Statement = r:ReturnClause => r |
i:IfClause => i |
ei:ElseifClause => ei |
e:ElseClause => e |
end:EndClause => end |
v:Expression => v ;

syntax IfClause = If name:Identifier operator:Operator Then statement:Statement => If[name, operator, Then[statement]];
syntax ElseifClause = Elseif name:Identifier operator:Operator Then statement:Statement => ElseIf[name, operator, Then[statement]];
syntax ElseClause = Else statement:Statement => Else[statement];
syntax EndClause = Endif; `

However, the Statement after the 'Then' and 'Else' is not greedy enough and only captures the first statement in the parse tree.

Has anyone tried to implement the parsing of an If statement using MGrammar or have any suggestions??

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

指尖上的星空 2024-08-11 19:44:24

您可以在 codeplex 上的原型项目中找到(几乎)完整的 C# 4.0 语法。 Dan Vanderboom 编写此代码是为了准备他名为“Archetype”的新语言代码。

也许这有帮助: http://archetype.codeplex.com/

You can find a (almost) complete C# 4.0 grammar in the archetype project on codeplex. Dan Vanderboom wrote it in preparation of his new language code named "Archetype".

Maybe that helps: http://archetype.codeplex.com/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文