-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgramatica.atg
More file actions
122 lines (90 loc) · 3.63 KB
/
Copy pathgramatica.atg
File metadata and controls
122 lines (90 loc) · 3.63 KB
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
120
121
122
#include <stdlib.h>
#include <iostream>
#include "../tree.h"
using namespace std;
COMPILER Block
string * _to_reg_(int r){
//return new string("R"+to_string(r));
switch(r){
case 1:{
return new string("rax");
}
case 2:{
return new string("rbx");
}
case 3:{
return new string("rcx");
}
case 4:{
return new string("rdx");
}
}
}
void LexString(string *lex){
string * ft = new string("");
ft->append(string(coco_string_create_char(t->val)));
//cout<<" ft "<<ft->c_str()<<endl;
lex->append(*ft);
}
CHARACTERS
tab = '\u0009'. /* 9 = tabulator */
lf = '\u000a'. /* 10 = line feed */
cr = '\u000d'. /* 13 = carriage return */
whiteSpace = ' ' + '\t' + '\r' +'\n'.
digit = "0123456789" .
letter = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" .
TOKENS
number = digit { digit } .
variable = letter {letter}.
COMMENTS FROM "/*" TO "*/" NESTED
COMMENTS FROM "//" TO "\n"
IGNORE whiteSpace
PRODUCTIONS
Block (. AST BS = CreateStatementBlock(); AST E; .) = "{" {Statement<E> (. StatementBlockAdd(BS,E); .)} "}" (.GenerateCode(BS);.) .
Statement<AST &E> (. printf("\n"); AST T; string * CH= new string("s"); .)
= Identifier<CH> ("=" Expression<T> (.
E = AssignmentNode(VarNode(CH),T);
.)
| "("")" (. cout<<"CALL "<<*CH<<endl; .)
) ";"
| "print" "(" Expression<E> ["," Expression<T>] ")" (. cout<<"CALL print"<<endl; .) ";"
| Block (. cout<<"bloc"<<endl;.)
| Expression<E> (. cout<<"expr"<<endl;.)
| "return" Expression<E> (. cout<<"expr"<<endl;.)
| FuncDecl<E>
| VarDecl<E>
| ";" .
FuncDecl<AST &E> (. string * name = new string(""); .)
= "func" Identifier<name> (. E = FuncDeclNode(name); .) '(' ')'
"{" { (. AST S; .) Statement<S> (. StatementBlockAdd(E,S); .)} "}" .
Type (. string *type = new string(""); .) = Identifier<type>.
VarDecl<AST &E> (.string *type = new string("");.)
= Type Identifier<type> {"," Identifier<type>} ";".
Expression<AST &E>
= (. AST T; .)
Term<E>
{ "+" Term<T> (. E = BinOpNode(Plus, E, T); .)
| "-" Term<T> (. E = BinOpNode(Minus, E, T); .)
} .
Term<AST &T>
= (. AST F; .)
Factor<T>
{ "*" Factor<F> (. T = BinOpNode(Times, T, F); .)
| "/" Factor<F> (. T = BinOpNode(Slash, T, F); .)
} .
Factor<AST &F>
= (. string * CH = new string(""); int N=0; F = EmptyNode(); .)
(..)
( Identifier<CH> (. F = VarNode(CH); .)
| Number<N> (. F = ConstNode(N); .)
| "(" Expression<F> ")"
) .
Identifier<string * CH> (. .)
= variable (. string * str = new string("");
LexString(str);
*CH= *str ; .) .
Number<int &N> (. .)
= number (.string * str = new string("");
LexString(str);
N = atoi(str->c_str()); .) .
END Block.