ocamlyacc上的错误表达类型

时间:2014-12-20 18:59:22

标签: compilation ocaml parse-tree ocamllex ocamlyacc

作为学校项目的一部分,我必须识别.dot文件并生成相应的解析树。要做到这一点,我必须使用ocamllex和ocamlyacc,我很难......

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

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);;

这是我的词法分析器文件:

{
    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文件:

%{
    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)]}

似乎足以识别像

这样的简单点文件
graph D {
    A -- B ;
}

但是当我尝试编译我的解析器接口时,我收到此错误: 该表达式具有类型&#39;列表        但是期望类型为Types.graph的表达式(指ID EDGE ID SEMICOLON {[$1,$3)]}行)

我不明白,因为{[$ 1,$ 3]}有一个(字符串*字符串)列表类型。如果我们正在寻找可以成为图形的types.mli。

否则,我是否正确理解ocamllex和ocamlyacc的运行?

1 个答案:

答案 0 :(得分:1)

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