JavaCC to create my only syntax

wangpeng

Neues Mitglied
Hello everyone,

I am a new with JavaCC. I have got a jj file. It works. But i don't unterstand 100%.

The jj file is :

options {
STATIC = false ;
FORCE_LA_CHECK = true;
IGNORE_CASE = true;
}

PARSER_BEGIN(ClassSYSCamosConverter)
package com.schaeffler.classification;

import java.io.StringReader;
import java.util.Set;
import java.util.HashSet;
import java.util.List;
import java.util.ArrayList;


public class ClassSYSCamosConverter extends SapDependencyCamosBase {

}
PARSER_END(ClassSYSCamosConverter)

SKIP : /* WHITE SPACE */
{
" "
| "\t"
| "\n"
| "\r"
| "\f"
}

TOKEN : { < IN : "in" | "IN" > }
TOKEN : { < AND : "and" | "AND" > }
TOKEN : { < OR : "or" | "OR" > }
TOKEN : { < NOT : "not" | "NOT" > }
TOKEN : { < LE : "<=" | "=<" > }
TOKEN : { < GE : ">=" | "=>" > }
TOKEN : { < NE : "<>" | "><" > }
TOKEN : { < LT : "<" > }
TOKEN : { < EQ : "==" > }
TOKEN : { < GT : ">" > }
TOKEN : { < TABLE : "table" > }
TOKEN : { < IF : "IF" | "if" > }

TOKEN : {
<IDENTIFIER: <LETTER> (<LETTER> | <DIGIT>)*>
| <#LETTER: ["$","A"-"Z","_","a"-"z"]>
| <#DIGIT: ["0"-"9"]>
}

TOKEN : {
<INTEGER_LITERAL: (["1"-"9"] (["0"-"9"])*) | "0">
| <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"])+>
| <STRING_LITERAL: "'" (~["'"])* "'">
}



TOKEN : { < KLAMMERAUF : "(" | "[" > }
TOKEN : { < KLAMMERZU : ")" | "]" > }
TOKEN : { < MINUS : "-" > }
TOKEN : { < PLUS : "+" > }
TOKEN : { < DIVIDE : "/" > }
TOKEN : { < MULTIPLY : "*" > }
TOKEN : { < KOMMA : "," > }
TOKEN : { < SELF : "$SELF." > }

void evaluate() :
{
Token t;
}
{
[
Expression()
|
Procedure()
( <KOMMA> Procedure())*
]
<EOF>
}

void Procedure() :
{
Token t;
Procedure procedure = new Procedure();
}
{
<SELF>
Identifier()
<EQ>
Expression()
{
procedure.action = code.toString();
code.setLength(0);
}
{procedures.add(procedure);}
}

void Expression():
{}
{
Or()
}

void Or() :
{}
{
And()
(
{code.append(" or ");}
<OR>
And()
)*
}

void And() :
{}
{
Not()
(
{code.append(" and ");}
<AND>
Not()
)*
}

void Not() :
{}
{
[
{code.append("not ");}
<NOT>
]
EqualityExpression()
}

void EqualityExpression() :
{}
{
RelationalExpression()
[
{code.append(" = ");}
<EQ>
RelationalExpression()
|
{code.append(" <> ");}
<NE>
RelationalExpression()
]
}

void RelationalExpression() :
{}
{
AdditiveExpression()
[
{code.append(" <= ");}
<LE>
AdditiveExpression()
|
{code.append(" >= ");}
<GE>
AdditiveExpression()
|
{code.append(" < ");}
<LT>
AdditiveExpression()
|
{code.append(" > ");}
<GT>
AdditiveExpression()
]
}

void AdditiveExpression() :
{}
{
MultiplicativeExpression()
(
{code.append(" + ");}
<PLUS>
MultiplicativeExpression()
|
{code.append(" - ");}
<MINUS>
MultiplicativeExpression()
)*
}

void MultiplicativeExpression() :
{}
{
InExpression()
(
{code.append(" * ");}
<MULTIPLY>
InExpression()
|
{code.append(" / ");}
<DIVIDE>
InExpression()
)*
}

void InExpression() :
{}
{
PrimaryExpression()
[


[
{code.append(" not ");}
<NOT>
]


{code.append(" in ");}
<IN>
ArrayExpression()
]
}

