[Beginner Question]A question about yacc & lex

From: Wen Yi <896634148(at)qq(dot)com>
To: pgsql-general <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: [Beginner Question]A question about yacc & lex
Date: 2023-06-02 00:47:17
Message-ID: tencent_303FE91897A1BA9E5511D30648B8B826B707@qq.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi team,
now I'm learning the yacc &amp; lex to understand the principle of the postgres's parser.
And I write a test program as this:

/*
&nbsp;&nbsp; &nbsp;array.l
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; Array program
&nbsp;&nbsp; &nbsp;Wen Yi
*/
%option noyywrap
%option noinput

%{
#include <stdlib.h&gt;
#include "y.tab.h"
%}

%%

[0-9]+ { yylval.number = atoi(yytext); return NUMBER;}

\n { return END; }

%%

/*
&nbsp;&nbsp; &nbsp;array.y
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; Array program
&nbsp;&nbsp; &nbsp;Wen Yi
*/

%{
#include <stdio.h&gt;
int yylex();
int yyerror(char *s);
%}
%token NUMBER
%token END
%union
{
&nbsp;&nbsp; &nbsp;int number;
}

%%

end:
| array_list END { printf ("= %d\n", $1.number); }

array_list: array { $$.number = $1.number; }

array: number { $$.number = $1.number; }
| array number { $$.number = $1.number + $2.number; }

number: NUMBER { $$.number = $1.number;&nbsp; }

%%

int main(int argc, char *argv[])
{
&nbsp;&nbsp; &nbsp;yyparse();
}
int yyerror(char *s)
{
&nbsp;&nbsp; &nbsp;puts(s);
}

The running result is this:

[beginnerc(at)bogon code]$ ./a.out
1 2 3 4 5 6
&nbsp;&nbsp;&nbsp;&nbsp; = 21
1
syntax error

I don't know&nbsp; why are there many extra spaces in the output, and why the error message 'syntax error' is showed.
Can someone give me some advice?
Thanks in advance!

Yours,
Wen Yi

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2023-06-02 12:07:12 Re: [Beginner Question]A question about yacc & lex
Previous Message Andrew Dunstan 2023-06-01 20:46:07 Re: Adding SHOW CREATE TABLE