Question

On the internet I've read the general meaning of the adjective lexical: the meaning of a word in relation to the physical world or to abstract concepts, without reference to any sentence in which the word may occur.

I've seen this word been used in many different contexts: lexical scope, lexical analysis, lexical constants, lexical operators etc.

Why do people choose to use the word lexical in front of something like a scope? I don't see any relation between the definition of lexical and a lexical scope. Same goes for lexical analysis, lexical constants etc.

I understand that you want to give something a name so you can reference to it more easily, but why would you name it something that doesn't make sense? If it does make sense, then could you clarify how it does?


I've seen a different thread in which someone asks what lexical means in C++: What does the term "lexical" means in C++?

The top answer on that question explains that lexical means that it is related to the source code. Then my question would be: How is anything in programming not related to the source code? Isn't every kind of scope, constant, form of code analysis related to the source code? Also, how is the definition of lexical related to that of the relation to the source code?

Was it helpful?

Solution

The word "lexically" has several different meanings depending on context.

In the context of a compiler, "lexical" typically refers to individual tokens in a source file or the process of obtaining those tokens without any regard to the overall structure formed by those tokens. For example, in C, the input

int main() {
    printf("Hi!\n");
    return 0;
}

would be broken apart into

int
main
(
)
{
printf
(
"Hi!\n"
)
;
return
0
;
}

These would then be tagged with their token type, such as "identifier" or "open parenthesis."

During lexical analysis, there is (usually) no attempt to determine the overall meaning of those tokens at a broader scale. That's the job of parsing (syntax analysis) or further semantic analysis. This is similar to the definition you've given - lexical analysis is the determination of what the tokens are, but not what they mean in context.

Sometimes, "lexically" simply means "as it appears in the source code." For example, lexically scoped variables are variables that are scoped to the block in which they appear in a source file, rather than scoped based on the dynamic execution of that program. This is a bit of a misnomer.

Hope this helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top