简体   繁体   English

在弹性词法分析器中与“ if”匹配

[英]Matching with “if” in flex lexical analyzer

I am trying to write a piece of code in flex that matches the "if" keyword. 我正在尝试在flex中编写与“ if”关键字匹配的一段代码。 When I write: 当我写:

if printf("Saw an if");

it matches but it also matches, for example if the input is abcdifcd , output becomes abcdSaw an ifcd , but I want to be invalid, ie the output must be abcifcd since it is invalid. 它匹配但也匹配,例如,如果输入为abcdifcd ,输出为abcdSaw一个ifcd ,但我想无效,即输出必须为abcifcd,因为它无效。 In other words, I want it to be valid only if the keyword "if" is found alone, for example I want if abcd or abcd if to be valid but ifa, aif etc. to be invalid. 换句话说,我希望仅在单独找到关键字“ if”的情况下才有效,例如,我希望abcdabcd是否有效,而ifa,aif等则无效。 How can I do this? 我怎样才能做到这一点?

You need to add a rule which matches "words", whatever you consider them to be. 您需要添加一个匹配“单词”的规则,无论您认为它们是什么。 Remember to put the rule for "if" first. 请记住首先放置“ if”规则。 For example: 例如:

"if"         printf("Saw a if");
[a-zA-Z]+    ECHO

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

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