简体   繁体   English

定义集C ++

[英]Defining sets C++

I'm implementing a syntax analyzer for Clite as a Programming Languages class assignment. 我正在为Clite实现一种语法分析器,作为一种编程语言类分配。 I'm using a recursive descent parser with a match() function that takes a string from an input scanner as an argument and checks if it is the expected token, else it returns an error message. 我正在使用具有match()函数的递归下降解析器,该函数从输入扫描器获取一个字符串作为参数,并检查它是否为预期的令牌,否则返回错误消息。

I've been trying to declare a set for the alphabet so I don't have to match each letter of the alphabet. 我一直在尝试为字母表声明一个集合,因此我不必匹配字母表中的每个字母。 Something like this: 像这样:

void letter(){
    if(currentToken==LETTER){
        match(LETTER);
    }
    else
        error();
}

I tried with #define but realized it wasn't working 我尝试使用#define尝试,但意识到它没有用

#define LETTER "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v" "w" "x" "y" "z"

Also tried with union or typedef but couldn't do it. 还尝试了union或typedef,但无法做到。 Any ideas? 有任何想法吗?

http://www.cplusplus.com/reference/cctype/isalpha/ http://www.cplusplus.com/reference/cctype/isalpha/

int isalpha ( int c ); int isalpha(int c);

Check if character is alphabetic 检查字符是否为字母

Also, check out the related functions: http://www.cplusplus.com/reference/cctype/ 另外,请查看相关功能: http : //www.cplusplus.com/reference/cctype/

By the way, what you were trying would never work, because 顺便说一句,您尝试的操作将永远无法工作,因为

1) == does not attempt to do things like 'is contained by' 'is subset of' etc, but demands that both its operands compare equal. 1)==不会尝试执行诸如“由...包含”,“是...的子集”等操作,而是要求其两个操作数相等。

2) The #define you are doing is not defining an array, or a container, or any other kind of data structure, so why would it work? 2)您正在执行的#define方法没有定义数组,容器或任何其他类型的数据结构,那么为什么它会起作用? You can look into std::set if you ever need to do this. 如果需要,您可以查看std :: set。

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

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