0

i have written a lex program ( .l file ) for line count and character count PROGRAM:

%{
  int charcount=0,linecount=0;
%}
%%
.charcount++
\n linecount++,charcount++;
%%
main()
{
yylex();
printf(“lines  %d”,linecount);
printf(“characters %d”,charcount);
}
int yywrap()
{
return 1;
}

i use flex bison and codeblocks after writing the program i executed it with the command flex lccc.l (lccc is the file name) now i have lex.yy.c file please tell me how do i get the output compiling lex.yy.c is igivng and error.. but this program works fine on linux which is at my college, at home i use the above mentioned tweaks on windows.. please help!

this is the error :

 J:\> gcc lex.yy.c
 lccc.l:  In function 'main':
 lccc.l:13: error:  stray'\223' in program
 lccc.l:13: error:  'lines' undeclared (first use in this function )
 lccc.l:13: error:  (Each undeclared identifier is reported only once
 lccc.l:13: error:  for each function it appears in. )
 lccc.l:13: error:  stray'\224' in program
 lccc.l:13: error:  'd' undeclared (first use in this function )
 lccc.l:14: error:  stray'\223' in program
 lccc.l:14: error:  'characters' undeclared (first use in this function )
 lccc.l:14: error:  stray'\224' in program
cosmicchichu
  • 63
  • 2
  • 8
  • Giving *what* error? You can't omit the vital information from your question and rationally expect to get it answered. – user207421 Nov 06 '11 at 22:49
  • Try read this post: http://stackoverflow.com/questions/5456011/how-to-compile-lex-yacc-files-on-windows – DrBeco Nov 07 '11 at 03:18
  • @Dr Beco i started off with ur suggestions sir.. otherwise i wudnt have had flex at all.. thanks fro the post – cosmicchichu Nov 07 '11 at 14:06

1 Answers1

0

You need to enclose custom code in braces. And there's no semi-colon after: charcount++ and you can't separate statements with a comma, so linecount++, charcount++; should be linecount++; charcount++;.

Try this:

%{
  int charcount=0, linecount=0;
%}
%%
.  {charcount++;}
\n {linecount++; charcount++;}
%%
main()
{
  yylex();
  printf("lines  %d", linecount);
  printf("characters %d", charcount);
}
// ...
Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
  • it did work sir !! im sorry for the silly mistakes.. we have a really terrible sir who knows nothin about the subject.. but sir without yy wrap it says there is an undefined reference to yywrap().. then when i included it .. it worked.. but the output is not as deired... i just keep entering text and it keeps taking.. it does not tel the no. of lines and characters – cosmicchichu Nov 07 '11 at 14:00