Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Context-free LR(k) parsing of generics?

843793Nov 6 2002 — edited Apr 29 2003
Has anyone been able to develop a syntax-free LR(k) (for finite k) grammar for parsing generics? The proposed grammar in spec8.pdf is hideous, as it has several ambiguities and even some typos. The two biggest problems I have in a context-free parser is deciding whether >> should be one or two tokens, and whether a<b starts a parameterized type name or is a relational expression. I know these two problems can be solved by writing a context-sensitive parser, but I would prefer not to do that if possible. I assume (without looking at the sources, because I'm trying to do a clean-room implementation) that the prototype compiler is using a context-sensitive parser.

I also am interested if any of the JSR folks would be willing to let the public know where the current syntax of the JSR or Java 1.5 differs from the ideas in the public draft. Remember the last minute change of the assert syntax (from "assert(a, b)" to "assert a:b"), 11 months after the public draft closed, made in order to acheive LR(1) parsing?

I've come close to writing an LR(2) grammar that gets everything except for casts. And I may even be able to trim that to LR(1) without too much headache. The problem with casts, however, is shown below:

Suppose an LR(2) parser is at "a" on this sequence:
class A { void m() { Object o = ( a < b

We could reduce "a" to ClassOrInterface, and ultimately have a cast expression, as in:
class A { void m() { Object o = ( a < b > ) null; } }

CastExpression : ( ReferenceType ) UnaryExpressionNotPlusMinus
ReferencType : ClassOrInterfaceType
ClassOrIterfaceType : ClassOrInterface TypeArguments
ClassOrInterface : Identifier(a)
TypeArguments : < ReferenceTypeList >
ReferenceTypeList : ReferenceType
ReferenceType : ClassOrInterfaceType
ClassOrInterfaceType : ClassOrInterface
ClassOrInterface : Identifier(b)

Or we could reduce "a" to Name, and ultimately have some expression, as in:
class A { void m() { Object o = ( a < b ) + ""; } }

AdditiveExpression : AdditiveExpression + MultiplicativeExpression
AdditiveExpression : MultiplicativeExpression
MultiplicativeExpression : UnaryExpression
UnaryExpression : UnaryExpressionNotPlusMinus
UnaryExpressionNotPlusMinus : PostfixExpression
PostfixExpression : Primary
Primary : ( Expression )
Expression : AssignmentExpression
AssignmentExpression : ConditionalExpression
ConditionalExpression : ConditionalOrExpression
ConditionalOrExpression : ConditionalAndExpression
ConditionalAndExpression : InclusiveOrExpression
InclusiveOrExpression : ExclusiveOrExpression
ExclusiveOrExpression : AndExpression
AndExpression : EqualityExpression
EqualityExpression : RelationalExpression
RelationalExpression : RelationalExpression < ShiftExpression
RelationalExpression : ShiftExpression
ShiftExpression : AdditiveExpression
AdditiveExpression : MultiplicativeExpression
MultiplicativeExpression : UnaryExpression
UnaryExpression : UnaryExpressionNotPlusMinus
UnaryExpressionNotPlusMinus : PostfixExpression
PostfixExpression : Name
Name : Identifier(a)
ShiftExpression : AdditiveExpression
AdditiveExpression : MultiplicativeExpression
MultiplicativeExpression : UnaryExpression
UnaryExpression : UnaryExpressionNotPlusMinus
UnaryExpressionNotPlusMinus : PostfixExpression
PostfixExpression : Name
Name : Identifier(b)

And since an infinite number of tokens can appear in both ReferenceTypeList and ShiftExpression, there is no finite way to determine which reduction to choose.

Comments

GhanaApexDeveloper

Hi Thomas,

your question is a bit vague.

Kindly provide more details -log ? from web server, what database apps are running from etc.

Thanks,

Benjamin.

1 - 1
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on May 27 2003
Added on Nov 6 2002
10 comments
210 views