-
Notifications
You must be signed in to change notification settings - Fork 2
/
parser.rkt
40 lines (24 loc) · 1.41 KB
/
parser.rkt
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
#lang brag
;; A #lang brag program automagically converts a BNF grammar (below) into a function
;; that parses bytes from an input port into an S-expression that matches the grammar.
;; It provides this function under the name `parse`.
tabloid-program: statement+
@statement: print | variable-assign | function-def | value-expression
| conditional | block-scope | program-end
print: /"YOU WON'T WANT TO MISS" value-expression
variable-assign: /"EXPERTS CLAIM" IDENTIFIER /"TO BE" value-expression
value-expression: [value-expression ("PLUS" | "MINUS")]* (product | function-apply | block-scope)
product: [product ("TIMES" | "DIVIDED BY" | "MODULO")]* (@value | value-expression)
@value: NUMBER | STRING | boolean | IDENTIFIER | /"(" value-expression /")"
function-apply: APPLY-FUNC-ID value-expression [/"," value-expression]* /END-ARGLIST
@boolean: true | false
true: /"TOTALLY RIGHT"
false: /"COMPLETELY WRONG"
conditional: /"WHAT IF" compare (statement | shocking-return)
[/"LIES!" (statement | shocking-return)]
compare: value-expression ("IS ACTUALLY" | "BEATS" | "SMALLER THAN") value-expression
function-def: /"DISCOVER HOW TO" id-and-arguments statement
/id-and-arguments: IDENTIFIER /"WITH" [IDENTIFIER /","]* IDENTIFIER
block-scope: /"RUMOR HAS IT" [statement | shocking-return]+ /"END OF STORY"
shocking-return: /"SHOCKING DEVELOPMENT" value-expression
program-end: /"PLEASE LIKE AND SUBSCRIBE"