我想處理dypgen中的一些歧義。我在手冊中發現了一些內容,我想知道,我該如何使用它。 在手動點5.2「上的符號模式匹配」有一個例如:dypgen中的模式匹配
expr:
| expr OP<"+"> expr { $1 + $2 }
| expr OP<"*"> expr { $1 * $2 }
OP與「+」或匹配的「*」,我的理解。我也在那裏找到:
這些模式可以是任何Caml模式(但沒有關鍵字時)。 例如這是可能的:
expr: expr<(Function([arg1;arg2],f_body)) as f> expr { some action }
於是,我就放在那裏一些其他的表情,但我不明白,發生了什麼。如果我放在那裏printf
它輸出匹配的字符串的值。但是,如果我在那裏放入(fun x -> printf x)
,那在我看來就像printf
一樣,dypgen會抱怨語法錯誤並指向表達式的結尾。如果我把Printf.printf
放在那裏,它會抱怨Syntax error: operator expected
。如果我把它放在那裏(fun x -> Printf.printf x)
它說:Lexing failed with message: lexing: empty token
這些不同的錯誤消息是什麼意思?
最後,我想查找一個哈希表中的東西,如果值在那裏,但我不知道,如果這是可能的話。它是不是可能?
編輯:派生自森林示例從dypgen演示的最小示例。
的grammarfile forest_parser.dyp包含:
{
open Parse_tree
let dyp_merge = Dyp.keep_all
}
%start main
%layout [' ' '\t']
%%
main : np "." "\n" { $1 }
np:
| sg {Noun($1)}
| pl {Noun($1)}
sg: word <Word("sheep"|"fish")> {Sg($1)}
sg: word <Word("cat"|"dog")> {Sg($1)}
pl: word <Word("sheep"|"fish")> {Pl($1)}
pl: word <Word("cats"|"dogs")> {Pl($1)}
/* OR try:
sg: word <printf> {Sg($1)}
pl: word <printf> {Pl($1)}
*/
word:
| (['A'-'Z' 'a'-'z']+) {Word($1)}
的forest.ml現已以下print_forest功能:
let print_forest forest =
let rec aux1 t = match t with
| Word x
-> print_string x
| Noun (x) -> (
print_string "N [";
aux1 x;
print_string " ]")
| Sg (x) -> (
print_string "Sg [";
aux1 x;
print_string " ]")
| Pl (x) -> (
print_string "Pl [";
aux1 x;
print_string " ]")
in
let aux2 t = aux1 t; print_newline() in
List.iter aux2 forest;
print_newline()
而且parser_tree.mli包含:
type tree =
| Word of string
| Noun of tree
| Sg of tree
| Pl of tree
然後你可以確定,什麼數字魚,羊,貓等。
sheep or fish can be singular and plural. cats and dogs cannot.
fish.
N [Sg [fish ] ]
N [Pl [fish ] ]
你如何解析你的函數? – Lhooq
我用dypgen的演示爲出發點,並使用這些makefile文件...語法是在.dyp-文件和它的作用: .dyp.ml: \t path_to_dypgen $ < \t ocamlc path_to_dyplib -C $ *。mli 我想現在,我被該構造函數的那個例子的模式語法困惑了。其他具有類型構造函數的ocaml模式可以工作。 (當然,printf不是一個模式,也許是它構建到dypgen中)但是我從來沒有在手冊中看到過類似的構造函數。 – gwf
你可以添加一個你做過的最簡單的例子嗎? – Lhooq