C Question Mark Appearances

I am making a simple program that should take into account the occurrences of the ternary operator ?: in the source code of C. And I am trying to simplify this as much as possible. Therefore, I filtered out the following things from the source code:

  • String literals ""
  • Character constants ''
  • Sequences Trigraph: =, ?? (etc.
  • Comments
  • Macros

And now I am only counting the events of the question marks.

So my question is this: is there any other character, operator, or anything else that might cause the problem to contain '?'

Suppose the source syntax is valid.

+4
source share
3 answers

I think you found all the places where the question mark was entered, and therefore eliminated all possible false positives (for triple op). But perhaps you have eliminated too much: perhaps you want to count these "?:" That are introduced by macros; you do not count them. Is that what you intend? If so, everything is ready.

+4
source

Run the tool on pre-processed source code (you can get it by running, for example, gcc -E ). This would make all macro expansions (as well as #include substitution) and eliminate all trigrams and comments, so your work will become much easier.

+3
source

In K & R ANSI C, the only places where the question mark may be valid are:

  • String literals ""
  • Character constants ''
  • Comments

Now you may notice that there are no macros or trigram sequences in this list.

I did not include triran sequences, as they are a compiler extension, not a "valid C". I do not want you to delete the check from your program, I am trying to say that you have already gone further than necessary for ANSI C.

I also did not include macros, because when you talk about a character that can occur in macros, you can understand two things:

  • Macro Names / Identifiers
  • Macronutrients

What? the character cannot be found in macro definitions (http://stackoverflow.com/questions/369495/what-are-the-valid-characters-for-macro-names), and I see the particles as regular C code, so the first list ( string literals, character constants, and comments *) should also cover them.

* Can macros correctly contain comments? Because if I use this:

 #define somemacro 15 // this is a comment 

then // this is a comment not part of the macro. But what if I compile this C file with -D somemacro="15 // this is a comment" ?

-1
source

Source: https://habr.com/ru/post/1343911/


All Articles