473,395 Members | 1,516 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,395 software developers and data experts.

What's wrong with my lex and yacc program?

Dear all,

I am a newbie using lex and yacc.
I really don't know what's wrong with my code, because the output is not
what I want.
My yacc rules don't match, and yylex seems just read only one line of the
text file.

Please help...thx.

test file:
---------------------------------------
*******
SLICE 1,17
*
*******
*
VERSION : 3.2.5
---------------------------------------

test.h
---------------------------------------
#ifndef YYERRCODE
#define YYERRCODE 256
#endif

#define SLICE 257
#define VERSION 258
#define INTEGER 259
typedef union {
int iInteger;
char* szRecord;
} YYSTYPE;
extern YYSTYPE yylval;
---------------------------------------

lex:
---------------------------------------
%{
#include "test.h"
%}

comment "*".*
whitespace [ \t\r\n]+

%%

{whitespace} ;
{comment} ;
SLICE {yylval.szRecord=strdup(yytext);printf("In lex %s
",yylval.szRecord);return SLICE;}
VERSION {yylval.szRecord=strdup(yytext);printf("In lex %s
",yylval.szRecord);return VERSION;}
[0-9]+ {yylval.iInteger=atoi(yytext);printf("In lex..%d
",yylval.iInteger);return INTEGER;}
.. ;

%%
---------------------------------------

yacc:
---------------------------------------
%{
/* MEBES job file parser */
#include <stdio.h>
%}

%union {
int iInteger;
char* szRecord;
}

%token <szRecord> SLICE VERSION
%token <iInteger> INTEGER

%%

test : SLICE {printf("Got SLICE\n");}
| SLICE INTEGER ',' INTEGER {printf("In SLICE,no1=%d,no2=%d\n",$2,$4);}
;
test2 : VERSION {printf("%s \n",$1);}
;

%%

extern FILE *yyin;

int main(int argc,char** argv)
{
yyin=fopen(argv[1],"r");
if (yyin) {
while (!feof(yyin)) {
yyparse();
}
printf("Finish.........\n");
fclose(yyin);
return 0;
}
else {
return -1;
}
}

yyerror(char* errmsg)
{
printf("%s\n");
}
---------------------------------------
Nov 14 '05 #1
1 4235
"cylin" <cy***@avant.com.tw> writes:
I am a newbie using lex and yacc.


Your question is outside the domain of comp.lang.c, which discusses
only the standard C programming language, including the standard C
library. This is a remarkably narrow topic compared to what many
people expect.

For your convenience, the list below contains topics that are not
on-topic for comp.lang.c, and suggests newsgroups for you to explore
if you have questions about these topics. Please do observe proper
netiquette before posting to any of these newsgroups. In particular,
you should read the group's charter and FAQ, if any (FAQs are
available from www.faqs.org and other sources). If those fail to
answer your question then you should browse through at least two weeks
of recent articles to make sure that your question has not already
been answered.

* OS-specific questions, such as how to clear the screen,
access the network, list the files in a directory, or read
"piped" output from a subprocess. These questions should be
directed to OS-specific newsgroups, such as
comp.os.ms-windows.programmer.misc, comp.unix.programmer, or
comp.os.linux.development.apps.

* Compiler-specific questions, such as installation issues and
locations of header files. Ask about these in
compiler-specific newsgroups, such as gnu.gcc.help or
comp.os.ms-windows.programmer.misc. Questions about writing
compilers are appropriate in comp.compilers.

* Processor-specific questions, such as questions about
assembly and machine code. x86 questions are appropriate in
comp.lang.asm.x86, embedded system processor questions may
be appropriate in comp.arch.embedded.

* ABI-specific questions, such as how to interface assembly
code to C. These questions are both processor- and
OS-specific and should typically be asked in OS-specific
newsgroups.

* Algorithms, except questions about C implementations of
algorithms. "How do I implement algorithm X in C?" is not a
question about a C implementation of an algorithm, it is a
request for source code. Newsgroups comp.programming and
comp.theory may be appropriate.

* Making C interoperate with other languages. C has no
facilities for such interoperation. These questions should
be directed to system- or compiler-specific newsgroups. C++
has features for interoperating with C, so consider
comp.lang.c++ for such questions.

* The C standard, as opposed to standard C. Questions about
the C standard are best asked in comp.std.c.

* C++. Please do not post or cross-post questions about C++
to comp.lang.c. Ask C++ questions in C++ newsgroups, such
as comp.lang.c++ or comp.lang.c++.moderated.

* Test posts. Please test in a newsgroup meant for testing,
such as alt.test.

news.groups.questions is a good place to ask about the appropriate
newsgroup for a given topic.

--
"Some people *are* arrogant, and others read the FAQ."
--Chris Dollin
Nov 14 '05 #2

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

Similar topics

17
by: Anoob | last post by:
Can we consider () unary operator when calling a function, in exps eq. f(), ( 1 + 2). But when we call function f( 10 ) it is a binary operator. Even if we pass f( 10, 20) as we are using ,...
4
by: SSG | last post by:
I tried one lex, yacc program, but it says the error yacc: e - line 349 of "comp_y.y", a token appears on the lhs of a production can anyone tell me about the error.. How to solve it... ...
2
by: SSG | last post by:
Hai All! I am new to lex and yacc.. I compile one yacc program , but it tells errors... 10 Rules never reduced.. how to debug the code.. just i am giving my yacc sample code...
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
2
by: Christoph Kukulies | last post by:
Compilation (make, yacc, lex) of little parser used to work fine under BSD (FreeBSD) and now that I want to build it under cygwin, I suddenly get gcc -c y.tab.c y.y: In function `yyparse':...
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: max.giacometti | last post by:
Hi everybody! I am using lex and yacc to write a vhdl to systemc converter. Lex simply reads the input file and yacc implements grammar and translation. I'd like to be able to make yacc...
1
by: Robert | last post by:
If the goal is to take a 1-dimensional package of text and produce a tree data structure that is easy to understand and manipulate, and then write C/C++ code that works with that tree - does...
27
by: Dave | last post by:
I'm having a hard time tying to build gcc 4.3.1 on Solaris using the GNU compilers. I then decided to try to use Sun's compiler. The Sun Studio 12 compiler reports the following code, which is in...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.