Hi team,
I am learning the yacc & 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 & lex rule file:


/*
    config.y
        The yacc rule file to analyse the config 
string
    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@bogon config]$ make config
lex config.l
yacc -d config.y
gcc lex.yy.c y.tab.c
[beginnerc@bogon config]$ ./a.out
memory_limit = 512
memory_limit = 512.000000
memroy_unit = 'MB' &nbsp;
syntax error
[beginnerc@bogon config]$ ./a.out
memory_unit = 'MB'
memory_unit = MB
memory_limit = 512
syntax error
[beginnerc@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

Reply via email to