MGrammar语法和变量声明

发布于 2024-07-15 20:12:05 字数 637 浏览 10 评论 0原文

我确信我会被告知以另一种方式进行,但由于特定原因必须以这种方式进行。 如果没有,我就不会被卡住:-P

我正在开发的脚本语言必须接受这样定义的变量:

Variables: x(1), y("hi"); 

这是一个要求。 我之前写了一个小语法,可以让我像这样定义它们,

int x = 1;
string y = "hi"; 

但要求发生了变化。 在我的语法之前,这个

syntax sDeclareVar = t:sType i:tID "=" x:sExpression ";"  => DeclareVar { VariableName{i},Type{t},Value{x}};

sType 的值要么是 System.String、Int32 等,然后是变量名,然后是表达式。 然后我将其投影到 DeclareVar 节点中并为其提供所需的参数,在代码中我将其解析为 XML,然后让 MGrammar 解析 XML 并很好地遍历了我的 AST。 由于他们希望能够在不声明类型的情况下处理变量,所以我有点坚持现在要做什么,即如何将没有声明类型的变量存储到适当的类中。 任何帮助将不胜感激,希望这一切都有意义。

I'm sure I'll get told to do it another way, but for specific reasons it has to be done this way. If it didn't, I wouldn't be stuck :-P

The scripting language I'm working on has to accept variables defined like this:

Variables: x(1), y("hi"); 

This is a requirement. I wrote a small grammar before that would let me define them like this

int x = 1;
string y = "hi"; 

but the requirements changed. The way it was before my grammar looked like this

syntax sDeclareVar = t:sType i:tID "=" x:sExpression ";"  => DeclareVar { VariableName{i},Type{t},Value{x}};

sType's values were either System.String, Int32, etc., then the variable name, and then whatever the expression was. I then projected this into a DeclareVar node and gave it the parameters required, in the code I parsed it to XML and then had MGrammar parse the XML and traversed my AST just fine. Since they want to be able to do variables without declaring the type, I'm kind of stuck on what to do now, i.e. how do I get my variables that don't have a declared type stored into the appropriate classes. Any help would be appreciated, hopefully it all makes sense.

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

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

发布评论

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

评论(2

凡间太子 2024-07-22 20:12:05

这取决于你的语法的其余部分的结构,但你可能会被困在做一些大致如下的事情:

syntax sDeclareVar = "Variables:" sVarList ":";
syntax sVarList = sVarDeclaration ("," sVarList)?;
syntax sVarDeclaration = sIntVarDeclaration | sStringVarDeclaration | ...
syntax sIntVarDeclaration = i:tID "(" x:sIntegerLiteral ")"  => DeclareVar VariableName{i},Type{Int32},Value{x}};
syntax sStringVarDeclaration = i:tID "(" x:sStringLiteral ")"  => DeclareVar VariableName{i},Type{System.String},Value{x}};

等等。

It depends on how the rest of your grammar is structured, but you might be stuck doing something roughly along the lines of:

syntax sDeclareVar = "Variables:" sVarList ":";
syntax sVarList = sVarDeclaration ("," sVarList)?;
syntax sVarDeclaration = sIntVarDeclaration | sStringVarDeclaration | ...
syntax sIntVarDeclaration = i:tID "(" x:sIntegerLiteral ")"  => DeclareVar VariableName{i},Type{Int32},Value{x}};
syntax sStringVarDeclaration = i:tID "(" x:sStringLiteral ")"  => DeclareVar VariableName{i},Type{System.String},Value{x}};

and so forth.

如果没有你 2024-07-22 20:12:05

谢谢马库斯,这让我走上了正轨,这就是我最终要做的。

语法 sDeclareVar = tVariableKeywords s:Common.List(sVarDeclaration) ";" => 变量列表{语句{s}};

语法 sVarDeclaration = s:sIntVarDeclaration => s
| s:sStringVarDeclaration =>; s;

语法 sIntVarDeclaration = ","? i:tID "(" x:tIntegerLiteral ")" => DeclareVar{VariableName{i}, Type{Type{RawValue{"System.Int32"}}}, Value{IntegerLiteral{RawValue{x}}}};

语法 sStringVarDeclaration = ","? i:tID '(' x:tStringLiteral ')' => DeclareVar{VariableName{i}, Type{Type{RawValue{"System.String"}}}, Value{StringLiteral{RawValue{x}}}};

与上面的内容非常接近,但将变量存储在列表中更容易,而且我还必须添加一些投影以从我不需要的图中获取一些节点。 谢谢您的帮助。

Thanks Markus, that got me on the right track, here's what I ended up doing.

syntax sDeclareVar = tVariableKeywords s:Common.List(sVarDeclaration) ";" => VariableList{Statements{s}};

syntax sVarDeclaration = s:sIntVarDeclaration => s
| s:sStringVarDeclaration => s;

syntax sIntVarDeclaration = ","? i:tID "(" x:tIntegerLiteral ")" => DeclareVar{VariableName{i}, Type{Type{RawValue{"System.Int32"}}}, Value{IntegerLiteral{RawValue{x}}}};

syntax sStringVarDeclaration = ","? i:tID '(' x:tStringLiteral ')' => DeclareVar{VariableName{i}, Type{Type{RawValue{"System.String"}}}, Value{StringLiteral{RawValue{x}}}};

So close to what you had above, it was easier to store the variables in a list though, and I also had to add some projections to get some of the nodes out of the graph I didn't need. Thanks for the help.

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