Amol
Amol

Reputation: 59

unable to understand the error in lex program to count no of tokens

i wrote the lex program to calculate the count of tokens using gedit in linux. but it is not running.i m new in this. also i m not able to find out the problem in the code.

this is the program code :

    count=0
digit [0-9]
letter [A-Z][a-z]
%%
{letter} | ({letter}|{digit})* 
count++
%%
int main()
{
yylex()
printf("no. of identifier=%d",count);
}

error msg is :

    scanner.l:9: error: expected declaration specifiers before ‘yylex’
scanner.l:10: error: expected ‘{’ at end of input

Upvotes: 0

Views: 374

Answers (1)

David Gorsline
David Gorsline

Reputation: 5018

Your initialization in the Definitions section of your lex spec isn't quite right. It should be a syntactically correct C statement, indented. Also, your code to increment count must be on the same line as the pattern it goes with. So you want something like this:

    int count = 0;
digit [0-9]
letter [A-Z][a-z]
%%
{letter}|({letter}|{digit})*    count++; 
%%
int main()
{
    yylex();
    printf("no. of identifier=%d",count);
}

Upvotes: 1

Related Questions