-1

I am implementing a parser of which the scanner (implemented using Flex) is not recognizing all the tokens at a time. It is just taking the first token from the input and terminating. Can someone please help me sort out this. Here is my ".lex" file:

%{
/* need this for the call to atof() below */
#include <math.h>
#include<string.h> 
#include "parser.h"
#include "idf.tab.h"
char findname ( char *yytext) { return yytext[0]; }
%}

DIGIT    [0-9]
ID       [a-zA-Z]*
%option noyywrap

%%

{ID} | 
-?{DIGIT}+"."{DIGIT}* |
-?{DIGIT}+   { printf("ID or number:%s\n",yytext); /*yylval.a_variable =  (char*)findname(yytext);*/   return TOKID;}

";"        { printf("Semicolon\n");   return TOKSEMICOLON; }
":"        {   printf("Colon\n"); return TOKCOLON;}
","           return TOKCOMMA;
"."           return TOKDOT;
"-"           return TOKMINUS;

[ \t\n]          /* eat up whitespace */
.       return TOKUNRECOG;

%%

int main( int argc,char*  argv[] )
{
++argv, --argc; 
if ( argc > 0 )
yyin = fopen( argv[0], "r" );
else
yyin = stdin;

yylex();
}

For example, if I give the input as abc; the expected output should be:

ID or number: abc
Semicolon

But the actual output is:

ID or number: abc

That is, it is just recognizing the first token abc and terminating without recognizing the semicolon. Whereas if the input is just ; then the output is

Semicolon
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Ujwal Potluri
  • 39
  • 2
  • 7
  • Sounds like the program is doing exactly what you specified -- it calls `yylex` to read one token and then exits. – Chris Dodd Apr 24 '12 at 05:39

1 Answers1

1

yylex() returns the next available token, on the asumption that is being called under control of a parser. If you want to process an entire input, call it in a loop until it returns 0.

geekosaur
  • 59,309
  • 11
  • 123
  • 114