-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgrammar.txt
35 lines (33 loc) · 1.54 KB
/
grammar.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
# All grammar rules of Nubb++, where ::= means "defined as".
# E.g. a program is defined as zero or more statements, or program::={statement}
# Grammar breakdown:
- {} ::= zero or more of whatever is in brackets
- [] ::= zero or one of whatever is in brackets
- + ::= one or more of whatever is to the left of the '+'
- () ::= just for grouping
- | ::= Logical OR operator
- Keywords/operators are denoted as strings (e.g. "PRINT")
- Anything that isn't in quotation marks is either another grammar rule or token
program ::= {statement}
statement ::= "PRINT" (expression | string) nl
| "ELSE" nl {statement} "ENDIF" nl
| "ELIF" comparison "THEN" nl {statement} "ENDIF" nl
| "IF" comparison "THEN" nl {statement} "ENDIF" nl
| "FOR" ident ":" comparison ":" expression "THEN" nl {statement} "ENDFOR" nl
| "WHILE" comparison "REPEAT" nl {statement} "ENDWHILE" nl
| "LABEL" ident nl
| "GOTO" ident nl
| "LET" type ident "=" expression nl
| "CAST" ident ":" type nl
| "INPUT" (type ident | ident) nl
| "ADD" array ":" expression nl
| "POP" array nl
| "FUNCTION" ["VOID"] ident ":" nl {statement} ["RETURN"] ident | expression "ENDFUNCTION" nl
| "CALL" ident nl
type ::= "int" | "float" | "double" | "string" | "bool" | "array"
comparison ::= ["NOT"] expression {("++" | "--") | ("==" | "!=" | ">" | ">=" | "<" | "<=") expression}
expression ::= term {( "-" | "+" | "-=" | "+-" ) term} | term ( "++" | "--")
term ::= unary {( "/" | "*" ) unary}
unary ::= ["+" | "-"] primary
primary ::= number | ident | bool
nl ::= '\n'+