字符串没收尾,错误报在哪

👁️ 2 人浏览 💬 0 人评论 ❤️ 添加收藏

下面这段源码的第二行有个字符串忘了收尾引号。输出扫描器报错的位置,格式 行:列

let a = 1;
let b = "no end;
let c = 2;

玩具语言的词法规则:关键字 let if else while;标识符以字母或下划线开头;数字是连续的数位;字符串用一对双引号括起来(不含转义);双字符运算符 >= <= == !=;单字符运算符 + - * / = < > ; ( );空白只做分隔。

KEYWORDS = ("let", "if", "else", "while")
TWO = ("==", "!=", ">=", "<=")
ONE = "+-*/=<>;()"
SRC = 'let rate = 12;\nlet msg = "hi there";\ntotal = rate >= 3;'

BAD = 'let a = 1;\nlet b = "no end;\nlet c = 2;'
line, col, i = 1, 1, 0
while i < len(BAD):
    c = BAD[i]
    if c == "\n":
        line += 1; col = 1; i += 1; continue
    if c == '"':
        j = BAD.find('"', i + 1)
        nl = BAD.find("\n", i + 1)
        if j < 0 or (nl >= 0 and nl < j):
            print(str(line) + ":" + str(col)); break
    col += 1; i += 1
提交你的答案
请登录后提交答案。
去登录
代码编辑器
Ctrl + Enter 运行
本次输入:
输出:

                        
👩‍🏫
AI
💬 题目评论

全部评论