如何处理 parser.mly 中的变量声明?
我正在用 Ocaml 编写 mini-pascal 编译器。例如,我希望我的编译器接受以下代码:
program test;
var
a,b : boolean;
n : integer;
begin
...
end.
我在处理变量声明(var
之后的部分)时遇到困难。目前,变量的类型在sib_syntax.ml中定义如下:
type s_var =
{ s_var_name: string;
s_var_type: s_type; }
这里是sib_parser.mly。我的问题是,我可以在哪里以及如何告诉编译器构建全局变量,即变量声明,它实际上是一个 s_var 列表。我想我需要改进 sib_parser.mly
(termerated_bindings
, binding
, separated_nonempty_list
末尾的 menhir 部分> 等),但我不知道如何...
有人可以帮忙吗?非常感谢!
I am writing a compiler of mini-pascal in Ocaml. I would like my compiler to accept the following code for instance:
program test;
var
a,b : boolean;
n : integer;
begin
...
end.
I have difficulties in dealing with the declaration of variables (the part following var
). At the moment, the type of variables is defined like this in sib_syntax.ml:
type s_var =
{ s_var_name: string;
s_var_type: s_type; }
Here is sib_parser.mly. My question is, where and how I could tell the compiler to build globals
, the declaration of variables, which is actually a list of s_var
. I guess I need to refine the part of menhir in the end of sib_parser.mly
(terminated_bindings
, binding
, separated_nonempty_list
, etc.), but I do not know how...
Could anyone help? Thank you very much!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从表面上看,在绑定规则中,您可以访问 ids ,它是变量名称列表,因此您可以编写,例如:
这将使 绑定> 规则返回一个
s_var 列表
。From the looks of it, in your binding rules, you have access to
ids
which is a list of variable names, so you could write, for instance:This would make the
binding
rule return as_var list
.