简体   繁体   English

为什么我在这个 flex 文件中变得糟糕 output ?

[英]Why I am getting bad output in this flex file?

I am trying to write a Flex file that allows to read systemctl output.我正在尝试编写一个允许读取systemctl output 的 Flex 文件。 The idea is to show only those service that have failed to start.这个想法是只显示那些未能启动的服务。 My code is:我的代码是:

%{
    #include <iostream>
    #include <fstream>

    using namespace std;

    ifstream file;
    char* service_name;
    int nfs=0;
%}

failed_service      *failed*
                     
%%
                                                           
failed_service          {nfs++;cout << yytext << endl;}  
  
%%


int main()
{   
    
    system("systemctl > /var/tmp/system_start.txt");
    file.open("/var/tmp/system_start.txt");
    
    yyFlexLexer strm (&file,0);   
    strm.yylex();
}

Can you tell me where I am failing?你能告诉我我哪里失败了吗? The output show me the entire systemctl output Thanks. output 向我展示了整个系统ctl output谢谢。

Flex is probably not the best tool for this task. Flex 可能不是完成这项任务的最佳工具。 I'd suggest the grep command-line utility.我建议使用grep命令行实用程序。

Flex is a tool used in writing parsers; Flex 是用于编写解析器的工具; its goal is to split the input into a sequence of tokens .它的目标是将输入拆分为一系列标记 It never searches for a pattern.它从不搜索模式。 At each input point, it tries all the configured patterns to find a match starting at that point, and selects the longest possibility.在每个输入点,它会尝试所有配置的模式以找到从该点开始的匹配,并选择最长的可能性。 It then executes the associated action, and then continues after advancing to the first character following the match.然后它执行相关的动作,然后在前进到匹配后的第一个字符后继续。

If it any point it fails to match the current input character, it uses a default rule whose pattern matches any single character, and whose action is to print the character.如果它在任何时候都无法匹配当前输入字符,它会使用一个默认规则,其模式匹配任何单个字符,并且其操作是打印该字符。 Since your only rule never matches, the entire input is copied to the output one character at a time using this default rule.由于您唯一的规则永远不会匹配,因此使用此默认规则一次一个字符将整个输入复制到 output。

It is not in any way line oriented.它绝不是面向线的。 You can split the input into lines by using an explicit pattern, but none of that happens automatically.您可以使用显式模式将输入拆分为行,但这些都不会自动发生。

If you are going to use flex, you'll want to learn at least a little bit about regular expressions .如果你打算使用 flex,你至少需要学习一点正则表达式 *failed* is not a valid regular expression; *failed*不是一个有效的正则表达式; in a regular expression, the * operator means zero or more repetitions of the thing it follows, so it cannot appear at the beginning of a pattern.在正则表达式中, *运算符表示其后面的内容的零次或多次重复,因此它不能出现在模式的开头。 (The second * in *failed* is valid but means "zero or more d s", which is not what you meant. You might be confusing regular expressions with shell pathname expansion ("globbing") in which a * means "zero or more characters other than / ". *failed*中的第二个*有效,但表示“零个或多个d s”,这不是您的意思。您可能会将正则表达式与 shell 路径名扩展(“globbing”)混淆,其中*表示“零或/ "以外的更多字符。

In any case, *failed* is not the pattern you are matching in that flex file.在任何情况下, *failed*都不是您在该弹性文件中匹配的模式。 The pattern is failed_service , which is a valid regular expression, which matches itself.模式是failed_service ,它是一个匹配自身的有效正则表达式。 You previously defined the rather unnecessary macro failed_service , but the flex syntax for expanding macros is {macro-name} .您之前定义了相当不必要的宏failed_service ,但扩展宏的弹性语法是{macro-name} Had you managed to expand that macro, flex would have reported an invalid pattern.如果你设法扩展了那个宏,flex 会报告一个无效的模式。 However, there is no need for macros in a flex file;但是,flex 文件中不需要宏。 you can simply place the pattern itself in the rule.您可以简单地将模式本身放在规则中。

There is a brief description of the syntax of regular expressions accepted by flex in the flex manual , but it assumes some basic familiarity with the concepts. flex 手册中对 flex 接受的正则表达式的语法有一个简短的描述,但它假设您对这些概念有一些基本的熟悉。 The manual also explains the flex file format and the matching algorithm.该手册还解释了 flex 文件格式和匹配算法。

Nonetheless, I really think you'd be better off just piping the output of systemctl through grep ( systemctl | grep -Fw failed , for example).尽管如此,我真的认为您最好将systemctl的 output 输送到grep (例如systemctl | grep -Fw failed )。 Grep , which is line oriented, is highly optimised for tasks like this, and does not require you to come up with a set of patterns which cover all possibilities. Grep是面向行的,针对此类任务进行了高度优化,并且不需要您想出一套涵盖所有可能性的模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM