Skip to content

Commit 6b94351

Browse files
authored
Update README.md
1 parent bc7480e commit 6b94351

File tree

1 file changed

+62
-20
lines changed

1 file changed

+62
-20
lines changed

README.md

Lines changed: 62 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,61 @@
11
<h1 align='center'>Yet Another Java ByteCode Generator</h1>
2-
3-
Built using Flex & Bison to take in any java source code and emits its equivalent bytecode. This is a project developed for the PLT (**P**rogramming **L**anguage **T**ranslation) course at Faculty of Engineering, Alexandria University in Spring 2020 Offering.
2+
<h3 align='center'>(A javac-like Compiler)</h3>
43

54
<p align='center'><img src='./images/cover.png'/></p>
65

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.
6+
## Overview
7+
A java bytecode generator/compiler built over the tools Flex & Bison to take in any java source code (a subset only of Java Lang is covered) and emits its equivalent bytecode. This is a project developed for the PLT (**P**rogramming **L**anguage **T**ranslation) course at Faculty of Engineering, Alexandria University in Spring 2020 Offering.
8+
9+
The **goal** is to practice techniques of constructing semantics rules to generate java bytecode. Generated bytecode must follow Standard bytecode instructions defined in [Java Virtual Machine Specification].(http://java.sun.com/docs/books/jvms/second_edition/html/VMSpecTOC.doc.html)
10+
11+
Our **workflow** included the following steps:
12+
1. Understanding the **Flex & Bison tools** we are going to build upon.
13+
2. Understanding the **Semantic Actions/Rules asscoiated with a Java CFG**.
14+
3. Understanding the **Java Bytecode** we are required to generate as an intermediate code represenattion.
15+
4. Building the Java bytecode generator upon the building blocks we understood.
16+
17+
18+
## Flex & Bison Tools
19+
Flex and bison are modern replacements for the classic lex and yacc that were both developed at Bell Laboratories in the 1970s.
20+
Flex and bison are tools designed for writers of compilers and interpreters, although they are also useful for many applications that will interest noncompiler writers. Any application that looks for patterns in its input or has an input or command language is a good candidate for flex and bison. Furthermore, they allow for rapid application prototyping, easy modification, and simple maintenance of programs.
21+
22+
To stimulate your imagination, here are a few things people have used flex and bison, or their predecessors lex and yacc, to develop:
23+
- The desktop calculator bc
24+
- The tools eqn and pic, typesetting preprocessors for mathematical equations and complex pictures
25+
- Many other “domain-specific languages” targeted for a particular application
26+
- PCC, the Portable C Compiler used with many Unix systems
27+
- Flex itself
28+
- A SQL database language translator
29+
30+
31+
## Translation of Productions to Semantic Actions/Rules.
32+
33+
Each production is associated with a **semantic actions**. This semantic actions will be generated when the production is matched by the parser. The semantic actions/rules we developed in this case is ones that produce teh equivalent java bytecode to the following statement matched by parser.
34+
35+
We explain next two non-terminals and their semantic actions as example to our idea and the rest can be found and traced in the code itself. Then we move to a further step into inermediate code generaton which is essential, namely, Backpatching.
36+
37+
### Example 1: Method Body
38+
The generated code consists of a header,footer alongside with the statment list that contains the rest of the code,where the header appends the equivalent java byte-code for the class,the main method and sets the stack and locals size.
39+
The footer generetes a return from the method.
940

41+
### Example 2: IF
42+
- Sends the true list of the boolean expression and the first marker instruction index to the backpatching function.
43+
- Sends the false list of the boolean expression and the second marker instrcution index to the backpatching function.
44+
- Perform the merging of lists logic
1045

11-
## Backpatching
46+
### More Into Code Generation: Backpatching
1247
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
1348
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.
1449

1550
***Backpatching*** counters the problem of two-passes translation by having lists of jumps are passed as synthesized attributes. Specifically, when a jump
1651
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
1752
label can be determined.
1853

19-
## Method Body
20-
The generated code consists of a header,footer alongside with the statment list that contains the rest of the code,where the header appends the equivalent java byte-code for the class,the main method and sets the stack and locals size.
21-
The footer generetes a return from the method.
2254

23-
## IF
24-
-Sends the true list of the boolean expression and the first marker instruction index to the backpatching function.
25-
-Sends the false list of the boolean expression and the second marker instrcution index to the backpatching function.
26-
-Perform the merging of lists logic
55+
## Assumptions Made
2756

28-
## Assumptions
29-
### Java Used Grammar
30-
The input programs we are dealing with follow the below rules. These aren't a full java grammar but a subset of it.
57+
### The subset of Java grammar used
58+
These aren't a full java grammar but a subset of it. It covers the requirements that were imposed on us by the project to be delivered. We make the natural assumption that input programs must follow the below rules and not just any java code. Howwever, we develop the code to be extensible easily to the rest of the Java set of rules.
3159

3260
```
3361
method_body:
@@ -82,24 +110,40 @@ boolean_expression:
82110
|expression REL_OP expression
83111
```
84112

85-
## Running & Testing
113+
## Design & Code Anatomy
114+
115+
- `java_lexical_analyzer.l` ---> The Flex input file which the tool generates from a Lexical Analayzer for our Java subset.
116+
117+
- `java_parser.y` ---> The Bison input file which the tool generates from a Parser for our Java subset.
118+
119+
- `semantic_actions_utils.h` ---> The file that contains all utility functions that are used in the intermediate code generation and semantic actions for each production.
120+
121+
- `Makefile` ---> Defines rules for bash commands that is needed to build,test and run the code.
122+
123+
- `run.sh` ---> Defines a simple way to execute Make rules easily.
124+
125+
### Running & Testing Using run.sh
86126

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

89129
./run.sh file_name
90130
where filename, can be any file that contains java source code.
91131

92-
For testing purposes while development use this,
132+
For **testing purposes** while development use this,
93133

94134
./run.sh --debug file_name
95135
or
96136

97137
./run.sh -d file_name
98138

99-
where file_name is a file that exists in the `test-cases` folder. The extra argument will ensure we keep all our test files from the `test-cases` folder and picking the suitable unit test case for the part you're developing.
139+
where file_name in this case is a file that exists in the `test-cases` folder. The extra argument will ensure we keep all our test files from the `test-cases` folder and picking the suitable unit test case for the part you're developing.
100140

101141
## Resources
102142

143+
- Compilers, Principles, Techniques, & Tools, Second Edition by Alfred V. Aho & Monica S. Lam & Ravi Sethi & Jeffrey D. Ullman
144+
145+
- Engineering a Compiler, Second Edition by Keith D. Cooper & Linda Torczon.
146+
103147
### Understanding Flex & Bison
104148

105149
- [Flex & Bison by John Levine. The definitive textbok for using these tools.](http://web.iitd.ac.in/~sumeet/flex__bison.pdf)
@@ -112,6 +156,4 @@ where file_name is a file that exists in the `test-cases` folder. The extra argu
112156

113157
- [Wiki's Java bytecode](https://en.wikipedia.org/wiki/Java_bytecode) and the [list of the instructions that make up the Java bytecode.](https://en.wikipedia.org/wiki/Java_bytecode_instruction_listings)
114158

115-
### Understanding Jasmin
116-
117159
- [Jasmin Official Documentation.](http://jasmin.sourceforge.net/)

0 commit comments

Comments
 (0)