comparison tools/ncparse.mly @ 0:bfdcc3820b32

Basis
author Mike Spivey <mike@cs.ox.ac.uk>
date Thu, 05 Oct 2017 08:04:15 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:bfdcc3820b32
1 /* ncparse.mly */
2 /* Copyright (c) 2017 J. M. Spivey */
3
4 %token <string> WORD OPEN
5 %token <char> CHAR
6 %token LPAREN RPAREN CLOSE EOF COMMA SPACE ATSIGN
7
8 %type <unit> text
9 %start text
10
11 %nonassoc LOW
12 %nonassoc COMMA SPACE ATSIGN
13 %nonassoc HIGH
14
15 %{
16 open Print
17
18 let outch ch = print_char ch
19
20 let out s = print_string s
21
22 let out_frag f = printf "$" [f]
23
24 let fJoin f1 f2 = fExt (fun prf -> prf "$$" [f1; f2])
25 %}
26
27 %%
28
29 text :
30 prog EOF { () } ;
31
32 prog :
33 /* empty */ { () }
34 | prog WORD { out $2 }
35 | prog SPACE { out " " }
36 | prog CLOSE { out ">" }
37 | prog LPAREN { out "(" }
38 | prog RPAREN { out ")" }
39 | prog COMMA { out "," }
40 | prog ATSIGN { out "@" }
41 | prog CHAR { outch $2 }
42 | prog node { out_frag $2 } ;
43
44 node :
45 OPEN bal1 args CLOSE { fMeta "(Node ($$, $))" [fStr $1; $2; $3] }
46
47 bal1 :
48 bal %prec HIGH { $1 } ;
49
50 args :
51 /* empty */ { fStr "[]" }
52 | COMMA blank arglist { fMeta "[$]" [$3] }
53 | COMMA blank ATSIGN bal { $4 } ;
54
55 arglist :
56 bal %prec HIGH { $1 }
57 | arglist COMMA bal %prec HIGH { fMeta "$;$" [$1; $3] } ;
58
59 bal :
60 /* empty */ %prec LOW { fStr "" }
61 | bal item { fJoin $1 $2 }
62 | bal LPAREN bal RPAREN { fMeta "$($)" [$1; $3] } ;
63
64 item :
65 WORD { fStr $1 }
66 | CHAR { fChr $1 }
67 | SPACE { fStr " " }
68 | COMMA { fStr "," }
69 | ATSIGN { fStr "@" }
70 | node { $1 } ;
71
72 blank :
73 /* empty */ %prec LOW { () }
74 | SPACE { () } ;