0

Given the following statement and ignoring any grammar concerns, what might be the most 'pure' way to parse the following SQL statement?

SELECT a, b AS x FROM tbl AS z

My thinking is something along the following:

               selectStatement
                     |
                  SELECT
                  /     \
            selectList  FROM
              /   \          \
     selectItem   selectItem  tableExpression
          |          /  \         |
          a         b   AS       tableItem
                         |     /     \
                         x  tbl      AS
                                      \
                                       z

Is that a more or less accurate representation of this? If not, where could it be improved? Sometimes I have a difficult time determining whether a keyword should be a parse node or not (for example should AS be a separate node, or should there just be some sort of label on the alias node, and are 'labels' valid in a parse tree?).

carl.hiass
  • 1,526
  • 1
  • 6
  • 26
  • Something like that is the way to go. I wouldn't have a separate AS node, just add that information to the selectitem node. – jarlh Aug 25 '22 at 19:30
  • The AST should be lossless and unambiguous. To me, it's just not worth the effort to create an equivalent representation to the CST, more chances to get it wrong. I rather have a description of the semantics that are associated directly to the CST. – kaby76 Aug 25 '22 at 19:38
  • @kaby76 I see. So for the CST, would the above look 'correct' ? – carl.hiass Aug 25 '22 at 19:55
  • 1
    No, that's not a CST. A CST is a derivation using the grammar. It's an AST because "SELECT" is a string right in the input, and "SELECT" cannot have children. For an AST, it looks okay. – kaby76 Aug 25 '22 at 20:22

0 Answers0