1
votes

I am super new to FLEX, I'm trying to write a simple FLEX lex file, but for some reason I get the error in the header.

The complete error is:

bash syntax error near unexpected token '('

The command I am running is:

flex sample.lex 

OR

flex sample.l 

Where am I going wrong here?

%{

union{
    int val;
    char name [30];
    char str [80];
}yylval;

#include <stdio.h>
%}

%%
. ECHO;
%%

int main(int argc, char **argv)
{
    if (argc != 2) {
        printf("no input file>\n");
        exit (1);
    }

  printf("token    lexeme    attribute\n");
  printf("--------------------------\n");
  yyin=fopen(argv[1], "r");
  if(yyin!=0)
  {    printf("file opened");
        fclose(yyin);}

  exit (0);
}
int yywrap () { return 1; }
1
What is the type of main?Igor
@Igor: Prior to C99, if you don't specify the type of a function it defaults to int. C99 dropped that rule, but many compilers still support it, perhaps with a warning (strictly speaking omitting the return type is now a syntax error). Still, there's no good reason to omit the return type. David: Change main to int main; that's not the cause of your problem, but you should fix it anyway.Keith Thompson
@KeithThompson Changed it to 'int main' thanks!David Faizulaev
@KeithThompson I changed it to 'int main', now I am getting the following error: " bash syntax error near unexpected token '(' " Any idea why?David Faizulaev
No idea at all. Since the error is from bash (I hadn't noticed that before), you must be executing some bash command that produces that error. Unless you tell us what command you're executing, we can't guess what's causing it. Almost certainly the problem isn't in the file you've shown us, it's in what you're doing with it. Please update your question to show the exact command you're running that produces that error message.Keith Thompson

1 Answers

3
votes

Are you running Flex on the file, and then GCC? I was able to compile this. First, run

flex test.l

This should generate a file name "lex.yy.c", which is C code generated by Flex. This file contains code to scan the input, and the C code in your .l file. Then running

gcc lex.yy.c

should produce an executable "a.out". Running

./a.out test.txt

produced the output you're printing, including "file opened". As was pointed out, main does not have a return type, but I guess my compiler was able to figure it out.

However, even if you can compile this, it looks like you are misunderstanding a few aspects of Flex.

union{
    int val;
    char name [30];
    char str [80];
}yylval;

is a regular C union. Bison's %union declaration is used when a semantic value can have more than one type, and is not the same thing. %union should not be in the .l file.

Also, you need to call yylex() to actually start scanning the input.

Right now, I would suggest going to the Flex manual and doing some of the starter examples: http://westes.github.io/flex/manual/Simple-Examples.html. If you are using Flex and Bison together, you should also go through some of the Bison examples: http://www.gnu.org/software/bison/manual/html_node/Examples.html#Examples