SA
i ask if any one can help me in this grammar problem :

Expression ::= Expression ( "&&" | "<" | "+" | "-" | "*" ) Expression
| Expression "[" Expression "]"
| Expression "." "length"
| Expression "." Identifier "(" ( Expression ( "," Expression )* )? ")"
| <INTEGER_LITERAL>
| "true"
| "false"
| Identifier
| "this"
| "new" "int" "[" Expression "]"
| "new" Identifier "(" ")"
| "!" Expression
| "(" Expression ")" Identifier ::= <IDENTIFIER>

this grammar contain left recursion and left factoring , i resolved left factoring

void Expression():
{
}
{
Expression()
( ( "&&" | "<" | "+" | "-" | "*" ) Expression()
| "[" Expression() "]"
| "." "length"
| "." Identifier() "(" ( Expression() ( "," Expression() )* )? ")" )
| (<NUMBER>)+
| "true"
| "false"
| Identifier()
| "this"
| <NEW> ("int" "[" Expression() "]"| Identifier() "(" ")")
| "!" Expression()
| "(" Expression() ")"
}
but left recursion no : i think it is now look like
A->A(B|C|E|D)
A->(B|C|E|D)expr2
expr2->(B|C|E|D)expr2*
but I'm not sure can any one confirm .

Thanks in advance Guys .

reply asap .

Recommended Answers

All 6 Replies

How does this relate to Java programming?
It looks like a Comp Sci problem.

really as I use Java CC i think it is Java if you don't mind :D

i think i solved it now :
void Expression():
{
}
{
Expression3() R()
}
void Expression2():
{}
{
(<NUMBER>)+
| "true"
| "false"
| Identifier()
| "this"
| <NEW> ("int" "[" Expression() "]"| Identifier() "(" ")")
| "!" Expression()
| "(" Expression() ")"
}
void Expression3():
{}
{
( "&&" | "<" | "+" | "-" | "*" ) Expression()
| "[" Expression() "]"
| "." "length"
| "." Identifier() "(" ( Expression() ( "," Expression() )* )? ")"
}

void R():{}
{
(Expression2()R())
}

Sorry, I had never heard of Java CC. I've never seen a student ask questions about it.

The last time I saw anything that looked like what you posted, I think we called it BNF.
As a grad student in the early 70s.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.