1 Replies - 332 Views - Last Post: 21 August 2013 - 05:38 AM Rate Topic: -----

#1 hari106  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 24-July 13

Help with lex&yacc

Posted 21 August 2013 - 05:12 AM

Hello there.

I just started on lex&yacc in my coursework, so I started studying from lex&yacc by John Levine(O'Reilly). Since GNU flex and GNU bison are more commonly used tools on Windows, I am using them for practice.

However when I try a code section from this book(the second example ch-02.l), I get few errors.
Here is the code:
%{
	/*simple word recogniser*/
%}
%%

	[ \t]+ ;
	is |
	am | 
	are | 
	were | 
	was | 
	be | 
	being | 
	been | 
	do | 
	does | 
	did | 
	will | 
	would | 
	should | 
	can | 
	could | 
	has | 
	have | 
	had |
	go { printf("%s is a verb\n", yytext);}
	[a-zA-Z]+ { printf("%s is not a verb\n", yytext);} 
	.|\n { ECHO; }
%%
main()
{
		yylex();
}


And here are the errors:

Quote

E:\Source Codes\workspace\lexyacc\wordrec>flex wordrec.l

E:\Source Codes\workspace\lexyacc\wordrec>gcc lex.yy.c -lfl
wordrec.l: In function 'yylex':
wordrec.l:6:2: error: expected expression before '[' token
wordrec.l:6:2: error: stray '\' in program
wordrec.l:6:5: error: 't' undeclared (first use in this function)
wordrec.l:6:5: note: each undeclared identifier is reported only once for each f
unction it appears in
wordrec.l:7:2: error: 'is' undeclared (first use in this function)
wordrec.l:8:2: error: 'am' undeclared (first use in this function)
wordrec.l:9:2: error: 'are' undeclared (first use in this function)
wordrec.l:10:2: error: 'were' undeclared (first use in this function)
wordrec.l:11:2: error: 'was' undeclared (first use in this function)
wordrec.l:12:2: error: 'be' undeclared (first use in this function)
wordrec.l:13:2: error: 'being' undeclared (first use in this function)
wordrec.l:14:2: error: 'been' undeclared (first use in this function)
wordrec.l:15:2: error: expected expression before 'do'
wordrec.l:28:2: error: stray '\' in program


I have no idea what's the issue here.


Is This A Good Question/Topic? 0
  • +

Replies To: Help with lex&yacc

#2 hari106  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 24-July 13

Re: Help with lex&yacc

Posted 21 August 2013 - 05:38 AM

Ok...I just got it.

The problem was in the main(): use "return yylex()" instead of "yylex()". Also there is white space between ".", "|" and "\n".

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1