Search code examples
clex

Lex program to check the structure(struct)


I'm trying to create program to check if the input such as struct st{ int j; real i; } it should print correct otherwise not correct.

but I have this error

    prog.l: In function 'yylex':
gyg.l:10:5: error: expected expression before '{' token

Solution

  • You have an unquoted/unescaped space inside your pattern. Lex uses spaces to separate the pattern from the action, so it thinks ("struct")" "({iden})+ is your pattern and your C code is:

    "{"({defs}|({defs})*)"}"";"    {printf(" correct ");}`
    

    Clearly the first part of that is not valid C code and that's why you get a compilation error. To fix this problem, remove the space, put it in quotes or escape it with a backslash.