1

I am a beginner in OCaml and trying to build a parser, I want to have a list that stores all the methods in my class. This is one part that I have in my .mly file.

init_method_list: 
    { [] }
    | method_list method_decl { List.rev($1) }
;

method_list:
    method_decl { [ $1 ] }
    | method_list method_decl { $2 :: $1 }
;

Can anyone explain exactly what's going on here? Especially the :: operation. Been googling around but couldn't find the operator in the docs.

I get that the list can be empty, or we make right recursive calls to fill it up with all the methods in class. method_decl just looks for the match of the specific token combinations that represents a method.

4

2 回答 2

4

正如我在评论中所说,运算符::用于将 type 的元素连接到 type'a的列表'a list。一个小例子:

1 :: [2;3]生成列表[1;2;3],所以是的,它将元素添加到列表的前面。

于 2015-09-11T13:45:42.880 回答
0

正如其他人所说, :: 将一个元素连接到相同类型的列表中。另外,如果您需要连接两个列表,可以使用 @ 符号,例如:

[1;2;3]@[4] 将给出列表 [1;2;3;4]

于 2016-06-07T12:11:23.933 回答