Project

Project Javacc

Buat yang lagi bikin compiler pake javcc,, ini ada sedikit hasil kerjaanku bareng temen2, sourcode ini merupakan tahap awal dalam pengerjaan..yang nantinya di compile pake javacc. typenya: *.j ,, yaa..walaupun mungkin ini tidak sempurna,, seditaknya dapat dijadikan gambaran,,atau masukan. jadi..bila ada kesalahan..moon maaf aj ya🙂

type file: .j yang kemudian dicompile dengan javacc:

options {
JAVA_UNICODE_ESCAPE = true;
}

PARSER_BEGIN(MyParser)
public class MyParser {

public static void main(String args[]) throws ParseException {
MyParser parser = new MyParser(System.in);
parser.PROG();
System.out.println(“Done”);
}
}

PARSER_END(MyParser)

SKIP :
{
” ”
| “\t”
| “\n”
| “\r”
| “\f”
}

SPECIAL_TOKEN :
{
<COMMENT1: “//” (~[“\n”,”\r”])* (“\n”|”\r”|”\r\n”)>
| <COMMENT2: “/**” (~[“*”])* “*” (“*” | (~[“*”,”/”] (~[“*”])* “*”))* “/”>
| <COMMENT3: “/*” (~[“*”])* “*” (“*” | (~[“*”,”/”] (~[“*”])* “*”))* “/”>
}

TOKEN :
{
< KURBUK: “(” >
| < KURTUP: “)” >
| < BUK: “[” >
| < TUP: “]” >
| < RABUK: “{” >
| < RATUP: “}” >
| < TIKKOM: “;” >
| < TK: “.” >
| < SD: “=” >
| < KD: “<” >
| < PLUS: “+” >
| < MINUS: “-” >
| < KALI: “*” >
| < AND : “&&” >
| < TASERU : “!” >
| < BOOLEAN: “boolean” >
| < CLASS: “class” >
| < INTERFACE: “interface” >
| < ELSE: “else” >
| < EXTENDS: “extends” >
| < FALSE: “false” >
| < IF: “if” >
| < WHILE: “while” >
| < INT: “int” >
| < LENGTH: “length” >
| < MAIN: “main” >
| < NEW: “new” >
| < PUBLIC: “public” >
| < RETURN: “return” >
| < STATIC: “static” >
| < STRING: “String” >
| < THIS: “this” >
| < TRUE: “true” >
| < PRINT: “System.out.println” >
| < VOID: “void” >
}

TOKEN :
{
< INTLIT: ( [“1”-“9”] ([“0”-“9”])* | “0” ) >
}

TOKEN :
{
< ID: ([“a”-“z”] | [“A”-“Z”]) (([“a”-“z”] | [“A”-“Z”])|<DIGIT>| “_”)* >
|

< #DIGIT: [“0”-“9”] >
}

void PROG() :
{}
{
MC() ( TD() )* <EOF>
}

void MC() :
{}
{
<CLASS> ID() <RABUK> <PUBLIC> <STATIC> <VOID> <MAIN> <KURBUK> <STRING> <BUK> <TUP> ID() <KURTUP> <RABUK> PRINTST() <RATUP> <RATUP>
}

void TD() :
{}
{
LOOKAHEAD(3)
CD()
|
CDEX()
}

void CD() :
{}
{
<CLASS> ID() <RABUK> ( VD() )*( MD() )* <RATUP>
}

void CDEX() :
{}
{
<CLASS> ID() <EXTENDS> ID() <RABUK> ( VD() )* ( MD() )* <RATUP>
}

void VD() :
{}
{
TYPE() ID() <TIKKOM>
}

void MD() :
{}
{
<PUBLIC> TYPE() ID() <KURBUK> ( FL() )? <KURTUP> <RABUK> ( LOOKAHEAD(2) VD() )* ( ST() )* <RETURN> EXP() <TIKKOM> <RATUP>
}

void FL() :
{}
{
FORMAL() ( FR() )*
}

void FORMAL() :
{}
{
TYPE() ID()
}

void FR() :
{}
{
“,” FORMAL()
}

void TYPE() :
{}
{
LOOKAHEAD(3)
ARRAYT()
|
BOOLEANT()
|
INTTYPE()
|
ID()
}

void ARRAYT() :
{}
{
<INT> <BUK> <TUP>
}

void BOOLEANT() :
{}
{
<BOOLEAN>
}

void INTTYPE() :
{}
{
<INT>
}

void ST() :
{}
{
LOOPST()
|
LOOKAHEAD(2)
SDST()
|
LOOKAHEAD(2)
ARRAYST()
|
IFST()
|
WHILEST()
|
PRINTST()
}

void LOOPST() :
{}
{
<RABUK> ( ST() )* <RATUP>
}

void SDST() :
{}
{
ID() <SD> EXP() <TIKKOM>
}

void ARRAYST() :
{}
{
ID() <BUK> EXP() <TUP> <SD> EXP() <TIKKOM>
}

void IFST() :
{}
{
<IF> <KURBUK> EXP() <KURTUP> ST() <ELSE> ST()
}

void WHILEST() :
{}
{
<WHILE> <KURBUK> EXP() <KURTUP> ST()
}

void PRINTST() :
{}
{
<PRINT> <KURBUK> EXP() <KURTUP> <TIKKOM>
}

void EXP() :
{}
{
LOOKAHEAD( EX1() <AND> )ANDEXP()
|
LOOKAHEAD( EX1() <KD> )KDEXP()
|
LOOKAHEAD( EX1() <PLUS> )PLUSEXP()
|
LOOKAHEAD( EX1() <MINUS> )MINUSEXP()
|
LOOKAHEAD( EX1() <KALI> )KALIEXP()
|
LOOKAHEAD( EX1() <BUK> )ARRAYEXP2()
|
LOOKAHEAD( EX1() <TK> <LENGTH> )ARRAYLENGTH()
|
LOOKAHEAD( EX1() <TK> ID() <KURBUK> )IDEXLIST()
|
EX1()
}

void ANDEXP() :
{}
{
EX1() <AND> EX1()
}

void KDEXP() :
{}
{
EX1() <KD> EX1()
}

void PLUSEXP() :
{}
{
EX1() <PLUS> EX1()
}

void MINUSEXP() :
{}
{
EX1() <MINUS> EX1()
}

void KALIEXP() :
{}
{
EX1() <KALI> EX1()
}

void ARRAYEXP2() :
{}
{
EX1() <BUK> EX1() <TUP>
}

void ARRAYLENGTH() :
{}
{
EX1() <TK> <LENGTH>
}

void IDEXLIST() :
{}
{
EX1() <TK> ID() <KURBUK> ( EXLIST() )? <KURTUP>
}

void EXLIST() :
{}
{
EXP() ( EXREST() )*
}

void EXREST() :
{}
{
“,” EXP()
}

void EX1() :
{}
{
INT_LIT()
|
TRUE_LIT()
|
FALSE_LIT()
|
ID()
|
THISEXP()
|
LOOKAHEAD(3)
ARRAYEXP()
|
NEWIDEXP()
|
TASERUEXP()
|
BUKTUPEXP()
}

void INT_LIT() :
{}
{
<INTLIT>
}

void TRUE_LIT() :
{}
{
<TRUE>
}

void FALSE_LIT() :
{}
{
<FALSE>
}

void ID() :
{}
{
<ID>
}

void THISEXP() :
{}
{
<THIS>
}

void ARRAYEXP() :
{}
{
<NEW> <INT> <BUK> EXP() <TUP>
}

void NEWIDEXP() :
{}
{
<NEW> ID() <KURBUK> <KURTUP>
}

void TASERUEXP() :
{}
{
<TASERU> EXP()
}

void BUKTUPEXP() :
{}
{
<KURBUK> EXP() <KURTUP>
}

    • najmasign
    • June 20th, 2009

    numpang copas dik🙂

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: