Mysql lexical analysis experiment (1)

1. Identification of keywords and identifiers

In the SQL language, certain words can serve as both keywords and identifiers (such as column or table names). Often, keywords have a specific meaning in a specific context and serve as ordinary identifiers in other contexts. For example, "COLUMN" is a keyword in the ALTER TABLE statement, but in other contexts it might just be an ordinary column name.

To handle this situation, the lexical analyzer is usually only responsible for identifying the type of a word (i.e. whether it is a keyword or an identifier), without regard to its context. In the subsequent grammatical analysis stage, the exact role of the word is determined based on its specific context.

2. Naming conflict problem

A word such as SELECTaaa should be recognized as an identifier if it is an identifier as a whole (such as a column or table name). However, if your SQL statement does have a column or table named SELECTaaa, this creates a naming conflict.

In this case, one solution is to follow a common convention of the SQL language: if an identifier conflicts with a keyword, then specific quotes (such as backticks, common in MySQL) can be used to make it clear that it is an identifier.

For example:

  • When SELECTaaa is used as a combination of keyword and identifier, it should be broken into SELECT and aaa.
  • When SELECTaaa is used as a complete identifier, it should be written `SELECTaaa`.

in conclusion

In actual SQL parsing, these problems are usually solved by combining lexical analysis and syntax analysis. The lexical analyzer is responsible for identifying word types, while the syntactic analyzer is responsible for further parsing the specific meaning of these words based on context. In some cases, it may also be necessary to consider the names of tables and columns that actually exist in the database.

Guess you like

Origin blog.csdn.net/qq_65052774/article/details/134387237