-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinc2json.lark
65 lines (36 loc) · 1.09 KB
/
inc2json.lark
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
start: statement*
statement: typedef | declare | define
typedef: "typedef" declare
define: "#" "define" NAME expr
?declare: (stdtype | alias | usertype) _C_TERM
?usertype: "struct" usertype_
?usertype_: NAME NAME declarrs -> usertyperef
| tag "{" declares "}" typename declarrs -> usertypedecl
tag: NAME?
declares: declare*
typename: NAME?
alias: NAME NAME declarrs
stdtype: builtins NAME declarrs
builtins: builtin*
!?builtin: "char" | "short" | "int" | "long" | "signed" | "unsigned" | "const" | "void" | "*"
declarrs: declarr*
declarr: "[" expr "]" -> id
// https://lark-parser.readthedocs.io/en/latest/examples/calc.html
expr: sum -> id
?sum: product
| sum "+" product -> add
| sum "-" product -> sub
?product: atom
| product "*" atom -> mul
| product "/" atom -> div
?atom: NUMBER -> atoi
| NAME -> var
| "(" expr ")" -> id
_C_TERM: ";"
PP_COMMENT: /#\s+\d+\s/ ESCAPED_STRING /(\s+\d+)*/
%import common.CNAME -> NAME
%import common(INT, NUMBER, WS, CPP_COMMENT, C_COMMENT, ESCAPED_STRING)
%ignore WS
%ignore PP_COMMENT
%ignore CPP_COMMENT
%ignore C_COMMENT