LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 02-05-2010, 03:53 PM   #1
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
YACC syntax error


I was going through this Lex/YACC tutorial:

http://ds9a.nl/lex-yacc/cvs/lex-yacc-howto.html

and I was working along with it. The Lex examples worked fine, but the YACC one quit white compiling:

Code:
$ cat test.y
%{
#include <stdio.h>
#include <string.h>
 
void yyerror(const char *str) {
	fprintf(stderr,"error: %s\n",str);
}
 
int yywrap() {
	return 1;
} 
  
main() {
	yyparse();
} 
%}

%token NUMBER STATE TOKHEAT TOKTEMP

commands:
	| commands command
	;

command:
	heat_switch
	|
	temp_set
	;

heat_switch:
	TOKHEAT STATE
	{
		printf("\tHeat turned on|off\n");
	}
	;

temp_set:
	TOKTEMP NUMBER
	{
		printf("\tTemperature Set\n");
	}
	;

$ bison -d test.y
test.y:20.1-8: syntax error, unexpected identifier:
What should I do?
 
Old 02-05-2010, 04:01 PM   #2
irmin
Member
 
Registered: Jan 2010
Location: the universe
Distribution: Slackware (modified), Slackware64 (modified), openSuSE (modified)
Posts: 342

Rep: Reputation: 62
You missed the '%%' between the %token-line and your rules.

Code:
%token NUMBER STATE TOKHEAT TOKTEMP

commands:
	| commands command
	;
turn to

Code:
%token NUMBER STATE TOKHEAT TOKTEMP

%%

commands:
	| commands command
	;
%% is used to separate the sections of the grammar file.
 
1 members found this post helpful.
Old 02-05-2010, 04:10 PM   #3
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Great, it works now!

The tutorial had the file split into several pieces, that was a bit confusing.
 
Old 02-05-2010, 04:12 PM   #4
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Code:
$ cat test.l
%{
#include <stdio.h>
#include "y.tab.h"
%}

%%
heat   return TOKHEAT;
temp   return TOKTEMP;
on|off return STATE;
[0-9]+ return NUMBER;
%%

$ cat test.y
%{
#include <stdio.h>
#include <string.h>
 
void yyerror(const char *str) {
	fprintf(stderr,"error: %s\n",str);
}
 
int yywrap() {
	return 1;
} 
  
main() {
	yyparse();
} 
%}

%token NUMBER STATE TOKHEAT TOKTEMP

%%

commands:
	| commands command
	;

command:
	heat_switch
	|
	temp_set
	;

heat_switch:
	TOKHEAT STATE
	{
		printf("\tHeat turned on|off\n");
	}
	;

temp_set:
	TOKTEMP NUMBER
	{
		printf("\tTemperature Set\n");
	}
	;

$ bison -d test.y
$ flex test.l
$ gcc lex.yy.c y.tab.c -o test
gcc: y.tab.c: No such file or directory
test.l:3:19: error: y.tab.h: No such file or directory
test.l: In function ‘yylex’:
test.l:7: error: ‘TOKHEAT’ undeclared (first use in this function)
test.l:7: error: (Each undeclared identifier is reported only once
test.l:7: error: for each function it appears in.)
test.l:8: error: ‘TOKTEMP’ undeclared (first use in this function)
test.l:9: error: ‘STATE’ undeclared (first use in this function)
test.l:10: error: ‘NUMBER’ undeclared (first use in this function)
Now gcc doesn't compile!
 
Old 02-05-2010, 04:20 PM   #5
irmin
Member
 
Registered: Jan 2010
Location: the universe
Distribution: Slackware (modified), Slackware64 (modified), openSuSE (modified)
Posts: 342

Rep: Reputation: 62
Note that bison is not yacc. The option -d instructs bison to create a header file containing all token definitions, but this header file is called "test.h" in your case. To get yacc behaviour call bison as:
Code:
bison -d -y test.y
 
1 members found this post helpful.
Old 02-05-2010, 04:54 PM   #6
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Good. Everything works now.
 
  


Reply



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
[python] syntax Error : invalid syntax Python_user Programming 2 09-06-2009 12:52 PM
n Rules Never Reduced error in Yacc ssg14j Programming 1 12-09-2005 11:46 PM
Error in Makefile for Lex and Yacc oulevon Programming 2 10-24-2005 12:52 AM
C++ syntax error before :: token HELP, i cant find the syntax error :( qwijibow Programming 2 12-14-2004 06:09 PM
an yacc error message question feetyouwell Programming 1 10-16-2004 04:33 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:55 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
Open Source Consulting | Domain Registration