获取声明的左值和右值
getting lvalue and rvalue of a declaration
我正在用 ANTLR4 语法解析 C++,我有 visitDeclarationStatement
的访问者函数。在我试图解析 Person p;
或任何自定义类型的声明的 C++ 代码中,在树中我得到两个相似的节点,我无法区分左值和右值!
"declarationStatement": [
{
"blockDeclaration": [
{
"simpleDeclaration": [
{
"declSpecifierSeq": [
{
"declSpecifier": [
{
"typeSpecifier": [
{
"trailingTypeSpecifier": [
{
"simpleTypeSpecifier": [
{
"theTypeName": [
{
"className": [
{
"type": 128,
"text": "Person"
}
]
}
]
}
]
}
]
}
]
}
]
},
{
"declSpecifier": [
{
"typeSpecifier": [
{
"trailingTypeSpecifier": [
{
"simpleTypeSpecifier": [
{
"theTypeName": [
{
"className": [
{
"type": 128,
"text": "p"
}
]
}
]
}
]
}
]
}
]
}
]
}
]
},
{
"type": 124,
"text": ";"
}
]
}
我希望能够分别获取变量类型和变量名称。这样做的正确方法是什么?我怎样才能更改 g4 文件以通过区分类型和名称的方式获得这些结果?
谢谢
您不需要更改语法。在 Person p;
的情况下,匹配的是:
declSpecifierSeq: declSpecifier+ attributeSpecifierSeq?;
declSpecifierSeq
的第一个 child(即 declSpecifier+
)将是 declSpecifier-contexts 的 List
,其中第一个是类型和第二个名字。
我正在用 ANTLR4 语法解析 C++,我有 visitDeclarationStatement
的访问者函数。在我试图解析 Person p;
或任何自定义类型的声明的 C++ 代码中,在树中我得到两个相似的节点,我无法区分左值和右值!
"declarationStatement": [
{
"blockDeclaration": [
{
"simpleDeclaration": [
{
"declSpecifierSeq": [
{
"declSpecifier": [
{
"typeSpecifier": [
{
"trailingTypeSpecifier": [
{
"simpleTypeSpecifier": [
{
"theTypeName": [
{
"className": [
{
"type": 128,
"text": "Person"
}
]
}
]
}
]
}
]
}
]
}
]
},
{
"declSpecifier": [
{
"typeSpecifier": [
{
"trailingTypeSpecifier": [
{
"simpleTypeSpecifier": [
{
"theTypeName": [
{
"className": [
{
"type": 128,
"text": "p"
}
]
}
]
}
]
}
]
}
]
}
]
}
]
},
{
"type": 124,
"text": ";"
}
]
}
我希望能够分别获取变量类型和变量名称。这样做的正确方法是什么?我怎样才能更改 g4 文件以通过区分类型和名称的方式获得这些结果?
谢谢
您不需要更改语法。在 Person p;
的情况下,匹配的是:
declSpecifierSeq: declSpecifier+ attributeSpecifierSeq?;
declSpecifierSeq
的第一个 child(即 declSpecifier+
)将是 declSpecifier-contexts 的 List
,其中第一个是类型和第二个名字。