在FSYACC中,通常会有导致元组的终端。但是,为了方便起见,我想使用记录类型。例如,如果在抽象语法树(AbstractSyntaxTree.fsl)中有以下内容:
namespace FS
module AbstractSyntaxTree =
type B = { x : int; y : int }
type Either =
| Record of B
| Tuple of int * string
type A =
| Int of int
| String of string
| IntTuple of Either我不清楚FSYACC (parser.fsy)中正确的语法,因为如果我使用:
%start a
%token <string> STRING
%token <System.Int32> INT
%token ATOMTOKEN TUPLETOKEN EOF
%type < A > a
%%
a:
| atomS { $1 }
| atomI { $1 }
| either { $1 }
atomI:
| ATOMTOKEN INT { Int($2) }
atomS:
| ATOMTOKEN STRING { String($2) }
either:
| TUPLETOKEN INT INT { Record {x=$2;y=$3} } // !!!
| TUPLETOKEN TUPLETOKEN INT STRING { Tuple( $3, $4) } // !!!我希望可以推断出B类型和元组。但是,FSYACC给出了标记为“!”的两行的错误:
This expression was expected to have type A but here has type Either对于最后两行的“任一”产品,正确的语法是什么?
发布于 2015-04-16 14:00:07
你不是指IntTuple($2, $3)而不是B($2, $3)吗?我想试试IntTuple{x=$2; y=$3}
编辑:工作:
module Ast
type B = { x : int; y : int }
type A =
| Int of int
| String of string
| IntTuple of B和
%{
open Ast
%}
%start a
%token <string> STRING
%token <System.Int32> INT
%token ATOMTOKEN TUPLETOKEN
%type < Ast.A > a
%%
a:
| atom { $1 }
| tuple { $1 }
atom:
| ATOMTOKEN INT { Int($2) }
| ATOMTOKEN STRING { String($2) }
tuple:
| TUPLETOKEN INT INT { IntTuple {x = $2; y = $3} }编辑2:非常小心,行%type < Ast.A > a要求您的非终端a为Ast.A类型。因此,由于您直接使用非终端tuple,所以tuple需要类型为Ast.A。因此,您必须用IntTuple包装记录,所以语法是IntTuple {x = $2; y = $3},而不是{x = $2; y = $3}。
https://stackoverflow.com/questions/29674841
复制相似问题