void ArrayExpression() :
{}
{
{code.append("(");}
<KLAMMERAUF>
RangeExpression()
(
{code.append(", ");}
<KOMMA>
RangeExpression()
)*
{code.append(")");}
<KLAMMERZU>
}

void RangeExpression() :
{}
{
PrimaryExpression()
[
{code.append(" - ");}
<MINUS>
PrimaryExpression()
]
}


void PrimaryExpression() :
{}
{
(
Identifier()
|
<TABLE>
Identifier()
<KLAMMERAUF>
Identifier()
<EQ>
Identifier()
<KLAMMERZU>
|
Constant()
|
{code.append("(");}
<KLAMMERAUF> Expression() <KLAMMERZU>
{code.append(")");}
)
}

void Identifier() :
{
Token t;
}
{
(
t = <IDENTIFIER>
{code.append(t.image);}
{featureList.add(t.image);}
)
}

void Constant() :
{
Token t;
}
{
(
t = <INTEGER_LITERAL>
{code.append(t.image);}
|
t = <FLOATING_POINT_LITERAL>
{code.append(t.image);}
|
t = <STRING_LITERAL>
{code.append(t.image);}
)
}

To test:

my input String is :

MMTAA524 in ('N','W') and MMTAA540 == 'g4' and MMTAA540 == ANYVALUE and MMTAA540 == NOVALUE AND MMTQQ = S45 AND MMTQW = blabla and MMPAA145


Syntax check is correct.

But i expect some errors.

MMTAA540 == ANYVALUE and MMTAA540 == NOVALUE AND MMTQQ = S45 AND MMTQW = blabla and MMPAA145

should output some errors.

How can i get some errors?

MMTAA540 == ANYVALUE and MMTAA540 == NOVALUE should be correct.

MMTAA540 == 'asdfasdfsd' should be correct

MMTAA540 == asdfasdfsd should be incorrect (error)

MMPAA540 hat no operation should be incorrect (error).

Some tipps for me!

Thank you very much!
 
Hi there,
wow, that looks very strange. Please format your source between java-tags. (see the RED sentence on top of editor...)
so it's better visible what you mean.
You wrote, that your'e new in java. In a short overwiew this code doesn't look like a code for a beginner :noe:
 
Zuletzt bearbeitet:
[WR]options {
STATIC = false ;
FORCE_LA_CHECK = true;
IGNORE_CASE = true;
}

PARSER_BEGIN(ClassSYSCamosConverter)
package com.schaeffler.classification;

import java.io.StringReader;
import java.util.Set;
import java.util.HashSet;
import java.util.List;
import java.util.ArrayList;


public class ClassSYSCamosConverter extends SapDependencyCamosBase {

}
PARSER_END(ClassSYSCamosConverter)

SKIP : /* WHITE SPACE */
{
" "
| "\t"
| "\n"
| "\r"
| "\f"
}

TOKEN : { < IN : "in" | "IN" > }
TOKEN : { < AND : "and" | "AND" > }
TOKEN : { < OR : "or" | "OR" > }
TOKEN : { < NOT : "not" | "NOT" > }
TOKEN : { < LE : "<=" | "=<" > }
TOKEN : { < GE : ">=" | "=>" > }
TOKEN : { < NE : "<>" | "><" > }
TOKEN : { < LT : "<" > }
TOKEN : { < EQ : "==" > }
TOKEN : { < GT : ">" > }
TOKEN : { < TABLE : "table" > }
TOKEN : { < IF : "IF" | "if" > }

TOKEN : {
<IDENTIFIER: <LETTER> (<LETTER> | <DIGIT>)*>
| <#LETTER: ["$","A"-"Z","_","a"-"z"]>
| <#DIGIT: ["0"-"9"]>
}

TOKEN : {
<INTEGER_LITERAL: (["1"-"9"] (["0"-"9"])*) | "0">
| <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"])+>
| <STRING_LITERAL: "'" (~["'"])* "'">
}



TOKEN : { < KLAMMERAUF : "(" | "[" > }
TOKEN : { < KLAMMERZU : ")" | "]" > }
TOKEN : { < MINUS : "-" > }
TOKEN : { < PLUS : "+" > }
TOKEN : { < DIVIDE : "/" > }
TOKEN : { < MULTIPLY : "*" > }
TOKEN : { < KOMMA : "," > }
TOKEN : { < SELF : "$SELF." > }

void evaluate() :
{
Token t;
}
{
[
Expression()
|
Procedure()
( <KOMMA> Procedure())*
]
<EOF>
}

