User Tools

Site Tools


cc17:eminijava

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
cc17:eminijava [2017/02/02 20:46]
hossein
cc17:eminijava [2017/02/20 17:17] (current)
hossein [BNF]
Line 1: Line 1:
-====== Extended MiniJava (E-MiniJava) ======+====== Extended MiniJava (eMiniJava) ======
  
 [[http://​www.cambridge.org/​resources/​052182060X/​|MiniJava]] ​ is a subset of the programming language Java as  described in the Appendix of the [[http://​www.cs.princeton.edu/​~appel/​modern/​|Tiger book]]. [[http://​www.cambridge.org/​resources/​052182060X/​|MiniJava]] ​ is a subset of the programming language Java as  described in the Appendix of the [[http://​www.cs.princeton.edu/​~appel/​modern/​|Tiger book]].
-Extended MiniJava (E-MiniJava) is an extension to MiniJava that we use in this course. +Extended MiniJava (eMiniJava) is an extension to MiniJava that we use in this course. 
-Each group will gradually develop a compiler ​through ​implementing different ​phases ​of the compiler step by step.+Each group of students ​will gradually develop a compiler ​for eMiniJava by implementing different ​stages ​of the compiler step-by-step.
  
 ===== General Overview ===== ===== General Overview =====
Line 9: Line 9:
   * Object oriented. It supports classes with inheritance and method overriding (but not overloading).   * Object oriented. It supports classes with inheritance and method overriding (but not overloading).
   * Imperative. All class fields and local variables are mutable. While-loop and conditional statement are the main control structure.   * Imperative. All class fields and local variables are mutable. While-loop and conditional statement are the main control structure.
-  * It supports the following types: ​IntsBooleansStringsInt arrays ​and reference types (classes). +  * It supports the following types: ​''​int''​''​boolean''​''​String''​''​int array'' ​and reference types (classes). 
-  * All classes in a E-MiniJava ​program are included in a single source file.+  * All classes in a eMiniJava ​program are included in a single source file.
  
  
 ===== BNF ===== ===== BNF =====
-The syntax of Tool is given by the following BNF grammar:+The syntax of eMiniJava ​is given by the following BNF grammar:
  
 |  //​Program//​|::​=|//​MainClass//​ ( //​ClassDeclaration//​ )* <​EOF> ​ | |  //​Program//​|::​=|//​MainClass//​ ( //​ClassDeclaration//​ )* <​EOF> ​ |
Line 38: Line 38:
 |      |   ​|//​Expression//​ **.** //​Identifier//​ **(** ( //​Expression//​ ( **,** //​Expression//​ )* )? **)**  | |      |   ​|//​Expression//​ **.** //​Identifier//​ **(** ( //​Expression//​ ( **,** //​Expression//​ )* )? **)**  |
 |      |   ​|<​INTEGER_LITERAL> ​ | |      |   ​|<​INTEGER_LITERAL> ​ |
-|      |   ​|**%%"​%%** <​STRING_LITERAL>​ **%%"​%%** ​ |+|      |   ​|**%%"​%%**<​STRING_LITERAL>​**%%"​%%** ​ |
 |      |   ​|**true** ​ | |      |   ​|**true** ​ |
 |      |   ​|**false** ​ | |      |   ​|**false** ​ |
 |      |   ​|//​Identifier// ​ | |      |   ​|//​Identifier// ​ |
 |      |   ​|**this** ​ | |      |   ​|**this** ​ |
-|      |   ​|**new ​Int [** //​Expression//​ **]**  |+|      |   ​|**new ​int [** //​Expression//​ **]**  |
 |      |   ​|**new** //​Identifier//​ **( )**  | |      |   ​|**new** //​Identifier//​ **( )**  |
 |      |   ​|**!** //​Expression// ​ | |      |   ​|**!** //​Expression// ​ |
Line 57: Line 57:
  
 The precise way to describe the semantics of a programming language is by using mathematical description,​ for example operational semantics (see e.g. [[plc16:​lecture_3|Lecture 3]] of PLC course). The precise way to describe the semantics of a programming language is by using mathematical description,​ for example operational semantics (see e.g. [[plc16:​lecture_3|Lecture 3]] of PLC course).
-Since studying semantics is not officially part of the Compiler Construction course and some students may not have the background knowledge for mathematical semantics, we give an informal description ​of some of the  language constructs.+Since studying semantics is not officially part of the Compiler Construction course and some students may not have the background knowledge for mathematical semantics, we give an informal description of the  language constructs.
  
 ==== + ==== ==== + ====
Line 63: Line 63:
 The ''​+''​ operator can be applied to both ''​int''​ and ''​String''​ operands. ​ The ''​+''​ operator can be applied to both ''​int''​ and ''​String''​ operands. ​
 When ''​+''​ is applied to integers, the result is always integer. When ''​+''​ is applied to integers, the result is always integer.
-When at least one operand is String, the result is the concatenation of operands.+When at least one operand is ''​String''​, the result is the concatenation of operands.
 For concatenation of an ''​int''​ to ''​String''​ the string representation of the integer operand is considered. For concatenation of an ''​int''​ to ''​String''​ the string representation of the integer operand is considered.
  
Line 106: Line 106:
     return 0;     return 0;
   }   }
-   +
-  ​+
 then then
   * ''​sidef(this.hi());''​ will print ''​Hello World!''​ on the standard output and will discard the result ​   * ''​sidef(this.hi());''​ will print ''​Hello World!''​ on the standard output and will discard the result ​
Line 145: Line 144:
 ===== Miscellaneous ===== ===== Miscellaneous =====
  
-* Comments can be marked using the double slash notation (**%%//​%%**) or blocks (** /* ** ... ** */ **). Nested blocks are __not__ allowed.+  ​* Comments can be marked using the double slash notation (**%%//​%%**) or blocks (** /* ** ... ** */ **). Nested blocks are __not__ allowed.
   * The **else** branch of the **if** construct is optional.   * The **else** branch of the **if** construct is optional.
-* Inheritance works as in Java. E-MiniJava ​doesn'​t ​have the notions of interfaces, abstract members or abstract classes. +  ​* Inheritance works as in Java. eMiniJava ​doesn'​t ​support ​the notions of interfaces, abstract members or abstract classes. 
-  * E-MiniJava ​only supports default constructors (constructors without arguments). +  * eMiniJava ​only supports default constructors (constructors without arguments). 
-* Method //​overloading//​ is not allowed, but method //​overriding//​ is allowed. If you are unclear about the difference between the two, see for instance [[http://​stackoverflow.com/​questions/​673721/​overloading-and-overriding|this page]]. The only overloading in E-MiniJava ​is on the ''​+''​ operator, which can be used with integers, strings, and between the two types. +  * Method //​overloading//​ is not allowed, but method //​overriding//​ is allowed. If you are unclear about the difference between the two, see for instance [[http://​stackoverflow.com/​questions/​673721/​overloading-and-overriding|this page]]. The only overloading in eMiniJava ​is on the ''​+''​ operator, which can be used with integers, strings, and between the two types. 
-  * E-MiniJava ​does not allow two fields with the same name in a class, or two classes that inherit each other (field overriding is not allowed). +  * eMiniJava ​does not allow two fields with the same name in a class, or two classes that inherit each other (field overriding is not allowed). 
-E-MiniJava ​does not allow two variables with the same name in a method (including parameters and locally defined variables). +  eMiniJava ​does not allow two variables with the same name in a method (including parameters and locally defined variables). However, it //allows// to define a variable in a method whose enclosing ​class has a field of the same name. In that case, variable shadowing happens: the method variable takes precedence. 
-However, it //allows// to define a variable in a method whose enclosing ​object ​has a field of the same name. In that case, variable shadowing happens: the method variable takes precedence. +  * Accessing a field that has not been initialized results in undefined behavior (you can use a default value in this case, but your compiler ​//​must ​not// crash in any case). 
-  * Accessing a field that has not been initialized results in undefined behavior (you can use a default value in this case, but your compiler ​should ​not crash in any case). +  * Only constant strings (strings given as literals) are allowed. 
-* Only constant strings (strings given as literals) are allowed. +  * Although the grammar specifies that expressions such as ''​%%"%%foobar%%"%%.method()''​ are legal, they have no meaning in eMiniJava ​(they would result in a type error). The only operations allowed are: concatenating strings with other strings or with integers, printing strings, passing strings as argument and returning strings.
-* Although the grammar specifies that expressions such as ''"​my string"​.method()''​ are legal, they have no meaning in E-MiniJava ​(they would result in a type error). The only operations allowed are: concatenating strings with other strings or with integers, printing strings, passing strings as argument and returning strings.+
   * The operator precedence is the same as in Java. From highest priority to lowest: **!**, then **%%*%%** and **/**, then **+** and **-**, then **<** and **==**, then **&&​**,​ then **||**. Also, ''​new''​ binds tighter than ''​.''​ (method call or ''​.length''​),​ which binds tighter than ''​[]''​ (array read), which binds tighter than any operator. So ''​ 1 + new Foo().bar().baz()[42]''​ means ''​1 + ( ( ( (new Foo()).bar()).baz())[42])''​.   * The operator precedence is the same as in Java. From highest priority to lowest: **!**, then **%%*%%** and **/**, then **+** and **-**, then **<** and **==**, then **&&​**,​ then **||**. Also, ''​new''​ binds tighter than ''​.''​ (method call or ''​.length''​),​ which binds tighter than ''​[]''​ (array read), which binds tighter than any operator. So ''​ 1 + new Foo().bar().baz()[42]''​ means ''​1 + ( ( ( (new Foo()).bar()).baz())[42])''​.
   * All binary operators are left-associative. E.g. ''​1-2+3''​ means ''​(1-2)+3''​ and **not** ''​1-(2+3)''​. (Of course this does not matter for operators of different precedence).   * All binary operators are left-associative. E.g. ''​1-2+3''​ means ''​(1-2)+3''​ and **not** ''​1-(2+3)''​. (Of course this does not matter for operators of different precedence).
 +
 +===== Examples =====
 +
 +There are a set of MiniJava programs on the [[http://​www.cambridge.org/​resources/​052182060X/​|MiniJava webpage]]. Since eMiniJava is a superset of MiniJava, they are eMiniJava programs as well.
 +
cc17/eminijava.1486086363.txt.gz · Last modified: 2017/02/02 20:46 by hossein