我为APL子集制作了语法。
grammar APL;
program: (statement NEWLINE)*;
statement: thing;
assignment: variable LARR thing;
thing: simpleThing
| complexThing;
escapedThing: simpleThing
| '(' complexThing ')';
simpleThing: variable # ThingVariable
| number # ThingNumber
;
complexThing: unary # ThingUOp
| binary # ThingBOp
| assignment # ThingAssignment
;
variable: CAPITAL;
number: DIGITS;
unary: iota # UOpIota
| negate # UOpNegate
;
iota: SMALL_IOTA number;
negate: TILDA thing;
binary: drop # BOpDrop
| select # BOpSelect
| outerProduct # BOpOuterProduct
| setInclusion # BOpSetInclusion
;
drop: left=number SPIKE right=thing;
select: left=escapedThing SLASH right=thing;
outerProduct: left=escapedThing OUTER_PRODUCT_OP right=thing;
setInclusion: left=escapedThing '∊' right=thing;
NEWLINE: [\r\n]+;
CAPITAL: [A-Z];
CAPITALS: (CAPITAL)+;
DIGITS: [0-9]+;
TILDA: '~';
SLASH: '/';
// greek
SMALL_IOTA: 'ι' | '@i';
// arrows
LARR: '←' | '@<-';
SPIKE: '↓' | '@Iv';
OUTER_PRODUCT_OP: '∘.×' | '@o.@x';
现在我想为它创建一个解释器。我正在尝试将clj-antlr与Clojure一起使用。我该怎么做?
答案 0 :(得分:1)
正如Jared314指出的那样,看看instaparse:
这是你创建语法的方法:
(def as-and-bs
(insta/parser
"S = AB*
AB = A B
A = 'a'+
B = 'b'+"))
这就是你所说的:
(as-and-bs "aaaaabbbaaaabb")
以下是默认格式的结果:
[:S
[:AB [:A "a" "a" "a" "a" "a"] [:B "b" "b" "b"]]
[:AB [:A "a" "a" "a" "a"] [:B "b" "b"]]]
虽然ANTLR肯定做得很好,但在Clojure世界中你可以使用instaparse删除所有周围的粘合剂。