-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.mly
More file actions
71 lines (67 loc) · 1.77 KB
/
Copy pathparser.mly
File metadata and controls
71 lines (67 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
%{
open PrologInterpreter
let rec unitList_to_termList unitList = match unitList with
| [] -> []
| (a,b)::u1 -> Node(a,b)::(unitList_to_termList u1)
let rec unitList_to_atomList unitList = match unitList with
| [] -> []
| ("!",[])::u1 -> Cut::(unitList_to_atomList u1)
| ("fail",[])::u1 -> Fail::(unitList_to_atomList u1)
| (a,b)::u1 -> Atom(a,b)::(unitList_to_atomList u1)
%}
%token <int> INT
%token T F
%token CUT FAIL
%token PLUS MINUS TIMES DIV MOD EXP
%token LPAREN RPAREN
%token PERIOD COMMA IF
%token <string> ID
%token <string> VAR
%token EQ GRT LST GRE LTE
%token LBRACKET RBRACKET BAR
%token EOL EOF
%start program
%start goal
%type <PrologInterpreter.program> program
%type <(PrologInterpreter.goal)> goal
%%
program:
expr { $1 }
;
expr:
| clause expr { $1::$2 }
| EOL expr { $2 }
| EOF { [] }
;
clause:
| fact { $1 }
| rule { $1 }
;
fact:
| unit PERIOD { match $1 with | (a,b) -> Fact(Atom(a,b)) }
;
rule:
| unit IF unitList PERIOD { match $1 with | (a,b) -> Rule(Atom(a,b), unitList_to_atomList $3) }
;
unit:
| ID termList { ($1,$2) }
| CUT { ("!",[]) }
| FAIL { ("fail",[]) }
;
unitList:
| unit { [$1] }
| unit COMMA unitList { $1::$3 }
;
unit_var_list:
| ID { [Node($1,[])] }
| VAR { [Var($1)] }
| ID COMMA unitList { Node($1,[])::(unitList_to_termList $3) }
| VAR COMMA unitList { Var($1)::(unitList_to_termList $3) }
| ID COMMA unit_var_list { Node($1,[])::$3 }
| VAR COMMA unit_var_list { Var($1)::$3 }
termList:
| LPAREN unitList RPAREN { unitList_to_termList $2 }
| LPAREN unit_var_list RPAREN { $2 }
;
goal:
unitList PERIOD { Goal (unitList_to_atomList $1) }