-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCopperHead.grammar
79 lines (60 loc) · 1.5 KB
/
CopperHead.grammar
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
%header%
GRAMMARTYPE = "LL"
%tokens%
WHITESPACE = <<[ \t\n\r]+>> %ignore% //ignore tag to discard such tokens
STRING = <<\'.*?\'>>
INTEGER = <<([-|+]?[0-9]+)>> //includes shorts and bytes
//value literals
TRUE = "true"
FALSE = "false"
//control flow keywords
WHILE = "while"
IF = "if"
//built in funcs with 1 argument
//ADD1 = "add1"
//SUB1 = "sub1"
//ISNUM = "isNum"
//ISBOOL = "isBool"
//built-in type names
INT = "int"
STR = "string"
BOOL = "bool"
//other keywords
LET = "let"
DEF = "def"
SET = "set"
VOID = "void"
NAME = <<[a-zA-Z][a-zA-Z0-9_]*>> //reduce spaces between "<<" and ">>"
//operators
PLUS = "+"
MINUS = "-"
MULT = "*"
LESS = "<"
GREAT = ">"
EQUAL = "="
NOT_EQ = "!="
GR_EQ = ">="
LS_EQ = "<="
COLON = ":"
EXPONENT = "^"
OP_PAREN = "("
CL_PAREN = ")"
%productions%
program = (expr+);
expr = (atom
| (OP_PAREN funcDef CL_PAREN)
| (OP_PAREN letDec CL_PAREN)
| (OP_PAREN ifExpr CL_PAREN)
| (OP_PAREN binOp CL_PAREN)
| (OP_PAREN setVar CL_PAREN)
| (OP_PAREN funcCall CL_PAREN)
| (OP_PAREN whileLoop CL_PAREN));
funcDef = DEF NAME ((OP_PAREN (NAME COLON type) CL_PAREN)+ | VOID) COLON (type | VOID) (expr)+ ;
letDec = LET OP_PAREN (OP_PAREN (NAME COLON type) expr CL_PAREN)+ CL_PAREN (expr)+ ;
ifExpr = IF expr expr expr;
binOp = (PLUS | MINUS | MULT | LESS | GREAT | EQUAL) expr expr;
setVar = SET NAME expr;
funcCall = NAME (expr)*;
whileLoop = WHILE expr (expr)*;
type = (INT | BOOL | STR);
atom = ( ( [MINUS] INTEGER) | FALSE | TRUE | STRING | NAME);