0

How keywords are defined in C++? Are they from a specific data type or they are like a class object?

Since I started studying programming languages I have noticed that each one of them contain keywords and that they are special words reserved for that sepcific programming language, which cannot be used by themselves as variable names , or class names for example. It's always exciting to run a for loop, or a while loop, or an if statement on my own and get no bug at all, it's satisfactory. Leaving this apart, since I am a curious person, I always wondered what is the algorythm behind each one of those keywords. It's like, is threre a header file and cpp file for example that defines how those keywords work behind the scenes? If is there a file where they are defined, where are they? How a keyword recognizes a symbol as being of its type? I wish I could know. I have been trying to ask Google and it seems that I cannot formulate the question in such that it gives me fair response, it always comes to the keywords related to a specific language, and how they are created I've never known.

6th Apr 2024, 10:34 AM
Helton Rodrigues
Helton Rodrigues - avatar
1 Réponse
+ 2
Your question is essentially this: how does the compiler work? https://en.m.wikipedia.org/wiki/Compiler The job of the compiler is to translate the source code, that is written by the programmer, to machine code that can be executed by the system. This has several steps, including: - lexer, which recognizes the different symbols and language elements, including keywords, turning them into "tokens" - parser, which interprets the sequence of tokens according to the language grammar, building up an Abstract Syntax Tree (AST) - in further steps the compiler can do other things like type checking and code optimization, before creating the machine code according to the specific computer architecture. Keywords are fundamental building blocks of the language, and they should be recognized at the parsing stage, to build different code patterns. Understanding or analysing compilers can be very challenging, but also rewarding. https://gcc.gnu.org/git.html
6th Apr 2024, 1:09 PM
Tibor Santa
Tibor Santa - avatar