Why lex & yacc think this is a syntax error?

From: Wen Yi <896634148(at)qq(dot)com>
To: pgsql-general <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Why lex & yacc think this is a syntax error?
Date: 2023-06-09 09:55:23
Message-ID: tencent_72D2182E477528ABD7A638907F710A6E2208@qq.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi team,
I am learning the yacc &amp; lex and want to use it to make a config parser, as you can see, the config file is following these rules:

key = value

For example:

memory_limit = 512
memory_unit = 'MB'

time_limit = 12
time_unit = 's'

And I make such a yacc &amp; lex rule file:

/*
&nbsp;&nbsp; &nbsp;config.y
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; The yacc rule file to analyse the config string
&nbsp;&nbsp; &nbsp;Wen Yi
*/
%{
#include <stdio.h&gt;
int yylex();
int yyerror(char *s);
%}
%token NUMBER STRING
%token TOKEN
%token EQUAL
%token NEWLINE
%union
{
&nbsp;&nbsp; &nbsp;double number;
&nbsp;&nbsp; &nbsp;char *string;
&nbsp;&nbsp; &nbsp;char *token;
}

%%

Rule:
| TOKEN EQUAL NUMBER NEWLINE { printf("%s = %f\n", $1.token, $3.number);&nbsp; }
| TOKEN EQUAL STRING NEWLINE { printf("%s = %s\n", $1.token, $3.string); }
;

%%

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

/*
&nbsp;&nbsp; &nbsp;config.l
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; The lex rule file to parse the config string
&nbsp;&nbsp; &nbsp;Wen Yi
*/
/*
&nbsp;&nbsp; &nbsp;Rule
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; Setting = 'value' (string)
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; Setting = value (numbder)
*/
%option noyywrap
%option noinput
%{
#include <stdio.h&gt;
#include <string.h&gt;
#include "y.tab.h"
%}

%%

&nbsp;&nbsp; &nbsp;/*
&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Number
&nbsp;&nbsp;&nbsp;&nbsp; */

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

&nbsp;&nbsp; &nbsp;/*
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; String
&nbsp;&nbsp; &nbsp;*/

'.+' {
&nbsp;&nbsp; &nbsp;size_t length = strlen(yytext);
&nbsp;&nbsp; &nbsp;yytext[length - 1] = '\0';
&nbsp;&nbsp; &nbsp;yytext[0] = '\0';
&nbsp;&nbsp; &nbsp;memmove(yytext, yytext + 1, length - 1);
&nbsp;&nbsp; &nbsp;yylval.string = strdup(yytext);
&nbsp;&nbsp; &nbsp;return STRING;
}
"''" {
&nbsp;&nbsp; &nbsp;yylval.string = strdup("");
&nbsp;&nbsp; &nbsp;return STRING;
}

&nbsp;&nbsp; &nbsp;/*
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; Common Token
&nbsp;&nbsp; &nbsp;*/

[a-zA-z0-9_]+ { yylval.string = strdup(yytext);return TOKEN; }

&nbsp;&nbsp; &nbsp;/*
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; Equal =
&nbsp;&nbsp; &nbsp;*/

"=" { return EQUAL; }

&nbsp;&nbsp; &nbsp;/*
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; New line
&nbsp;&nbsp; &nbsp;*/
\n { return NEWLINE; }

&nbsp;&nbsp; &nbsp;/*
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; Other things
&nbsp;&nbsp; &nbsp;*/

. { ; }

%%

I think I have ignored the extra characters, but when I running it:

[beginnerc(at)bogon config]$ make config
lex config.l
yacc -d config.y
gcc lex.yy.c y.tab.c
[beginnerc(at)bogon config]$ ./a.out
memory_limit = 512
memory_limit = 512.000000
memroy_unit = 'MB' &nbsp;
syntax error
[beginnerc(at)bogon config]$ ./a.out
memory_unit = 'MB'
memory_unit = MB
memory_limit = 512
syntax error
[beginnerc(at)bogon config]$

Can someone give me some advice? I really don't know why the 'syntax error' message showed.

Thanks in advance!

Yours,
Wen Yi

Browse pgsql-general by date

  From Date Subject
Next Message Erik Wienhold 2023-06-09 10:32:33 Re: How to securely isolate databases/users in a multi-tenant Postgresql?
Previous Message Ajin Cherian 2023-06-09 09:51:14 Re: Support logical replication of DDLs