-
Notifications
You must be signed in to change notification settings - Fork 0
/
sdd.txt
66 lines (66 loc) · 4.18 KB
/
sdd.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
PROGRAM -> STMTS { program.code = STMTS.code } -------------------
STMTS -> STMT STMTS { STMTS.code = STMT.code || STMTS.code } -------------
STMTS -> '' { STMTS.code = '' } ----------------
STMT -> FUNC
STMT -> IF { STMT.code = IF.code }
STMT -> FOR
STMT -> WHILE { STMT.code = WHILE.code } ------------------------
STMT -> RETURN
STMT -> ASSIGN ; { STMT.code = ASSIGN.code }
STMT -> DEFN ; { STMT.code = DEFN.code // pop twice } -------------
STMT -> FUNC_CALL ; { STMT.code = FUNC_CALL.code }
DEFN -> TYPE DEFN_LIST { DEFN.code = DEFN_LIST.code } ---------------
DEFN_LIST -> ID_MAYBE_INIT , DEFN_LIST2 { DEFN_LIST.code = ID_MAYBE_INIT.code + '\n' + DEFN_LIST2.code } -----------
DEFN_LIST -> ID_MAYBE_INIT { DEFN_LIST.temp = ID_MAYBE_INIT.temp, DEFN_LIST.code = ID_MAYBE_INIT.code } ----------
ID_MAYBE_INIT -> ID { ID_MAYBE_INIT.temp = ID.temp; ID_MAYBE_INIT.code = ID.code } ----------
ID_MAYBE_INIT -> ID = EXPR { ID_MAYBE_INIT.code = EXPR.code||ID.temp + ' := ' + EXPR.temp }
FUNC -> TYPE ID ( ARGS ) { STMTS } { FUNC.code = STMT.code }
ARGS -> ARG , ARGS
ARGS -> ARG
ARG -> TYPE ID
ARG -> ''
IF -> if ( EXPR ) { STMTS1 } else { STMTS2 } { IF.code == EXP.code || "if (" expr.temp ") goto IFL\ngoto elseL\n ifL: Stmts1.code\ngoto skipL\n elseL: statements2.code \n skipL:\n" } -----------------
IF -> if ( EXPR ) { STMTS } else IF ----------------------------
IF -> if ( EXPR ) { STMTS } { ifL = genL() skipL = genL() IF.code = EXPR.code || "if (EXPR.temp) goto ifL \n goto skipL \n ifL: STMTS.code \n skipL:" } -----------
FOR -> for ( DEFN ; EXPR ; ASSIGN ) { STMTS }
WHILE -> while ( EXPR ) { STMTS } -----------
RETURN -> return EXPR
ASSIGN -> ID = EXPR { ASSIGN.code = EXPR.code || "ID.temp := EXPR.temp" } ------------
EXPR -> EXPR == TERM { EXPR.temp = newtemp(); EXPR.code = "EXPR.temp := EXPR.temp == TERM.temp" } --------------
EXPR -> EXPR != TERM { EXPR.temp = newtemp(); EXPR.code = "EXPR.temp := EXPR.temp != TERM.temp" } --------------
EXPR -> EXPR > TERM { EXPR.temp = newtemp(); EXPR.code = "EXPR.temp := EXPR.temp > TERM.temp" } --------------
EXPR -> EXPR < TERM { EXPR.temp = newtemp(); EXPR.code = "EXPR.temp := EXPR.temp < TERM.temp" } --------------
EXPR -> EXPR >= TERM { EXPR.temp = newtemp(); EXPR.code = "EXPR.temp := EXPR.temp >= TERM.temp" } --------------
EXPR -> EXPR <= TERM { EXPR.temp = newtemp(); EXPR.code = "EXPR.temp := EXPR.temp <= TERM.temp" } --------------
EXPR -> TERM { EXPR.temp = TERM.temp } ------------
TERM -> TERM + TERM2 { TERM.temp = newtemp(); TERM.code = "TERM.temp := TERM.temp + TERM2.temp" } --------------
TERM -> TERM - TERM2 { TERM.temp = newtemp(); TERM.code = "TERM.temp := TERM.temp - TERM2.temp" } ----------
TERM -> TERM2 { TERM.temp = TERM2.temp } ----------
TERM2 -> TERM2 * TERM3 { TERM2.temp = newtemp(); TERM2.code = "TERM2.temp := TERM2.temp * TERM3.temp" } --------
TERM2 -> TERM2 / TERM3 { TERM2.temp = newtemp(); TERM2.code = "TERM2.temp := TERM2.temp / TERM3.temp" } --------
TERM2 -> TERM3 { TERM2.temp = TERM3.temp } --------
TERM3 -> ( EXPR ) { TERM3.temp = EXPR.temp } --------
TERM3 -> ID { TERM3.temp = ID.temp } --------
TERM3 -> NUM { TERM3.temp = NUM.temp; } --------
TERM3 -> STR { TERM3.temp = STR.temp } --------
TERM3 -> FLT { TERM3.temp = FLT.temp } --------
TERM3 -> FUNC_CALL { TERM3.code = FUNC_CALL.code }
FUNC_CALL -> ID ( FUNC_ARGS ) { FUNC_CALL.code = FUNC_ARGS.code }
FUNC_ARGS -> FUNC_ARG , FUNC_ARGS { FUNC_ARGS.code = FUNC_ARG.code || FUNC_ARGS.code }
FUNC_ARGS -> FUNC_ARG { FUNC_ARGS.code = FUNC_ARG.code }
FUNC_ARG -> EXPR { FUNC_ARG.code = EXPR.code }
FUNC_ARG -> ''
TYPE -> int
TYPE -> float
TYPE -> char
TYPE -> double
TYPE -> string
TYPE -> void
ID -> <id> { ID.temp = <id>; ID.code = '' } --------
NUM -> <num> { NUM.temp = newtemp(); NUM.code = "NUM.temp := <num>" } --------
NUM -> + <num> { NUM.temp = newtemp(); NUM.code = "NUM.temp := <num>" } --------
NUM -> - <num> { NUM.temp = newtemp(); NUM.code = "NUM.temp := -<num>" } --------
STR -> <str> { STR.temp = newtemp(); STR.code = "STR.temp := <str>" } --------
FLT -> <flt> { FLT.temp = newtemp(); FLT.code = "FLT.temp := <flt>" } --------
FLT -> + <flt> { FLT.temp = newtemp(); FLT.code = "FLT.temp := <flt>" } --------
FLT -> - <flt> { FLT.temp = newtemp(); FLT.code = "FLT.temp := -<flt>" } --------