pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
[Beginner Question]A question about yacc & lex
2+ messages / 2 participants
[nested] [flat]

* [Beginner Question]A question about yacc & lex
@ 2023-06-02 00:47 Wen Yi <896634148@qq.com>
  2023-06-02 12:07 ` Re: [Beginner Question]A question about yacc & lex Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 2+ messages in thread

From: Wen Yi @ 2023-06-02 00:47 UTC (permalink / raw)
  To: pgsql-general <pgsql-general@lists.postgresql.org>

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



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

%{
#include <stdlib.h>
#include "y.tab.h"
%}

%%

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

\n { return END; }

%%


/*
    array.y
        Array program
    Wen Yi
*/

%{
#include <stdio.h>
int yylex();
int yyerror(char *s);
%}
%token NUMBER
%token END
%union 
{
    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;  }

%%

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


The running result is this:


[beginnerc@bogon code]$ ./a.out
1 2 3 4 5 6 
     = 21
1
syntax error



I don't know  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

^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: [Beginner Question]A question about yacc & lex
  2023-06-02 00:47 [Beginner Question]A question about yacc & lex Wen Yi <896634148@qq.com>
@ 2023-06-02 12:07 ` Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 2+ messages in thread

From: Tom Lane @ 2023-06-02 12:07 UTC (permalink / raw)
  To: Wen Yi <896634148@qq.com>; +Cc: pgsql-general <pgsql-general@lists.postgresql.org>

"=?ISO-8859-1?B?V2VuIFlp?=" <896634148@qq.com> writes:
> I don't know why are there many extra spaces in the output, and why the error message 'syntax error' is showed.

You didn't say exactly what you typed at it, but:

Your flex lexer lacks actions for many possible input characters,
notably spaces.  I seem to recall that the default action in
such cases is to print the character on stdout.

The grammar lacks any way to deal with any input after the
first newline.  Anything except EOF will draw a syntax error.

			regards, tom lane





^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2023-06-02 12:07 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2023-06-02 00:47 [Beginner Question]A question about yacc & lex Wen Yi <896634148@qq.com>
2023-06-02 12:07 ` Tom Lane <tgl@sss.pgh.pa.us>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox