如何在flex中找到除少数几个单词以外的所有单词? [英] How to find all word except a few in flex?

查看:128
本文介绍了如何在flex中找到除少数几个单词以外的所有单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在flex中找到除少数几个单词(例如"car","bus","train")以外的所有单词.您有任何想法怎么做吗?

I need to find all words except a few (e.g. "car", "bus", "train") in flex. Have you got any ideas how to do it?

推荐答案

这个问题已经有一段时间没有得到回答了,这是lex/flex新用户在启动时遇到的一个标准问题.关于SO的其他地方也没有明确的答案.

This has been left unanswered for some time, and is a standard problem that new users of lex/flex encounter when starting. There is also not a clear answer elsewhere on SO.

问题是询问您如何匹配所有非保留字的标识符.这是几乎所有编译器中都存在的标准计算机语言功能.只是表达方式不明确.在使用flex的编译器构建中,我们必须找到除少数几个词外的所有词(也称为标识符)(例如"if","then","else").这是通过在规范中放入词汇模式的顺序来实现的.太普遍了, Flex上的Wikipedia页面上的主要示例显示了这一点!

The question is asking how do you match all identifiers that are not reserved words. This is a standard computer language feature that occurs in almost every compiler. It just was not expressed in a way that made that clear. In compiler construction, using flex, we have to find all words (also known as identifiers) except a few (e.g. "if", "then", "else"). This is achieved by the order in which the lexical patterns are put in the specification. It is so common that the main example in the Wikipedia page on Flex shows this!

我们会这样:

car    return(CAR);
bus    return(BUS);
train    return(TRAIN);
[a-z]+   return(WORD);

但是,如果我们以不同的顺序执行此操作,则会收到错误消息:

However, if we did this in a different order we would get an error:

[a-z]+   return(WORD);
car    return(CAR);
bus    return(BUS);
train    return(TRAIN);

它将反对汽车",公共汽车"和火车"的匹配,因为它们已经包含在WORD中.

It would object to the matching of "car", "bus" and "train" as they are already included in WORD.

这种情况也发生在其他语言中.以符号"=",> =",>","==","++","+","+ ="等为例.我们还必须注意定义其词素的顺序确保没有错误.

This situation also occurs elsewhere in languages. Take for example the symbols "=", ">=", ">", "==", "++", "+", "+=" etc. We also have to be careful which order we define their lexemes in flex to ensure no errors.

这篇关于如何在flex中找到除少数几个单词以外的所有单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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