473,386 Members | 1,817 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

a yacc problem, help!

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 <intvalINTEGER
%token <namevalNAME
%token <compvalCOMPARE
%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!
Oct 4 '08 #1
2 3097
On 4 Oct, 03:10, "betterk...@gmail.com" <betterk...@gmail.comwrote:
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.
--
Nick Keighley
Oct 6 '08 #2
<be********@gmail.comwrote in message
news:d4**********************************@x41g2000 hsb.googlegroups.com...
>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 <intvalINTEGER
%token <namevalNAME
%token <compvalCOMPARE
%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.
You want news:comp.compilers
This may be helpful:
http://www.google.com/search?rls=ig&...ammar&aq=f&oq=

** Posted from http://www.teranews.com **
Oct 6 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Profetas | last post by:
Hi. I was wondering how can I access the yacc functions? because when use a normal yacc function such as qstring { yylval.string = strdup(yytext+1); if(yylval.string !='"')...
3
by: Gvs | last post by:
Hi, i'm trying to implement a few things in lex and yacc though i'm having trouble. What i want to do is be able to parse a term and an expression. in EBNF my term is defined as: term ::=...
6
by: Volker Hetzer | last post by:
Hi! We are finding ourselves in a situation where we have to parse several more or less free format text files. In the past, on linux, we had flex and bison for generating very fast parsers for...
1
by: deepusrp | last post by:
Helo everyone, i am doing a project on some graphic tool using qt as the front end. i am using lex and yacc as parser. now i am facing a problem since yacc generates only c code as i want to...
13
by: Berk Birand | last post by:
Hi, I am working on a school project where we use lex/yacc to write a compiler for a fictional (Java-like) language. I have handled all the details about the yacc and lex files, but I still have...
2
by: Mohitz | last post by:
Hi Guys, I am facing a peculiar problem with my yacc script. Following is the snippet of the yacc script that i am using. I am using lex as the lexical analyzer. Sample Input :
1
by: neena.usenet | last post by:
I have a gui that reads in a file using C++/yacc. If the file has a parsing error, yacc throws the error to my program and I report it. The problem is at that point, I would like to try to open...
5
by: thomas | last post by:
hello I m writing a simple parser using bison. I just used someone's else c++ grammar, to produce a code beutifier. The thing is , most of the actions for productions would have form {$$ = $1 + $2...
0
by: Laszlo Nagy | last post by:
This is a fragment from my yacc file: import ply.yacc as yacc from lex import tokens from ast import * def p_msd(p): r"""msd : SCHEMA WORD LBRACE defs RBRACE """ p = MSDSchema(p)
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.