简体   繁体   中英

Defining sets C++

I'm implementing a syntax analyzer for Clite as a Programming Languages class assignment. 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.

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 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. Any ideas?

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

int isalpha ( int c );

Check if character is alphabetic

Also, check out the related functions: 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.

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? You can look into std::set if you ever need to do this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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