1+ /* src/pgsql_parser/pgsql_parser.y */
12%code requires {
23 namespace PgsqlParser {
34 struct AstNode ;
45 }
56 #include < string>
67}
78
9+
810%{ // C PROLOGUE - This code is now part of a C++ compilation unit
911#include " pgsql_parser/pgsql_parser.h" // For PgsqlParser::Parser context & yyscan_t
1012
@@ -36,15 +38,14 @@ int pgsql_yylex(union PGSQL_YYSTYPE* yylval_param, yyscan_t yyscanner, PgsqlPars
3638 PgsqlParser::AstNode* node_val;
3739}
3840
39- /* Declare tokens from the lexer */
4041%token TOKEN_SELECT TOKEN_FROM TOKEN_INSERT TOKEN_INTO TOKEN_VALUES
4142%token TOKEN_LPAREN TOKEN_RPAREN TOKEN_SEMICOLON
43+ %token TOKEN_ASTERISK
4244
4345%token <str_val> TOKEN_QUIT
4446%token <str_val> TOKEN_IDENTIFIER
4547%token <str_val> TOKEN_STRING_LITERAL
4648
47- /* Declare non-terminals and the type of their semantic value */
4849%type <node_val> statement
4950%type <node_val> simple_statement
5051%type <node_val> command_statement
@@ -53,9 +54,11 @@ int pgsql_yylex(union PGSQL_YYSTYPE* yylval_param, yyscan_t yyscanner, PgsqlPars
5354%type <node_val> identifier_node
5455%type <node_val> string_literal_node
5556%type <node_val> value_for_insert
57+ %type <node_val> select_list_item
58+ %type <node_val> optional_semicolon /* <<< NEW TYPE (though it produces no value for AST) */
5659
5760
58- %start query_list /* The start symbol of our grammar */
61+ %start query_list
5962
6063%%
6164
@@ -64,6 +67,12 @@ query_list:
6467 | query_list statement { /* Manages last statement's AST. */ }
6568 ;
6669
70+ /* NEW RULE for optional semicolon */
71+ optional_semicolon :
72+ TOKEN_SEMICOLON { $$ = nullptr ; /* Or some placeholder if needed, but usually not for this */ }
73+ | /* empty */ { $$ = nullptr ; } /* Allows nothing, produces no AST node for itself */
74+ ;
75+
6776statement :
6877 simple_statement { $$ = $1 ; if (parser_context) parser_context->internal_set_ast ($1 ); }
6978 | select_statement { $$ = $1 ; if (parser_context) parser_context->internal_set_ast ($1 ); }
@@ -75,9 +84,10 @@ simple_statement:
7584 ;
7685
7786command_statement :
78- TOKEN_QUIT TOKEN_SEMICOLON {
87+ TOKEN_QUIT optional_semicolon {
7988 $$ = new PgsqlParser::AstNode(PgsqlParser::NodeType::NODE_COMMAND, std::move(*$1 ));
8089 delete $1 ;
90+ // $2 (optional_semicolon) doesn't contribute a meaningful value here
8191 }
8292 ;
8393
@@ -99,11 +109,19 @@ string_literal_node:
99109 }
100110 ;
101111
112+ select_list_item :
113+ identifier_node { $$ = $1 ; }
114+ | TOKEN_ASTERISK {
115+ $$ = new PgsqlParser::AstNode(PgsqlParser::NodeType::NODE_ASTERISK, " *" );
116+ }
117+ ;
118+
102119select_statement :
103- TOKEN_SELECT identifier_node TOKEN_FROM identifier_node TOKEN_SEMICOLON {
120+ TOKEN_SELECT select_list_item TOKEN_FROM identifier_node optional_semicolon { /* MODIFIED */
104121 $$ = new PgsqlParser::AstNode(PgsqlParser::NodeType::NODE_SELECT_STATEMENT);
105122 $$ ->addChild ($2 );
106123 $$ ->addChild ($4 );
124+ // $5 (optional_semicolon) doesn't contribute
107125 }
108126 ;
109127
@@ -112,14 +130,16 @@ value_for_insert:
112130 ;
113131
114132insert_statement :
115- TOKEN_INSERT TOKEN_INTO identifier_node TOKEN_VALUES TOKEN_LPAREN value_for_insert TOKEN_RPAREN TOKEN_SEMICOLON {
133+ TOKEN_INSERT TOKEN_INTO identifier_node TOKEN_VALUES TOKEN_LPAREN value_for_insert TOKEN_RPAREN optional_semicolon {
116134 $$ = new PgsqlParser::AstNode(PgsqlParser::NodeType::NODE_INSERT_STATEMENT);
117135 $$ ->addChild ($3 );
118136 $$ ->addChild ($6 );
137+ // $8 (optional_semicolon) doesn't contribute
119138 }
120139 ;
121140
122141%%
123142
124143// pgsql_yyerror is defined in pgsql_parser.cpp with C linkage,
125144// and declared in pgsql_parser.h. Bison will generate a call to it.
145+
0 commit comments