\def\star{$^{\textstyle *}$}
\def\ra{\!\!\!\!\rightarrow\!\!\!\!}
\chapter{MiniJava Language Reference Manual}

%
MiniJava is a subset of Java. The meaning of a MiniJava program is
given by its meaning as a Java program.  Overloading is not allowed
in MiniJava.  The MiniJava statement {\it System.out.println( \ldots );} can
only print integers. The MiniJava expression {\it e.length} only applies to 
expressions of type {\it int[]}.

%

%
\section{Lexical Issues}

\begin{description}
\item[Identifiers:]
An \emph{identifier} is a sequence of letters, digits, and underscores,
starting with a letter.  Uppercase letters are distinguished from lowercase.
In this reference manual the symbol \emph{id} stands for an identifier.

\item[Integer literals:]
A sequence of decimal digits is an {\it integer constant} that denotes
the corresponding integer value. 
In this appendix the symbol \emph{INTEGER\_LITERAL} stands for an integer constant.

\item[Binary operators:]
A \emph{binary operator} is one of 
\[\it{\&\&}\;\; 
\it{<}\;\;
\it{+}\;\;
\it{-}\;\;
\it{*}\]
In this appendix the symbol \emph{op} stands for a binary operator.


\item[Comments:]
A comment may appear between any two tokens.
There are two forms of comments:
one starts with 
\it{/*}, ends with \it{*/}, and may be nested;
another begins with \it{//} and goes to the end of the line.
\end{description}


\section{Grammar}
In the MiniJava grammar, we use the notation 
{\it N}\star, where {\it N} is a nonterminal,
to mean 0, 1, or more repetitions of {\it N}.
 
%\begin{figure}
\begin{tabular}{rcl}
          {\it Program} &$\ra$& {\it MainClass} {\it ClassDecl}\star\ \\
        {\it MainClass} &$\ra$& {\bf class}  {\it id} \it{\{} {\bf public} {\bf static} {\bf void}
                                {\bf main} \it{(} {\bf String} \it{[]}
                                {\it id} \it{)}\\
                             &&~~~~ \it{\{} {\it Statement} \it{\}} \it{\}} \\
        {\it ClassDecl} &$\ra$&  {\bf class} {\it id} \it{\{} 
                                 {\it VarDecl}\star\  {\it MethodDecl}\star\  \it{\}} \\
                        &$\ra$&  {\bf class} {\it id} {\bf extends} {\it id} \it{\{} 
                                 {\it VarDecl}\star\ {\it MethodDecl}\star\ \it{\}} \\
          {\it VarDecl} &$\ra$&  {\it Type id} \it{;} \\
       {\it MethodDecl} &$\ra$&  {\bf public} {\it Type id} \it{(}
                                 {\it FormalList} \it{)} \\
                             &&~~~~ \it{\{}  {\it VarDecl}\star\  
                                 {\it Statement}\star\ {\bf return}
                                 {\it Exp} \it{;} \it{\}} \\ 
      {\it FormalList}  &$\ra$&  {\it Type id} {\it FormalRest}\star\ \\
                        &$\ra$& \\       
       {\it FormalRest} &$\ra$& \it{,} {\it Type id} \\  
             {\it Type} &$\ra$&  {\bf int} \it{[]} \\
                        &$\ra$&  {\bf boolean} \\
                        &$\ra$&  {\bf int} \\
                        &$\ra$&  {\it id} \\
        {\it Statement} &$\ra$&  \it{\{} {\it Statement}\star\ \it{\}} \\
                        &$\ra$&  {\bf if} \it{(} {\it Exp} \it{)}
                                 {\it Statement} {\bf else} {\it Statement} \\
                        &$\ra$&  {\bf while} \it{(} {\it Exp} \it{)}
                                 {\it Statement} \\
                        &$\ra$&  {\bf System.out.println} \it{(}
                                 {\it Exp } \it{)} \it{;} \\
                        &$\ra$&  {\it id} \it{=} {\it Exp} \it{;} \\
                        &$\ra$&  {\it id} \it{[} {\it Exp} \it{]}
                                  \it{=} {\it Exp} \it{;} \\
              {\it Exp} &$\ra$&  {\it Exp }  {\it op} {\it Exp } \\
                        &$\ra$&  {\it Exp } \it{[} {\it Exp } \it{]} \\
                        &$\ra$&  {\it Exp } \it{.} {\bf length} \\
                        &$\ra$&  {\it Exp } \it{.} {\it id} \it{(} 
                                 {\it ExpList} \it{)} \\
                        &$\ra$&  {\it INTEGER\_LITERAL} \\
                        &$\ra$&  {\bf true} \\
                        &$\ra$&  {\bf false} \\
                        &$\ra$&  {\it id} \\
                        &$\ra$&  {\bf this} \\
                        &$\ra$&  {\bf new} {\bf int} \it{[} {\it Exp} \it{]} \\
                        &$\ra$&  {\bf new} {\it id} \it{(} \it{)} \\
                        &$\ra$&  \it{!} {\it Exp} \\
                        &$\ra$&  \it{(} {\it Exp} \it{)} \\
          {\it ExpList} &$\ra$&  {\it Exp} {\it ExpRest}\star\ \\
                        &$\ra$&\\
          {\it ExpRest} &$\ra$& \it{,} {\it Exp}
\end{tabular}
%\end{figure}
\clearpage
%
\section{Sample Program}
%
\begin{verbatim}
class Factorial {
    public static void main(String[] a) {
        System.out.println(new Fac().ComputeFac(10));
    }
}
class Fac {
    public int ComputeFac(int num) {
        int num_aux;
        if (num < 1)
            num_aux = 1;
        else 
            num_aux = num * (this.ComputeFac(num-1));
        return num_aux;
    }
}
\end{verbatim}
