antlr / grammars-v4

Grammars written for ANTLR v4; expectation that the grammars are free of actions.
MIT License
10.22k stars 3.71k forks source link

Java20 ambiguities and improvements -- unannReferenceType #4240

Open kaby76 opened 2 months ago

kaby76 commented 2 months ago

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,

 
UnannReferenceType:
    UnannClassOrInterfaceType
    UnannTypeVariable
    UnannArrayType

UnannClassOrInterfaceType:
    UnannClassType
    UnannInterfaceType

UnannClassType:
    TypeIdentifier [TypeArguments]
    PackageName . { Annotation } TypeIdentifier [ TypeArguments ]
    UnannClassOrInterfaceType . { Annotation } TypeIdentifier [ TypeArguments ]

UnannInterfaceType:
    UnannClassType

UnannTypeVariable:
    TypeIdentifier

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: adotIdChain: identifier ('.' identifier)*;`.