a yacc problem, help!

B

betterking

I want to write a tiny sql parser with lex and yacc. At first I just
write sql.l sql.y with simple CREATE TABLE funciton.

sql.l

%{
#include "y.tab.h" //<>
#include <stdlib.h>
#include <string.h>
void yyerror(char *);
%}

%%
CREATE { return CREATE;}

TABLE { return TABLE;}


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

[a-z][a-z0-9]* {
strcpy(yylval.nameval,
yytext);
// fprintf(stderr,
"-----%s\n", *yytext);
return NAME;
}

[<>=] {
yylval.compval =
yytext[0];
return COMPARE;
}

[\n] { return *yytext;
}

[ \t] ;

.. {
yyerror("Unknown
Character!");
}
%%

int yywrap(void)
{
return 1;
}


sql.y:

%{
#include <stdio.h>
void yyerror(char *s);
int yylex(void);

%}

%union {
int intval;
char nameval[50];
char compval;
};

%token <intval> INTEGER
%token <nameval> NAME
%token <compval> COMPARE
%token CREATE TABLE

%%
createtablestatement:
| createtablestatement '\n'
| CREATE TABLE NAME '\n' {printf("%s\n", $3);}
;

%%
void yyerror(char *s)
{
fprintf(stderr," %s\n", s);
}

int main(void)
{
yyparse();
}


I compile and run it as follows:

lex sql.l
yacc -d sql.y
gcc -o create lex.yy.c y.tab.c -ll

$./create
CREATE TABLE aaaa
aaaa
CREATE TABLE bbbb
parse error
$

I want to know why it could run at the first time, but it could not
run at the second time.
Please help me. Thanks a lot!
 
N

Nick Keighley

I want to write a tiny sql parser with lex and yacc. At first I just
write sql.l sql.y with simple CREATE TABLE funciton.

<snip>

both yacc and sql are off-topic in comp.lang.c.
You need to try a compiler or unix ng for questions
about yacc.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,734
Latest member
manin

Latest Threads

Top