-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgrammar.txt
78 lines (68 loc) · 2.25 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
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
<program> ::= <def> <program> | <def>
<def> ::= <var-def> ';'
| 'void' <id> '(' (<formal-params>)? ')' <block>
| <type> <id> '(' (<formal-params>)? ')' <block>
<var-def> ::= <type> <id> (':=' <expr>)?
<type> ::= <simple-type> | <simple-type> '[' ']'
<simple-type> ::= 'bool' | 'int' | 'char'
<formal-params> ::= <type> <id> (',' <type> <id>)*
<actual-params> ::= <expr> (',' <expr>)*
<block> ::= '{' (<stmt-list>)? '}'
<stmt-list> ::= <stmt> | <stmt> <stmt-list>
<stmt> ::= <var-def>
| 'if' '(' <expr> ')' <stmt> ('else' <stmt>)?
| 'for' '(' <var-def> ';' <expr> ';' <expr> ')' <stmt>
| 'while' '(' <expr> ')' <stmt>
| <expr> ';'
| <block>
| 'delete' expr ';'
| 'return' (<expr>)? ';'
<expr> ::= <id>
| <expr> '[' expr ']'
| <id> '(' (<actual-params>)? ')'
| <expr> ':=' <expr> // we should check in semantics checks that lval is valid
| 'new' <simple-type> '[' <expr> ']'
| 'size' <expr> // Arrays will have their size builtin __size in lib
| <expr> <bin-op> <expr>
| <un-op> <expr>
| <int-literal> | <bool-literal> | <char-literal> | <string-literal>
<bin-op> ::= '+' | '-' | '*' | '/' | '%' | '==' | '!='
| '>' | '<' | '>=' | '<=" | '&&' | '||' | '^'
<un-op> ::= '!' | '-' | '+'
<int-literal> ::= [0-9]+
<char-literal> ::= as in C
<string-literal> ::= as in C
<bool-literal> ::= 'true' | 'false'
Standard Lib
============
char chr(int x);
int ord(char c);
char[] readLine();
void print(char[] str);
int string_to_int(char[] str);
char[] int_to_string(int x);
char[] strcpy(char[] str);
int strcmp(char[] str1, char[] str2);
char[] strcat(char[] str1, char[] str2);
int strlen(char[] str); // strlen(str) < __size(str)
Associativity
=============
%right ':='
%left '&&' '||'
%nonassoc '==' '!=' '>' '<' '<=' '>='
%left '+' '-'
%left '*' '/' '%'
%nonassoc SIGN '+'
%nonassoc SIGN '-'
%nonassoc '!'
%nonassoc size
TODO
====
* Check array-bounds (or maybe not? GOOD LUCK!)
* Null-terminated strings ("\0") for cc
Fuzzy things - Ask nickie
=========================
* No special care for Calling conv, right?
* Handle array size? Store size in {arr[], size} is enough?
Nickie: Store in Heap as first word.
* Stdlib is OK?