1

作为学校项目的一部分,我必须识别 .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 ;
}

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

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

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

4

1 回答 1

1

type 的值graph必须以Graphor开头Graph_node。这一点也不一样(string * string) list

于 2014-12-20T20:05:48.987 回答