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