Search code examples
sqlitecompiler-constructiongrammarbisonbnf

SQLite source code parse.y - nm


I am reading the grammar of SQLite and having a few questions about the following paragraph.

// The name of a column or table can be any of the following:
//
%type nm {Token}
nm(A) ::= id(A). 
nm(A) ::= STRING(A). 
nm(A) ::= JOIN_KW(A).

The nm has been used quite widely in the program. The lemon parser documentation said

Typically the data type of a non-terminal is a pointer to the root of a parse-tree structure that contains all information about that non-terminal

%type   expr  {Expr*}
  1. Should I understand {Token} actually stands for a syntactic grouping which is a non-terminal token that "is a parse-tree structure that contains all.."?

  2. What is nm short for in this same, is it simply "name"?

  3. What is the period sign (dot .) that each nm(A) declaration ends up with?


Solution

    1. No, you should understand that Token is a C object type used for the semantic value of nms. (It is defined in sqliteInt.h and consists of a pointer to a non-null terminated character array and the length of that array.)
    2. The comment immediately above the definition of nm starts with the words "the name", which definitely suggests to me that nm is an abbreviation for "name", yes. That is also consistent with its semantic type, as above, which is basically a name (or at least a string of characters).

    3. All lemon productions end with a dot. It tells lemon where the end of the production is, like semicolons​ indicate to a C compiler where the end of a statement is. This makes it easier to parse consecutive productions, since otherwise the parser would have to look several symbols ahead to see the ::=