LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-30-2004, 04:21 AM   #1
chuanyung
LQ Newbie
 
Registered: Jan 2004
Posts: 25

Rep: Reputation: 15
Question [flex & bison] Why does my rule fail from line 2?


Dear all,

I am a newbie to use flex and bison.
I don't know how to solve this problem.
My test file has 2 lines which are same.
I can match my rule in first line, but fail in second line.
What's wrong?

And when I need to use loop to yyparse() many times?
Please help, thanks.

Here are my files.

************
test.txt
-----------------------------------------------------
test abc 10 def
test abc 10 def
-----------------------------------------------------


***********
flex.l
--------------------------------------------------------
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "bison.h"
int mylineno=1;
bool isEOF=false;
extern void yyerror(char* szErrorMessage);
%}

%option noyywrap
%option outfile="flex.c"
%option yylineno

%x stTEST

white ([ \t\r]+)
digit ([0-9])
letter ([a-zA-Z])
eol (\r?\n)

%%

{white} ;

test{white} {printf("Begin test:\n");BEGIN stTEST;return TEST;}
<stTEST>{white} ;
<stTEST>{digit}+ {yylval.INT=atoi(yytext); printf("Got integer %d\n",yylval.INT);return INTEGER;}
<stTEST>{letter}+ {yylval.STR=strdup(yytext);printf("Got string %s\n",yylval.STR);return TEXT;}
<stTEST>{eol} {printf("Got eof in test\n");BEGIN INITIAL; return EOL;}
<stTEST>. {yyerror("Not an alpha number!");yyterminate();}

. {yyerror("Not a test command\n");yyterminate();}
{eol} {mylineno++;}

<<EOF>> {isEOF=true;printf("end of file\n");return 0;}

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

************
bison.y
-------------------------------------------------------
%{
#include <stdio.h>
extern FILE* yyin;
extern int yylineno;
extern int yylex(void);
extern int mylineno;
extern bool isEOF;
void yyerror(char* szErrorMessage);
%}


%union {
int INT;
char* STR;
}
/* options */
%output="bison.c"
%no-lines
/* end of options*/

%token <INT> INTEGER
%token <STR> TEXT
%token EOL /* end of line */
%token TEST

%%

start: test {printf("test command is ok.\n");}
;

test: test_expression EOL {mylineno++};
;

test_expression: TEST TEXT INTEGER TEXT
;
%%


int main(int argc,char* argv[])
{
if (argc<2) {
printf("No file to parse\n");
return -1;
}

yyin=fopen(argv[1],"r");
if (yyin==NULL) {
printf("Fail to open file %s\n",argv[1]);
return -2;
}

yyparse();

return 0;
}

void yyerror(char* szErrorMessage)
{
printf("My error in %d: %s.\n",mylineno,szErrorMessage);
}
---------------------------------------------------------------------

*********
Makefile
--------------------------------------------------------
test: bison.o flex.o
g++ -o test bison.o flex.o -lfl -ly -lm

bison.o: bison.c bison.h
g++ -c bison.c

flex.o: flex.c bison.h
g++ -c flex.c

bison.c: bison.y
bison -d bison.y

flex.c: flex.l
flex -L flex.l

clean:
rm -rf *.o flex.c bison.c bison.h test test.exe
---------------------------------------------------------------

Last edited by chuanyung; 07-30-2004 at 04:22 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
installing bison and flex rameshsena Linux - Software 10 06-21-2010 11:33 AM
bison / flex zaman Programming 1 08-16-2005 10:19 AM
Problem with binutils first pass / bison & flex installation peristaltic Linux From Scratch 3 03-20-2005 10:17 AM
another flex & yacc question sibtay Programming 1 12-25-2004 10:34 PM
bison, flex and make problem ChimpFace9000 Programming 4 05-19-2002 07:49 PM


All times are GMT -5. The time now is 08:09 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration