-
Notifications
You must be signed in to change notification settings - Fork 0
/
gramatica.bnf
48 lines (37 loc) · 1.48 KB
/
gramatica.bnf
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
# Definição da Gramática
# Lexer
# É de realçar que a gramática do Natrix foi alterada durante a implementação
# Sendo assim este ficheiro pode não ser 100% fiel
<digit> ::= ['0'-'9']
<char> ::= ['a-zA-Z']
<integer> ::= <digit>+
<id> ::= ('_' | <char>)(<char> | <digit>)*
<newline> ::= '\n'
<whitespace> ::= '\t' | ' '
<binop> ::= '+' | '-' | '/' | '*'
<booleanbop> ::= '>' | '>=' | '==' | '!=' | '<' | '<='
# Parser
<program> ::= <statements>
<statements> ::= (<statement> | <function_def>)*
<type> ::= int | <id>
<statement> ::= <statement>*
| 'if' '(' <boolean_expression> ')'
'{' <statement> '}'
'else'
'{' <statement> '}'
| 'foreach' <id> 'in' [<integer> '..' <integer>] '{' <statement> '}'
| 'print' '(' <expression> ')' ';'
| <id> ':=' <expression> ';'
| <id> '[' <expression> ']' ':=' <expression> ';'
| 'val' <id> ':' <type> '=' <expression> ';'
| 'type' <id> '=' '[' <integer> '..' <integer> ']' ';'
<expression> ::= <lparen> <expression> <rparen>
| <expression> <binop> <expression>
| <expression> <lbracket> <expression> <rbracket>
| <integer>
| <id>
<boolean_expression> ::= '('<expression> <booleanop> <expression> ')'
| <expression> <booleanop> <expression>
| '!' <boolean_expression>
<argument_list> ::= (<id> ':' <type>)*
<function_def> ::= 'function' <id> '(' <argument_list> ')' ':' <type> '{' <statement> 'return' <integer> ';' '}'