提问人:Dj Sushi 提问时间:10/27/2023 更新时间:10/27/2023 访问量:56
如何在 ANTLR4 中测试我的解析器语法?
How do I test out my parser grammar in ANTLR4?
问:
我在 ANTLR4 中编写了一个解析器语法,如下所示:
parser grammar IFJ23;
tokens {
Identifier, Type,
LeftBracket, RightBracket,
LeftCurlyBracket, RightCurlyBracket,
Semicolon, Colon, Arrow, Comma,
/** Keywords */
FunctionKeyword
}
swiftFile
: functionDeclaration swiftFile
;
/** Statements */
statement
: declaration Semicolon?
;
statements
: statement statements?
;
codeBlock
: LeftCurlyBracket statements? RightCurlyBracket
;
/** Declarations */
declaration
: functionDeclaration
// | variableDeclaration
;
/** Function declarations */
functionDeclaration
: FunctionKeyword Identifier functionSignature functionBody
;
functionSignature
: parameterClause functionResult?
;
functionBody
: codeBlock
;
parameterClause
: LeftBracket RightBracket
| LeftBracket parameterList RightBracket
;
functionResult
: Arrow Type
;
parameterList
: parameter
| parameter Comma parameterList
;
parameter
: externalParameterName? localParameterName typeAnnotation
;
externalParameterName
: Identifier
;
localParameterName
: Identifier
;
typeAnnotation
: Colon Type
;
现在,我面临的问题是我已经有一个不是用 ANTLR4 编写的功能词法分析器。它是用 C 语言编写的,它为文件生成一个标记列表。
有没有办法以某种方式让 ANTLR4 接受此令牌列表作为输入并尝试生成解析树?
我还需要将列表中的标记与从词法分析器生成的相应标记类型进行匹配。Tokens {}
答: 暂无答案
评论
WhileKeyword
Identifier
Double
tokens
.g4