Skip to content

Commit 7b43ca9

Browse files
committed
Fix merge conflict
2 parents d751b43 + fec483a commit 7b43ca9

File tree

3 files changed

+91
-15
lines changed

3 files changed

+91
-15
lines changed

README.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,76 @@ Built using Flex & Bison to take in any java source code and emits its equivalen
44

55
<p align='center'><img src='./images/cover.png'/></p>
66

7+
## Translation of Productions
8+
Each production is associated with a **semantic actions**. This semantic actions will be generated when the production is matched by the parser.
9+
10+
11+
## Backpatching
12+
When generating code for boolean expressions and flow-of-control statements is that of matching a jump instruction with the target of the jump. For example, the translation of the boolean expression B in **if ( B ) S**, the target of the jump when **B** is false will not be declared until **S** is examined. In a
13+
one-pass translation, the problem is solved by passing labels as inherited attributes to where the relevant jump instructions were generated. But a separate pass is then needed to bind labels to addresses.
14+
15+
***Backpatching*** counters the problem of two-passes translation by having lists of jumps are passed as synthesized attributes. Specifically, when a jump
16+
is generated, the target of the jump is temporarily left unspecified. Each such jump is put on a list of jumps whose labels are to be filled in when the proper
17+
label can be determined.
18+
19+
20+
## Assumptions
21+
### Java Used Grammar
22+
The input programs we are dealing with follow the below rules. These aren't a full java grammar but a subset of it.
23+
24+
```
25+
method_body:
26+
%empty
27+
|statement_list
28+
;
29+
30+
statement_list:
31+
statement
32+
|statement_list statement
33+
;
34+
35+
statement:
36+
declaration
37+
|if
38+
|while
39+
|assignment
40+
;
41+
42+
declaration: primitive_type IDENTIFIER ';';
43+
44+
primitive_type:
45+
INT
46+
|FLOAT
47+
;
48+
49+
if:
50+
IF '(' boolean_expression ')'
51+
'{' statement_list '}'
52+
ELSE '{' statement_list '}'
53+
;
54+
55+
while:
56+
WHILE '(' boolean_expression ')'
57+
'{' statement_list '}'
58+
;
59+
60+
assignment: IDENTIFIER '=' expression ';';
61+
62+
expression:
63+
INT_NUM
64+
|FLOAT_NUM
65+
|IDENTIFIER
66+
|expression ARITH_OP expression
67+
|'(' expression ')'
68+
;
69+
70+
boolean_expression:
71+
TRUE
72+
|FALSE
73+
|expression BOOL_OP expression
74+
|expression REL_OP expression
75+
```
76+
777
## Running & Testing
878

979
To run the program, use the script `run.sh` as follows:

java_parser.y

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,11 @@ while:
146146
backpatch(*($4.trueList), $7.nextInstructionIndex);
147147
$$.nextList = new unordered_set<int>();
148148
*($$.nextList) = *($4.falseList);
149+
<<<<<<< HEAD
149150
appendToCode("goto " + to_string($2.nextInstructionIndex));
151+
=======
152+
appendToCode("goto Label_" + to_string($2.nextInstructionIndex));
153+
>>>>>>> fec483a1a35de6398462b673852227877513f7b8
150154
}
151155
;
152156

semantic_actions_utils.h

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -54,44 +54,46 @@ namespace semantic_actions_util {
5454
}
5555

5656
void appendToCode(string code) {
57-
outputCode.push_back(code);
57+
outputCode.push_back("Label_" + std::to_string(nextInstructionIndex) + ":\n" + code);
5858
nextInstructionIndex++;
5959
}
6060

6161
void backpatch(unordered_set<int> list, int instruction_index) {
6262
for (auto index : list) {
6363
outputCode[index] = outputCode[index].substr(0, outputCode[index].size()-1);
64-
outputCode[index] += to_string(instruction_index);
64+
outputCode[index] += "Label_" + to_string(instruction_index);
6565
}
6666
}
6767

6868
void defineVariable(string name, int varType) {
6969
declareVariable(name, varType);
7070
if (varType == INT_TYPE) {
71-
appendToCode("iconst_0\nistore_" + to_string(currentVariableIndex -1));
71+
appendToCode("iconst_0");
72+
appendToCode("istore_" + to_string(currentVariableIndex -1));
7273
} else if (varType == FLOAT_TYPE) {
73-
appendToCode("fconst_0\nfstore_" + to_string(currentVariableIndex -1));
74+
appendToCode("fconst_0");
75+
appendToCode("fstore_" + to_string(currentVariableIndex -1));
7476
}
7577

7678
}
7779

7880
void generateHeader() {
7981
//TO-DO get file name
8082
//appendToCode(".source " + outfileName);
81-
appendToCode(".class public java_class\n.super java/lang/Object\n");
82-
appendToCode(".method public <init>()V");
83-
appendToCode("aload_0");
84-
appendToCode("invokenonvirtual java/lang/Object/<init>()V");
85-
appendToCode("return");
86-
appendToCode(".end method\n");
87-
appendToCode(".method public static main([Ljava/lang/String;)V");
88-
appendToCode(".limit locals 100\n.limit stack 100");
89-
appendToCode(".line 1");
83+
outputCode.push_back(".class public java_class\n.super java/lang/Object\n");
84+
outputCode.push_back(".method public <init>()V");
85+
outputCode.push_back("aload_0");
86+
outputCode.push_back("invokenonvirtual java/lang/Object/<init>()V");
87+
outputCode.push_back("return");
88+
outputCode.push_back(".end method\n");
89+
outputCode.push_back(".method public static main([Ljava/lang/String;)V");
90+
outputCode.push_back(".limit locals 100\n.limit stack 100");
91+
outputCode.push_back(".line 1");
9092
}
9193

9294
void generateFooter() {
93-
appendToCode("return");
94-
appendToCode(".end method");
95+
outputCode.push_back("return");
96+
outputCode.push_back(".end method");
9597
}
9698

9799
unordered_set<int> makeList(int instruction_index) {

0 commit comments

Comments
 (0)