Java parser

computerharpyΛογισμικό & κατασκευή λογ/κού

2 Δεκ 2013 (πριν από 3 χρόνια και 8 μήνες)

85 εμφανίσεις

Java Parser

Reserved words

TOKEN :

{


< ABSTRACT: "abstract" >

| < BOOLEAN: "boolean" >

| < BREAK: "break" >

| < BYTE: "byte" >

| < CASE: "case" >

| < CATCH: "catch" >

| < CHAR: "char" >

| < CLASS: "class" >

| < CONST: "const" >

| < CONTINUE: "continue" >

| < _DEFAULT: "default" >

| < DO: "do" >

| < DOUBLE: "double" >

| < ELSE: "else" >

| < EXTENDS: "extends" >

| < FALSE: "false" >

| < FINAL: "final" >

| < FINALLY: "finally" >

| < FLOAT: "float" >

| < FOR: "for" >

| < GOTO: "goto" >

| < IF: "if" >

| < IMPLEMENTS: "implements" >

| < IMPORT: "import" >

| < INSTANCEOF: "instanceof" >

| < INT: "int" >


| < INTERFACE: "interface" >

| < LONG: "long" >

| < NATIVE: "native" >

| < NEW: "new" >

| < NULL: "null" >

| < PACKAGE: "package">

| < PRIVATE: "private" >

| < PROTECTED: "protected" >

| < PUBLIC: "public" >

| < RETURN: "return" >

| < SHORT: "short" >

| < STATIC: "static" >

| < SUPER: "super" >

| < SWITCH: "switch" >

| < SYNCHRONIZED: "synchronized" >

| < THIS: "this" >

| < THROW: "throw" >

| < THROWS: "throws" >

| < TRANSIENT: "transient" >

| < TRUE: "true" >

| < TRY: "try" >

| < VOID: "void" >

| < VOLATILE: "volatile" >

| < WHILE: "while" >

}

interface

/**
The VoxAgent interface acts as a bridge between agents and the game. <BR>
Implement all of the methods and Vox will call them at the specified times.
*/
public interface VoxAgent{
/**
At the start of the game your agent will be constructed and then the setPrefs() method will be
called. It will tell you your owner ID as well as give you a reference to the VoxWorld object
for this game. You should store this information, as you will need it later.
*/
public void setPrefs( int ID, VoxWorld world );
/** Each turn this method is called for the agent to send his moves to the VoxWorld. This is
the method where most of your bot smarts will go.
*/
public void declareMoves(Country[] countries);
/** This is the name of your agent. It will identify you in the info window and record books
*/
public String name();
/** The version of your agent. It is used by the plugin manager to notify the user when new
versions are made available.
*/
public float version();
/** A description of your agent.
*/
public String description();
/** If your agent wins the game then this method will be called.
<BR>
Whatever you return will be displayed in big letters across the screen. <P>
If you think that you will win a lot please provide many different answers for variety.
*/
public String youWon();
/** This method may be used in the future to send notifications to the VoxAgent.
*/
public String message( String message, Object data );
}
// end of the VoxAgent interface
Abstract classes


abstract class

is a class that is declared abstract.


May or may not include abstract methods.


Abstract classes cannot be instantiated.


An

abstract method

is a method that is declared without an
implementation (without braces, and followed by a semicolon).

abstract

class GraphicObject {


int x, y;


...


void moveTo(int newX, int newY) {


...


}


abstract

void draw();


abstract

void resize();

}

goto


In the Java programming language, goto is a
reserved word.


The Java programming language does not
have a goto statement.


It is a word reserved for possible
incorporation in a future version.

instanceof


The instanceof keyword can be used to test
if an object is of a specified type.

public class MainClass {


public static void main(String[] a) {


String s = "Hello";


if (s
instanceof

java.lang.String)


System.out.println("is a String");


}

}

Difference between throw and throws in Java


throw is used to actually throw the exception.


throws is declarative for the method.


One declares it, and the other one does it


They are not interchangeable.

public void myMethod (int param)
throws

MyException

{


if (param < 10) {


throw

new MyException("Too low!");


}


//Blah, Blah, Blah...

}

transient


When an object is transferred through the network,
the object needs to be 'serialized'. Serialization
converts the object state to serial bytes. Those bytes
are sent over the network and the object is recreated
from those bytes. Member variables marked by the
java
transient

keyword are not transferred, they are
lost on purpose.

