首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ocamlyacc上的表达式类型错误

ocamlyacc上的表达式类型错误
EN

Stack Overflow用户
提问于 2014-12-21 02:59:22
回答 1查看 190关注 0票数 1

这是我的ocaml .mli类型文件:

代码语言:javascript
复制
type id = string 
and node = id
and attr = id * id
and attr_list = attr list list
and attr_stmt =
    Graphs of (attr_list)
    | Nodes of (attr_list)
    | Edge of (attr_list)
and node_stmt = node * attr_list
and node_subgraph = 
    Node of (node)
    | Subgraphs of (graph)  
and edge_stmt = node_subgraph * (node_subgraph list) * attr_list
and stmt = 
    Node_stmt of (node_stmt)
    | Edge_stmt of (edge_stmt)
    | Attr_stmt of (attr_stmt)
    | Attr of (attr)
    | Subgraph of  graph
and graph = 
    Graph_node of (node * stmt list)
    | Graph of (stmt list);;

这是我的lexer文件:

代码语言:javascript
复制
{
    open Parser
}
rule token = parse 
    (* Espacements *)
    | [ ' ' '\t' '\n']+  {token lexbuf}
    (* *)
    | "graph" {GRAPH}
    | "subgraph" {SUBGRAPH}
    | "--" {EDGE}
    (* Délimiteurs *)
    | "{" {LEFT_ACC}
    | "}" {RIGHT_ACC}
    | "(" {LEFT_PAR}
    | ")" {RIGHT_PAR}
    | "[" {LEFT_BRA}
    | "]" {RIGHT_BRA} 
    | "," {COMMA}
    | ";" {SEMICOLON}
    | "=" {EQUAL}
    | ":" {TWOPOINT}
    | ['a'-'z''A'-'Z''0'-'9']*  as id {ID (id)} 
    | eof { raise End_of_file }

这是我未完成的yacc文件:

代码语言:javascript
复制
%{
    open Types
%}

%token <string> ID 
%token <string> STR
%token GRAPH SUBGRAPH EDGE
%token LEFT_ACC RIGHT_ACC LEFT_PAR RIGHT_PAR LEFT_BRA RIGHT_BRA
%token COMME SEMICOLON EQUAL TWOPOINT EOF 

%start main
%type <graph> main 

%%

main:
 graph EOF { $1 }


graph:
    GRAPH ID LEFT_ACC content RIGHT_ACC {$4}
    | GRAPH LEFT_ACC content RIGHT_ACC {$3}

subgraph:
    SUBGRAPH ID LEFT_ACC content RIGHT_ACC {$4}
    | SUBGRAPH LEFT_ACC content RIGHT_ACC {$3}

content:
    | ID EDGE ID SEMICOLON {[($1,$3)]}

它似乎足以识别一个简单的点文件,比如

代码语言:javascript
复制
graph D {
    A -- B ;
}

但是当我试图编译我的解析器接口时,我得到了这个错误:这个表达式的类型是'a list,但表达式的类型应该是Types.graph (引用ID EDGE ID SEMICOLON {[$1,$3)]}行)。

如果我们正在寻找types.mli,那可以是一张图。

EN

回答 1

Stack Overflow用户

发布于 2014-12-21 04:05:49

graph类型的值必须以GraphGraph_node开头。这与(string * string) list完全不同。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27583355

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档