-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrammar
119 lines (74 loc) · 3.06 KB
/
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
Grammar
-------
PROGRAM -> STMT_LIST
VAR_DECL -> ‘def‘ ASSIGN_LIST
ASSIGN_LIST -> ASSIGN_EXPR_LIST | VAR_NAME ‘,’ ASSIGN_LIST | VAR_NAME
ASSIGN_EXPR_LIST -> ASSIGN_EXPR ‘,’ ASSIGN_LIST | ASSIGN_EXPR
ASSIGN_EXPR -> CONTAINS_VALUE ASSIGN_OPER YIELDS_VALUE
CONTAINS_VALUE -> VAR_NAME | INDEXED_VAR_NAME
LIST_LITERAL -> ‘[’ LIST_ITEMS ‘]’
LIST_ITEMS -> LIST_ITEMS ‘,’ YIELDS_VALUE | YIELDS_VALUE | e
HASH_LITERAL -> ‘{’ HASH_ITEMS ‘}’
HASH_ITEMS -> HASH_ITEMS ‘,’ HASH_ITEM | HASH_ITEM | e
HASH_ITEM -> OPERATION ‘:‘ YIELDS_VALUE | OPERAND ‘:‘ YIELDS_VALUE
YIELDS_VALUE -> OPERATION | OPERAND | LAMBDA | COND_LIST
ASSIGN_OPER -> ‘=’ | ‘+=‘ | ‘-=‘ | ‘/=‘ | ‘%=‘ | '*='
IF_ELSE -> ‘if’ COND_LIST ‘{’ STMT_LIST ‘} else’ IF_ELSE
| ‘if’ COND_LIST ‘{’ STMT_LIST ‘} else’ STMT_LIST
| ‘if’ COND_LIST ‘{’ STMT_LIST ‘}’
COND_STMT -> ‘(’ COND_LIST ‘)’
COND_LIST -> COND_EXPR
| COND_STMT
| COND_EXPR LOGIC_OPER COND_LIST
| COND_STMT LOGIC_OPER COND_LIST
| ‘!’ COND_EXPR
| ‘!’ COND_STMT
| ‘!’ COND_EXPR LOGIC_OPER COND_LIST
| ‘!’ COND_STMT LOGIC_OPER COND_LIST
LOGIC_OPER -> ‘and’ | ‘or’ | ‘&&’ | ‘||’
COND_EXPR -> OPERAND COND_OPER OPERAND | OPERAND
COND_OPER -> ‘<’ | ‘<=’ | ‘==’ | ‘>’ | ‘>=’ | ‘!=’
STMT_LIST -> STATEMENT STMT_LIST | e
STATEMENT -> VAR_DECL
| ASSIGN_EXPR
| IF_ELSE
| FOR_LOOP
| WHILE_LOOP
| FUNC_CALL
| RETURN_STMT
| FUNC_DECL
| e
DECL_OR_ASSIGN = 'def' ASSIGN_EXPR_LIST | ASSIGN_EXPR_LIST
FOR_LOOP -> ‘for( ’ DECL_OR_ASSIGN ‘;’ COND_LIST ‘;’ VAR_NAME ASSIGN_OPER OPERATION ‘){’ STMT_LIST ‘}’
WHILE_LOOP -> ‘while’ COND_LIST ‘{’ STMT_LIST ‘}’
FUNC_DECL -> ‘def‘ VAR_NAME ‘(‘ FORMAL_PARAM_LIST ‘){’ STMT_LIST ‘}’
| ‘def‘ VAR_NAME ‘{’ STMT_LIST ‘}’
INVOKABLE -> CONTAINS_VALUE | FUNC_CALL | LAMBDA
FUNC_CALL -> INVOKABLE ‘(’ ACTUAL_PARAM_LIST ‘)’
FORMAL_PARAM_LIST -> FORMAL_PARAM ‘,’ FORMAL_PARAM_LIST | FORMAL_PARAM
FORMAL_PARAM -> VAR_NAME | e
RETURN_STMT -> ‘return’ YIELDS_VALUE | ‘return’
ACTUAL_PARAM_LIST -> ACTUAL_PARAM ‘,’ ACTUAL_PARAM_LIST | ACTUAL_PARAM
ACTUAL_PARAM -> YIELDS_VALUE | e
VAR_NAME -> LETTER | LETTER VAR_NAME
INDEXED_VAR_NAME -> VAR_NAME ‘[’ YIELDS_VALUE ']'
OPERATION -> OPERAND OPERATOR OPERATION
| ‘(‘ OPERATION ‘)’ OPERATOR OPERATION
OPERAND -> VALUE
| VAR_NAME
| STRING
| FUNC_CALL
| LIST_LITERAL
| HASH_LITERAL
| INDEXED_VAR_NAME
LAMBDA -> ‘lambda‘ ‘(‘ FORMAL_PARAM_LIST ‘){’ STMT_LIST ‘}’
| ‘lambda‘ ‘{’ STMT_LIST ‘}’
Regular Expressions
-------------------
LETTER = ( ‘a‘..‘z‘ + ‘_’ )
DIGIT = ( ‘0’..‘9’ )
OPERATOR = ( ‘+’ + ‘-‘ + ‘*’ + ‘/’ + ‘%’)
INTEGER = (‘+’ + ‘-‘ + e ) DIGIT+
FLOAT = (‘+’ + ‘-‘ + e ) DIGIT+ ( ( ‘.’ DIGIT+) + e ) ( (‘E’ (‘+’ + ‘-‘ +
e ) DIGIT+) + e)
STRING = ‘ ” ’ any unicode character(s) ‘ “ ’