LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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
 
LinkBack Search this Thread
Old 03-01-2011, 12:25 PM   #1
Codeless
LQ Newbie
 
Registered: Nov 2010
Posts: 2

Rep: Reputation: 0
Bison/flex help (I can't resolve this parse error)


I don't know what I'm doing wrong, perhapes I don't know what i'm doing... I could really use some help. This is my first compiler and I'm trying to design something very simple.

here's my flex file
Code:
%{
#include <stdlib.h>
#include "compile.h"
%}

%%
[ \t]+
"+"	{return PLUS;}
"-"	{return MINUS;}
"*"	{return TIMES;}
"/"	{return DIVIDE;}
"("	{return LEFT;}
")"	{return RIGHT;}
"\n"	{return END;}
[0-9]+	{yylval=atof(yytext); return NUMBER;}


%%
yywrap() {
  return 1;
}
and here's my bison file
Code:
%{
#define YYSTYPE double	
#include <stdio.h>
#include <stdlib.h>
#include "compile.h"	
%}

%token NUMBER
%token PLUS
%token MINUS
%token TIMES
%token DIVIDE
%token LEFT
%token RIGHT
%token END

%left PLUS 
%left MINUS
%left TIMES
%left DIVIDE

%start Input
%%

Input:	
	|	Input	Line 
;

Line:	'\n'	
	| Expression '\n'  { printf ("\t%.10g\n", $1); }	
;

Expression:
	NUMBER { $$=$1; }
	|	Expression PLUS Expression { $$=$1+$3; printf("test");}	
	|	Expression MINUS Expression { $$=$1-$3; }
	|	Expression TIMES Expression { $$=$1*$3; }
	|	Expression DIVIDE Expression { $$=$1/$3; if ($1 = 0) printf("testagain");}
	|	LEFT Expression RIGHT { $$=$2; }
;

%%

int yyerror(char *errMessage) {
	printf("Trouble: %s\n",errMessage);
}

main() {
	yyparse(); 
}
So you can see I'm trying to impliment a very simple calculator, I go through the cmds

flex lang.lex
bison -d -o compile.c land.y
gcc -o compile compile.c lex.yy.c
compile < test.txt (which is say "5+6") and I get a parse error

whats going on?

Last edited by Codeless; 03-01-2011 at 12:32 PM.
 
Old 03-01-2011, 02:20 PM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,390
Blog Entries: 2

Rep: Reputation: 900Reputation: 900Reputation: 900Reputation: 900Reputation: 900Reputation: 900Reputation: 900Reputation: 900
Does your input data file include a newline? Perhaps the parser is reaching EOF before seeing the require newline. It is hard to replicate your scenario without 'compile.h'.

--- rod.
 
Old 03-01-2011, 05:15 PM   #3
Dr_P_Ross
Member
 
Registered: Nov 2003
Location: Edinburgh, UK
Distribution: Arch
Posts: 41

Rep: Reputation: 18
I think there are two things going wrong.

First, your flex file returns END for '\n', but you have explicit '\n' in your bison file. In your bison file, put END instead of '\n' in both clauses for Line.

Second, compile.h is generated by your call of bison. Have a look at it, you'll see that it only defines YYSTYPE if YYSTYPE is undeclared. So in your flex file, you need to define YYSTYPE as double in the declarations. The it should all work.
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there any support for bison-bridge and bison-locations in flex on windows systems? rami alkhateeb Linux - Software 0 12-29-2010 09:10 AM
Error after installing flex and bison narakunaraku Linux - Newbie 3 07-30-2010 11:51 AM
flex/bison parse error expecting a char that it shoulnd't vargadanis Programming 0 08-08-2008 04:37 AM
flex/bison -> parse error but why? vargadanis Programming 3 08-03-2008 07:44 PM
Using Flex and Bison to parse the variable declaration section in Pascal unreal07 Programming 1 03-10-2007 08:25 PM


All times are GMT -5. The time now is 02:50 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