-
Notifications
You must be signed in to change notification settings - Fork 0
/
grammer.txt
109 lines (85 loc) · 2.88 KB
/
grammer.txt
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
The english explanation for these rules is present in readme.
prog:
declStmt
| funDeclStmt
| declStmt prog
| funDeclStmt prog
funDeclStmt: funName incScope LPAREN declArg RPAREN LBRACE stmts RBRACE decScope
| funName incScope LPAREN RPAREN LBRACE stmts RBRACE decScope
declStmt: type identifiersList SEMICOLON
funName: type IDENTIFIER
type: INT
| CHAR
| VOID
| STRING
declArg: declArg COMMA type identifiers
| type identifiers
identifiersList: identifiers
| identifiersList COMMA identifiers
identifiers: IDENTIFIER
| IDENTIFIER multiDim
multiDim: multiDim LBRACKET NUM_CHAR RBRACKET
| LBRACKET NUM_CHAR RBRACKET
stmts: stmt
| stmts stmt
stmt:
| incScope LBRACE stmts RBRACE decScope
| controlflowStmt
| loopStmt
| ifelseStmt
| declStmt
| customFunc
| exprStmt
controlflowStmt: BREAK SEMICOLON
| CONTINUE SEMICOLON
| RETURN SEMICOLON
| RETURN expression SEMICOLON
loopStmt: FOR incScopeFor LPAREN exprStmt exprStmt expression RPAREN LBRACE stmts RBRACE decScope
| FOR incScopeFor LPAREN exprStmt exprStmt RPAREN LBRACE stmts RBRACE decScope
| WHILE incScopeFor LPAREN expression RPAREN LBRACE stmts RBRACE decScope
incScopeFor: %empty
incScope: %empty
decScope: %empty
ifelseStmt: IF LPAREN expression RPAREN incScope LBRACE stmts RBRACE decScope
| IF LPAREN expression RPAREN incScope LBRACE stmts RBRACE decScope ELSE stmt
| arithmeticExpr incScope TERNERY expression COLON expression decScope
customFunc: INPUT LPAREN type COMMA IDENTIFIER RPAREN SEMICOLON
| OUTPUT LPAREN type COMMA IDENTIFIER RPAREN SEMICOLON
| NEWLINE LPAREN RPAREN SEMICOLON
exprStmt: expression SEMICOLON
| SEMICOLON
expression : arithmeticExpr
| STRINGVAL
| unaryExpr ASSIGN expression
callArg: expression
| callArg COMMA expression
arithmeticExpr: unaryExpr
| arithmeticExpr MUL arithmeticExpr
| arithmeticExpr DIV arithmeticExpr
| arithmeticExpr MOD arithmeticExpr
| arithmeticExpr MINUS arithmeticExpr
| arithmeticExpr LE arithmeticExpr
| arithmeticExpr GE arithmeticExpr
| arithmeticExpr LT arithmeticExpr
| arithmeticExpr GT arithmeticExpr
| arithmeticExpr EQ arithmeticExpr
| arithmeticExpr NE arithmeticExpr
| arithmeticExpr BITAND arithmeticExpr
| arithmeticExpr XOR arithmeticExpr
| arithmeticExpr BITOR arithmeticExpr
| arithmeticExpr AND arithmeticExpr
| arithmeticExpr OR arithmeticExpr
unaryOperator: MINUS
| NOT
Unary expression can an identifier, number, string, expression within parantheses (scope), function call with no arguments, function call with arguments, unary expression with a unary operator, single dimensional array access using expression, multidimensional array access.
unaryExpr: IDENTIFIER
| NUM_CHAR
| STRINGVAL
| LPAREN expression RPAREN
| IDENTIFIER LPAREN RPAREN
| IDENTIFIER LPAREN callArg RPAREN
| unaryOperator unaryExpr
| IDENTIFIER LBRACKET expression RBRACKET
| IDENTIFIER multiAccess
multiAccess: multiAccess LBRACKET NUM_CHAR RBRACKET
| LBRACKET NUM_CHAR RBRACKET