public class Foo

{


private String saveMe;


private
transient

String dontSaveMe;


private
transient

String password;


.


.

}

volatile


It marks a member variable not to be used in
optimization, during compilation.


Optimizing compilers may insert code to gain
run
-
time performance.


This can cause problems when a variable can
be changed by many threads.


Those variables that can be changed by more
than one thread should be set to volatile.


E.g.,

private
volatile

changingVar;

Numeric literals

TOKEN :

{


< INTEGER_LITERAL:


<DECIMAL_LITERAL> (["l","L"])?


| <HEX_LITERAL> (["l","L"])?


| <OCTAL_LITERAL> (["l","L"])?


>

|


< #DECIMAL_LITERAL: ["1"
-
"9"] (["0"
-
"9"])* >

|


< #HEX_LITERAL: "0" ["x","X"] (["0"
-
"9","a"
-
"f","A"
-
"F"])+ >

|


< #OCTAL_LITERAL: "0" (["0"
-
"7"])* >

|


< FLOATING_POINT_LITERAL:


(["0"
-
"9"])+ "." (["0"
-
"9"])* (<EXPONENT>)? (["f","F","d","D"])?


| "." (["0"
-
"9"])+ (<EXPONENT>)? (["f","F","d","D"])?


| (["0"
-
"9"])+ <EXPONENT> (["f","F","d","D"])?


| (["0"
-
"9"])+ (<EXPONENT>)? ["f","F","d","D"]


>

|


< #EXPONENT: ["e","E"] (["+","
-
"])? (["0"
-
"9"])+ >



Separators

TOKEN :

{


< LPAREN: "(" >

| < RPAREN: ")" >

| < LBRACE: "{" >

| < RBRACE: "}" >

| < LBRACKET: "[" >

| < RBRACKET: "]" >

| < SEMICOLON: ";" >

| < COMMA: "," >

| < DOT: "." >

}

Operators

TOKEN :

{


< ASSIGN: "=" >

| < GT: ">" >

| < LT: "<" >

| < BANG: "!" >

| < TILDE: "~" >

| < HOOK: "?" >

| < COLON: ":" >

| < EQ: "==" >

| < LE: "<=" >

| < GE: ">=" >

| < NE: "!=" >

| < SC_OR: "||" >

| < SC_AND: "&&" >

| < INCR: "++" >

| < DECR: "
--
" >

| < PLUS: "+" >

| < MINUS: "
-
" >

| < STAR: "*" >

| < SLASH: "/" >

| < BIT_AND: "&" >

| < BIT_OR: "|" >

| < XOR: "^" >

| < REM: "%" >

| < LSHIFT: "<<" >

| < RSIGNEDSHIFT: ">>" >

| < RUNSIGNEDSHIFT: ">>>" >

| < PLUSASSIGN: "+=" >

| < MINUSASSIGN: "
-
=" >

| < STARASSIGN: "*=" >

| < SLASHASSIGN: "/=" >

| < ANDASSIGN: "&=" >

| < ORASSIGN: "|=" >

| < XORASSIGN: "^=" >

| < REMASSIGN: "%=" >

| < LSHIFTASSIGN: "<<=" >

| < RSIGNEDSHIFTASSIGN: ">>=" >

| < RUNSIGNEDSHIFTASSIGN: ">>>=" >

}

CompilationUnit

void CompilationUnit() :

{ }

{


[ PackageDeclaration() ]


( ImportDeclaration() )*


( TypeDeclaration() )*


<EOF>

}

What is the smallest legal Java program?

Declarations in jj

void PackageDeclaration() : { }

{ "package" Name() ";" }


void ImportDeclaration() : { }

{ "import" Name() [ "." "*" ] ";" } // Eg, import java.io.*;


void TypeDeclaration() : { }

{


LOOKAHEAD( ( "abstract" | "final" | "public" )* "
class
" )


ClassDeclaration()

| InterfaceDeclaration()

| ";"

}


void ClassDeclaration() : { }

{


( "abstract" | "final" | "public" )* "
class
"


<IDENTIFIER> [ "extends" Name() ] [ "implements" NameList() ]


"{" ( ClassBodyDeclaration() )* "}"

}

extends vs. implements


extends


the new class extends a superclass


automatically inherits all of the superclass's
methods


override any of them (except the ones defined
"final")


implements


implements an interface


the class must override all of the interface's
methods, since the interface contains only
definitions, not code.