void Procedure() :
{
Token t;
Procedure procedure = new Procedure();
}
{
<SELF>
Identifier()
<EQ>
Expression()
{
procedure.action = code.toString();
code.setLength(0);
}
{procedures.add(procedure);}
}

void Expression():
{}
{
Or()
}

void Or() :
{}
{
And()
(
{code.append(" or ");}
<OR>
And()
)*
}

void And() :
{}
{
Not()
(
{code.append(" and ");}
<AND>
Not()
)*
}

void Not() :
{}
{
[
{code.append("not ");}
<NOT>
]
EqualityExpression()
}

void EqualityExpression() :
{}
{
RelationalExpression()
[
{code.append(" = ");}
<EQ>
RelationalExpression()
|
{code.append(" <> ");}
<NE>
RelationalExpression()
]
}

void RelationalExpression() :
{}
{
AdditiveExpression()
[
{code.append(" <= ");}
<LE>
AdditiveExpression()
|
{code.append(" >= ");}
<GE>
AdditiveExpression()
|
{code.append(" < ");}
<LT>
AdditiveExpression()
|
{code.append(" > ");}
<GT>
AdditiveExpression()
]
}

void AdditiveExpression() :
{}
{
MultiplicativeExpression()
(
{code.append(" + ");}
<PLUS>
MultiplicativeExpression()
|
{code.append(" - ");}
<MINUS>
MultiplicativeExpression()
)*
}

void MultiplicativeExpression() :
{}
{
InExpression()
(
{code.append(" * ");}
<MULTIPLY>
InExpression()
|
{code.append(" / ");}
<DIVIDE>
InExpression()
)*
}

void InExpression() :
{}
{
PrimaryExpression()
[


[
{code.append(" not ");}
<NOT>
]


{code.append(" in ");}
<IN>
ArrayExpression()
]
}

void ArrayExpression() :
{}
{
{code.append("(");}
<KLAMMERAUF>
RangeExpression()
(
{code.append(", ");}
<KOMMA>
RangeExpression()
)*
{code.append(")");}
<KLAMMERZU>
}

void RangeExpression() :
{}
{
PrimaryExpression()
[
{code.append(" - ");}
<MINUS>
PrimaryExpression()
]
}


void PrimaryExpression() :
{}
{
(
Identifier()
|
<TABLE>
Identifier()
<KLAMMERAUF>
Identifier()
<EQ>
Identifier()
<KLAMMERZU>
|
Constant()
|
{code.append("(");}
<KLAMMERAUF> Expression() <KLAMMERZU>
{code.append(")");}
)
}

void Identifier() :
{
Token t;
}
{
(
t = <IDENTIFIER>
{code.append(t.image);}
{featureList.add(t.image);}
)
}

void Constant() :
{
Token t;
}
{
(
t = <INTEGER_LITERAL>
{code.append(t.image);}
|
t = <FLOATING_POINT_LITERAL>
{code.append(t.image);}
|
t = <STRING_LITERAL>
{code.append(t.image);}
)
}

[/WR]


To test:

my input String is :

MMTAA524 in ('N','W') and MMTAA540 == 'g4' and MMTAA540 == ANYVALUE and MMTAA540 == NOVALUE AND MMTQQ = S45 AND MMTQW = blabla and MMPAA145


Syntax check is correct.

But i expect some errors.

MMTAA540 == ANYVALUE and MMTAA540 == NOVALUE AND MMTQQ = S45 AND MMTQW = blabla and MMPAA145

should output some errors.

How can i get some errors?

MMTAA540 == ANYVALUE and MMTAA540 == NOVALUE should be correct.

MMTAA540 == 'asdfasdfsd' should be correct

MMTAA540 == asdfasdfsd should be incorrect (error)

MMPAA540 hat no operation should be incorrect (error).

Now i input the java code with the correct format. @Knilch: some tipps for me! thank you very much.
 
Das sind keine Java-Tags und somit ist das Ganze immer noch unlesbar.
Thats not Java-Tag. But your Problem is, that you don't difference between Uppercase and Lowercase Letters. Lowercase must be between ' , Uppercase not
 
[OT]@lawila: Vielleicht deswegen, weil (mal abgesehen vom Titel) der einzige Satz den der TO geschrieben hat grammatikalisch ehere Denglisch als Englisch ist? [/OT]
 

Zurück
Oben