使用Flex检测和跳过行注释 [英] Detecting and skipping line comments with Flex

查看:173
本文介绍了使用Flex检测和跳过行注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Flex中检测到 // 之类的行注释并跳过这些行?

How can I detect one line comments like // in Flex and skip those lines?

此外,对于 / * 评论,下面的代码片段足够吗?

Also, for /* comments, will the following snippet be enough?

   "/*"         { comment(); }
  %%

    comment()
    {
        char c, c1;
    loop:
        while ((c = input()) != '*' && c != 0)
            putchar(c);

        if ((c1 = input()) != '/' && c != 0)
        {
            unput(c1);
            goto loop;
        }

        if (c != 0)
            putchar(c1);
    }


推荐答案

为什么不只是使用正则表达式识别注释? lex / flex 的全部要点是使您不必手动编写词法扫描器。您提供的代码应该可以工作(如果将模式 / * 放在行的开头),但是它有点难看,并且不知道它是否可以工作

Why don't you just use regular expressions to recognize the comments? The whole point of lex/flex is to save you from having to write lexical scanners by hand. The code you present should work (if you put the pattern /* at the beginning of the line), but it's a bit ugly, and it is not obvious that it will work.

您的问题是您想跳过注释,但是您提供的代码使用 putchar()打印注释,除了开头的 / * 。您想做什么?如果您想回显评论,则可以使用 ECHO 操作,而不执行任何操作。

Your question says that you want to skip comments, but the code you provide uses putchar() to print the comment, except for the /* at the beginning. Which is it that you want to do? If you want to echo the comments, you can use an ECHO action instead of doing nothing.

以下是正则表达式:

单行注释

这很容易,因为在lex中/ flex,与换行符不匹配。因此,以下内容将从 // 匹配到该行的末尾,然后不执行任何操作。

This one is easy because in lex/flex, . won't match a newline. So the following will match from // to the end of the line, and then do nothing.

"//".*                                    { /* DO NOTHING */ }

多行注释

这有点棘手,并且 * 既是正则表达式又是注释标记的关键部分使以下正则表达式有点难以阅读。我使用 [*] 作为可识别字符 * 的模式;在flex / lex中,可以改用 * 。使用更易读的内容。本质上,正则表达式匹配以(kbd> * (字符串)结尾的字符序列,直到找到下一个字符为 / 的字符为止。换句话说,它与C代码具有相同的逻辑。

This is a bit trickier, and the fact that * is a regular expression character as well as a key part of the comment marker makes the following regex a bit hard to read. I use [*] as a pattern which recognizes the character *; in flex/lex, you can use "*" instead. Use whichever you find more readable. Essentially, the regular expression matches sequences of characters ending with a (string of) * until it finds one where the next character is a /. In other words, it has the same logic as your C code.

[/][*][^*]*[*]+([^*/][^*]*[*]+)*[/]       { /* DO NOTHING */ }

以上要求终止的 * / ;未终止的注释将迫使词法分析器备份到注释的开头,并接受其他标记,通常是 / 除法运算符。那可能不是您想要的,但是要从未终止的评论中恢复并不容易,因为没有真正好的方法可以知道评论应该在哪里结束。因此,我建议添加一条错误规则:

The above requires the terminating */; an unterminated comment will force the lexer to back up to the beginning of the comment and accept some other token, usually a / division operator. That's likely not what you want, but it's not easy to recover from an unterminated comment since there's no really good way to know where the comment should have ended. Consequently, I recommend adding an error rule:

[/][*][^*]*[*]+([^*/][^*]*[*]+)*[/]       { /* DO NOTHING */ }
[/][*]                                    { fatal_error("Unterminated comment"); }

这篇关于使用Flex检测和跳过行注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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