如何在 ANTLR 4 中创建可以捕获不同类型词法错误的词法分析器 [英] How to create a lexical analyzer in ANTLR 4 that can catch different types of lexical errors

查看:33
本文介绍了如何在 ANTLR 4 中创建可以捕获不同类型词法错误的词法分析器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 ANTLR 4 创建我的词法分析器,但我不知道如何创建一个词法分析器来捕获不同类型的词法错误.

I am using ANTLR 4 to create my lexer, but I don't how to create a lexical analyzer that catches different types of lexical errors.

例如:

  1. 如果我有一个像 ^ 这样无法识别的符号,词法分析器应该会报告这样的错误无法识别的符号^""

  1. If I have an unrecognized symbol like ^ the lexical analyzer should a report an error like this "Unrecognized symbol "^" "

如果我有一个像 2n 这样的无效标识符,词法分析器应该报告这样的错误标识符2n"必须以字母开头"

If I have an invalid identifier like 2n the lexical analyzer should report an error like this "identifier "2n" must begin with a letter"

请你帮帮我.

推荐答案

为每个已知错误创建一个错误标记规则,并在末尾创建一个catchall"错误标记规则,如下所示:

Create an error token rule for each known error and an "catchall" error token rule at the end like this:

// valid tokens first!
Number : [0-9]+;
Identifier : [a-zA-Z] [a-zA-Z0-9]*;
//...

// "error" tokens
// don't use these tokens in your grammar; They will show up as extraneous tokens during parsing and can be handled if desired.
InvalidIdentifier : [0-9]([0-9a-zA-Z])+; 
ACommonInvalidToken : '^'; // if you want to be more specific for certain cases
// add more to address common mistakes

UnknownToken : . ; // the "catch-all" error token; be sure not to be too greedy...

这篇关于如何在 ANTLR 4 中创建可以捕获不同类型词法错误的词法分析器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