LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 01-07-2004, 09:07 AM   #1
hemanexp
LQ Newbie
 
Registered: May 2003
Posts: 6

Rep: Reputation: 0
Lex & yacc Programming in linux.


Hi,
I developed an application using lex & yacc. It is a simple calculator which does add, subtract and multiplication. The program is compiled correctly without errors. But when i execute the program, it is giving 0 as output irrespective of input and the result is always 0.000. For example if the input is 3.2 + 4.5, the output is 0.000. I dont know where i have gone wrong. The programs are given below.

cal.l
------

%{
#include "global.h"
#include "y.tab.h"
#include <stdlib.h>
%}
white [ \t]+
digit [0-9]
integer {digit}+
real {integer}("."{integer})?
%%
{white} { }
{real} {yyval=atof(yytext);
return (NUMBER);}
"+" return (PLUS);
"-" return (MINUS);
"*" return (TIMES);
"\n" return (END);

-------------------------------------------------
cal.y
------

%{
#include "global.h"
#include <stdio.h>
#include <stdlib.h>
%}
%token NUMBER
%token PLUS MINUS TIMES
%token END

%left PLUS MINUS TIMES

%start Input
%%

Input:
| Input Line
;

Line:
END
| Expression END {printf("\n Result: %f",$1);}
;

Expression:
NUMBER { $$=$1; }
| Expression PLUS Expression { $$=$1+$3;}
| Expression MINUS Expression { $$=$1-$3; }
| Expression TIMES Expression { $$=$1*$3; }

;
%%

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

int main(void)
{
yyparse();
}

global.h
---------
#define YYSTYPE double
extern YYSTYPE yyval;


Result:
-------
[root@mycal]# yacc -d cal.y
[root@mycal]# lex cal.l
[root@mycal]# gcc -o cal y.tab.c lex.yy.c -lfl
[root@mycal]# ./cal
2.3+4.2
Result: 0.000000
[root@mycal]#


Let know what is the problem and how to solve it.

Thanx.
 
Old 01-07-2004, 10:42 AM   #2
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 66
I would recommend printing out all the arguments to make sure you are parsing them as you intended it too. It has been a long time since I have used lex and yacc but I am guessing you are getting 0's for all your values with that code.

This should probably be moved to the programming forum....
 
Old 01-07-2004, 11:09 AM   #3
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 66
Ok... so I decided to do some debugging for you because I was bored...

You are setting yyval correctly. You are also correctly determining which operation you are doing. However, $1 is always 0.0000 when your number case comes up so this is where your problem lies. I'll leave the rest to you.
 
  


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
LEX and YACC with C gr33ndata Programming 4 11-18-2007 05:12 PM
lex & yacc installation sharathg786 Ubuntu 3 10-12-2005 01:21 AM
how to work on lex & yacc? sharathg786 Ubuntu 2 09-13-2005 04:52 AM
Lex & YACC coolfrog Programming 3 09-25-2004 07:00 AM
Lex & yacc programming in windows. hemanexp Linux - Software 1 01-09-2004 05:41 AM


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