LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-27-2011, 12:00 AM   #1
Avanti
LQ Newbie
 
Registered: Oct 2011
Posts: 10

Rep: Reputation: Disabled
implementing calculator program in lex and yacc but getting syntax error


I am implementing calculator program in lex and yacc.I have created calci.y file. Also y_tab.c and lexyy.c have been created.But whenever i enter the expression(eg:2+3) it gives me "syntax error"


%{
#include<stdlib.h>
#include<math.h>
%}
%union{
int intval;
float floatval;
};
%token <intval> D1
%token <floatval> D2
%token NAME
%type <floatval> exp
%left '+' '-'
%left '*' '/'
%start s
%%
s: NAME'='exp {printf("\nresult: %d\n",$3);}
|exp {printf("\nresult:%d\n",$1);}
;
exp:exp'+'exp {$$=$1+$3;}
|exp'-'exp {$$=$1-$3;}
|exp'*'exp {$$=$1*$3;}
|exp'/'exp {$$=$1/$3;}
;
%%
main()
{
char ch;
do
{
printf("\nenter exression:");
flushall();
yyparse();
yylex();
getch();
printf("\ndo u want to continue:");
scanf("%c",&ch);
}while(ch=='y');
}
void yyerror(char *ch)
{
printf("%s: error\n",ch);
}
 
Old 10-27-2011, 11:27 AM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: CentOS 6 (pre-systemd)
Posts: 2,838

Rep: Reputation: 755Reputation: 755Reputation: 755Reputation: 755Reputation: 755Reputation: 755Reputation: 755
Need two things:
1) edit your post and add code tags
2) Show that you have done at least minimal debugging: check and print results of all calls
 
  


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 aarsh Linux - Software 3 01-03-2011 09:58 AM
[SOLVED] YACC syntax error MTK358 Programming 5 02-05-2010 05:54 PM
LEX and YACC with C gr33ndata Programming 4 11-18-2007 06:12 PM
Implementing yacc/lex Millenniumman Programming 2 10-07-2006 11:49 AM
Error in Makefile for Lex and Yacc oulevon Programming 2 10-24-2005 01:52 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:12 PM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration