[Beginner Question]A question about yacc & lex
От | Wen Yi |
---|---|
Тема | [Beginner Question]A question about yacc & lex |
Дата | |
Msg-id | tencent_303FE91897A1BA9E5511D30648B8B826B707@qq.com обсуждение исходный текст |
Ответы |
Re: [Beginner Question]A question about yacc & lex
|
Список | pgsql-general |
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.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);
}
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
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
В списке pgsql-general по дате отправления: