Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java20 ambiguities and improvements -- unannReferenceType #4240

Open
kaby76 opened this issue Sep 15, 2024 · 0 comments
Open

Java20 ambiguities and improvements -- unannReferenceType #4240

kaby76 opened this issue Sep 15, 2024 · 0 comments

Comments

@kaby76
Copy link
Contributor

kaby76 commented Sep 15, 2024

This is another in a series of some ambiguities and improvements that I'm finding with the newest version of trparse with the --ambig option to display ambiguous parses.

Input: in.txt

Ambig trees:

$ trparse in.txt --ambig | trtree -a | grep in.txt.79
CSharp 0 in.txt success 0.9483059
in.txt.79: (start_ (compilationUnit (ordinaryCompilationUnit (topLevelClassOrInterfaceDeclaration (classDeclaration (normalClassDeclaration (CLASS "class") (typeIdentifier (Identifier "S")) (classBody (LBRACE "{") (classBodyDeclaration (classMemberDeclaration (fieldDeclaration (unannType (unannPrimitiveType (numericType (integralType (INT "int"))))) (variableDeclaratorList (variableDeclarator (variableDeclaratorId (Identifier "x")) (ASSIGN "=") (variableInitializer (expression (assignmentExpression (conditionalExpression (conditionalOrExpression (conditionalAndExpression (inclusiveOrExpression (exclusiveOrExpression (andExpression (equalityExpression (relationalExpression (shiftExpression (additiveExpression (multiplicativeExpression (unaryExpression (unaryExpressionNotPlusMinus (postfixExpression (primary (primaryNoNewArray (literal (IntegerLiteral "0"))))))))))))))))))))))) (SEMI ";")))) (RBRACE "}"))))) (topLevelClassOrInterfaceDeclaration (classDeclaration (normalClassDeclaration (CLASS "class") (typeIdentifier (Identifier "Test1")) (classBody (LBRACE "{") (classBodyDeclaration (classMemberDeclaration (methodDeclaration (methodModifier (PUBLIC "public")) (methodModifier (STATIC "static")) (methodHeader (result (VOID "void")) (methodDeclarator (Identifier "main") (LPAREN "(") (formalParameterList (formalParameter (unannType (unannReferenceType (unannArrayType (unannClassOrInterfaceType (typeIdentifier (Identifier "String"))) (dims (LBRACK "[") (RBRACK "]"))))) (variableDeclaratorId (Identifier "args")))) (RPAREN ")"))) (methodBody (block (LBRACE "{") (blockStatements (blockStatement (localVariableDeclarationStatement (localVariableDeclaration (localVariableType (unannType (unannReferenceType (unannClassOrInterfaceType (typeIdentifier (Identifier "S")))))) (variableDeclaratorList (variableDeclarator (variableDeclaratorId (Identifier "s")) (ASSIGN "=") (variableInitializer (expression (assignmentExpression (conditionalExpression (conditionalOrExpression (conditionalAndExpression (inclusiveOrExpression (exclusiveOrExpression (andExpression (equalityExpression (relationalExpression (shiftExpression (additiveExpression (multiplicativeExpression (unaryExpression (unaryExpressionNotPlusMinus (postfixExpression (primary (primaryNoNewArray (unqualifiedClassInstanceCreationExpression (NEW "new") (classOrInterfaceTypeToInstantiate (Identifier "S")) (LPAREN "(") (RPAREN ")")))))))))))))))))))))))) (SEMI ";"))) (blockStatement (statement (statementWithoutTrailingSubstatement (expressionStatement (statementExpression (methodInvocation (typeName (packageName (Identifier "System") (DOT ".") (packageName (Identifier "out")))) (DOT ".") (Identifier "println") (LPAREN "(") (argumentList (expression (assignmentExpression (conditionalExpression (conditionalOrExpression (conditionalAndExpression (inclusiveOrExpression (exclusiveOrExpression (andExpression (equalityExpression (relationalExpression (shiftExpression (additiveExpression (additiveExpression (multiplicativeExpression (unaryExpression (unaryExpressionNotPlusMinus (postfixExpression (primary (primaryNoNewArray (literal (StringLiteral "\"s.x=\""))))))))) (ADD "+") (multiplicativeExpression (unaryExpression (unaryExpressionNotPlusMinus (postfixExpression (expressionName (ambiguousName (Identifier "s")) (DOT ".") (Identifier "x"))))))))))))))))))) (RPAREN ")"))) (SEMI ";")))))) (RBRACE "}")))))) (RBRACE "}"))))))) (EOF ""))
in.txt.79: (start_ (compilationUnit (ordinaryCompilationUnit (topLevelClassOrInterfaceDeclaration (classDeclaration (normalClassDeclaration (CLASS "class") (typeIdentifier (Identifier "S")) (classBody (LBRACE "{") (classBodyDeclaration (classMemberDeclaration (fieldDeclaration (unannType (unannPrimitiveType (numericType (integralType (INT "int"))))) (variableDeclaratorList (variableDeclarator (variableDeclaratorId (Identifier "x")) (ASSIGN "=") (variableInitializer (expression (assignmentExpression (conditionalExpression (conditionalOrExpression (conditionalAndExpression (inclusiveOrExpression (exclusiveOrExpression (andExpression (equalityExpression (relationalExpression (shiftExpression (additiveExpression (multiplicativeExpression (unaryExpression (unaryExpressionNotPlusMinus (postfixExpression (primary (primaryNoNewArray (literal (IntegerLiteral "0"))))))))))))))))))))))) (SEMI ";")))) (RBRACE "}"))))) (topLevelClassOrInterfaceDeclaration (classDeclaration (normalClassDeclaration (CLASS "class") (typeIdentifier (Identifier "Test1")) (classBody (LBRACE "{") (classBodyDeclaration (classMemberDeclaration (methodDeclaration (methodModifier (PUBLIC "public")) (methodModifier (STATIC "static")) (methodHeader (result (VOID "void")) (methodDeclarator (Identifier "main") (LPAREN "(") (formalParameterList (formalParameter (unannType (unannReferenceType (unannArrayType (unannClassOrInterfaceType (typeIdentifier (Identifier "String"))) (dims (LBRACK "[") (RBRACK "]"))))) (variableDeclaratorId (Identifier "args")))) (RPAREN ")"))) (methodBody (block (LBRACE "{") (blockStatements (blockStatement (localVariableDeclarationStatement (localVariableDeclaration (localVariableType (unannType (unannReferenceType (unannTypeVariable (typeIdentifier (Identifier "S")))))) (variableDeclaratorList (variableDeclarator (variableDeclaratorId (Identifier "s")) (ASSIGN "=") (variableInitializer (expression (assignmentExpression (conditionalExpression (conditionalOrExpression (conditionalAndExpression (inclusiveOrExpression (exclusiveOrExpression (andExpression (equalityExpression (relationalExpression (shiftExpression (additiveExpression (multiplicativeExpression (unaryExpression (unaryExpressionNotPlusMinus (postfixExpression (primary (primaryNoNewArray (unqualifiedClassInstanceCreationExpression (NEW "new") (classOrInterfaceTypeToInstantiate (Identifier "S")) (LPAREN "(") (RPAREN ")")))))))))))))))))))))))) (SEMI ";"))) (blockStatement (statement (statementWithoutTrailingSubstatement (expressionStatement (statementExpression (methodInvocation (typeName (packageName (Identifier "System") (DOT ".") (packageName (Identifier "out")))) (DOT ".") (Identifier "println") (LPAREN "(") (argumentList (expression (assignmentExpression (conditionalExpression (conditionalOrExpression (conditionalAndExpression (inclusiveOrExpression (exclusiveOrExpression (andExpression (equalityExpression (relationalExpression (shiftExpression (additiveExpression (additiveExpression (multiplicativeExpression (unaryExpression (unaryExpressionNotPlusMinus (postfixExpression (primary (primaryNoNewArray (literal (StringLiteral "\"s.x=\""))))))))) (ADD "+") (multiplicativeExpression (unaryExpression (unaryExpressionNotPlusMinus (postfixExpression (expressionName (ambiguousName (Identifier "s")) (DOT ".") (Identifier "x"))))))))))))))))))) (RPAREN ")"))) (SEMI ";")))))) (RBRACE "}")))))) (RBRACE "}"))))))) (EOF ""))
09/15-11:00:50 ~/issues/g4-current/java/java20/Generated-CSharp
$

unannReferenceType/unannClassOrInterfaceType/unannTypeVariable

unannReferenceType
    : unannClassOrInterfaceType
    | unannTypeVariable
    | unannArrayType
    ;

unannClassOrInterfaceType
    : (packageName '.' annotation*)? typeIdentifier typeArguments? uCOIT?
    ;

unannTypeVariable
    : typeIdentifier
    ;

From the JLS20,

Notes

  1. The two parse trees for Decision d=79 (rule unannReferenceType) are ambiguous for alts 1 and 2 at "S" in input "S s = new S();. "S" in either alt is a typeIdentifier -> Identifier. It's all the same thing: a dotIdChain: identifier ('.' identifier)*;`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant