Search code examples
cbisonyacclex

expected expression before '=' token


I want to do parser, which will print out expressions into steps of their calculation. And when I compile my code, I cannot solve these problem. I always get error

code.l:13:1: error: expected expression before '=' token
  yylval.name = strdup(yytext);
 ^
code.l:18:1: error: expected expression before '=' token
  yylval.name = strdup(yytext);

 ^

I tried a lot of different things, what I thought were a problem, but was unsuccessful.

code.l

%{
#include <stdio.h>
#include <string.h>
#include "Assignment.tab.h"
%}

%%

" " ;
"\t" ;
[a-zA-Z]+
        {
        yylval.name = strdup(yytext);
        return(ID);
        }
[0-9]+
        {
        yylval.name = strdup(yytext);
        return(NUM);
        }
[-+=()*/\n]
        {
        return yytext[0];
        }
.
        {
        yyerror("unknown character");
        }

%%

code.y

%{
#include<stdio.h>

int temp = 0;
%}

%start list

%union
        {
        char *name;
        }
%token <name> ID
%token <name> NUM
%type <name> list stat expr

%left '+' '-'
%left '*' '/'
%left UMINUS

%%

list:
        |
        list stat '\n'
        |
        list error '\n'
        {
        yyerrok;
        }
        ;
stat:
        expr
        {
        printf("stat:t = (%s)\n:stat",$1);
        }
        |
        ID '=' expr
        {
        printf("stat:(%s) = (%s):stat", $1, $3);
        }
        ;

expr:
        '(' expr ')'
        {
        $$ = $2;
        }
        |
        expr '*' expr
        {
        printf("t = (%s) * (%s)", $1, $3);
        $$ = "t";
        }
        |
        expr '/' expr
        {
        printf("t = (%s) / (%s)", $1, $3);
        $$ = "t";
        }
        |
        expr '+' expr
        {
        printf("t = (%s) + (%s)", $1, $3);
        $$ = "t";
        }
        |
        expr '-' expr
        {
        printf("t = (%s) - (%s)", $1, $3);
        $$ = "t";
        }
        |
        '-' expr %prec UMINUS
        {
        printf("t = -(%s)", $2);
        $$ = "t";
        }
        |
        ID
        {
        $$ = $1;
        }
        |
        NUM
        {
        $$ = $1;
        }
        ;

%%

main()
{
 return(yyparse());
}

yyerror(s)
char *s;
{
  fprintf(stderr, "%s\n",s);
}

yywrap()
{
  return(1);
}

I do not need solution for my end project, I just need to find what is causing the error. Any idea is helpful.

EDIT: Assignment.tab.h file


#ifndef YY_YY_ASSIGNMENT_TAB_H_INCLUDED
# define YY_YY_ASSIGNMENT_TAB_H_INCLUDED
/* Enabling traces.  */
#ifndef YYDEBUG
# define YYDEBUG 0
#endif
#if YYDEBUG
extern int yydebug;
#endif

/* Tokens.  */
#ifndef YYTOKENTYPE
# define YYTOKENTYPE
   /* Put the tokens into the symbol table, so that GDB and other debuggers
      know about them.  */
   enum yytokentype {
     ID = 258,
     NUM = 259,
     UMINUS = 260
   };
#endif


#if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
typedef union YYSTYPE
{
/* Line 2058 of yacc.c  */
#line 10 "Assignment.y"

        char *name;


/* Line 2058 of yacc.c  */
#line 67 "Assignment.tab.h"
} YYSTYPE;
# define YYSTYPE_IS_TRIVIAL 1
# define yystype YYSTYPE /* obsolescent; will be withdrawn */
# define YYSTYPE_IS_DECLARED 1
#endif

extern YYSTYPE yylval;

#ifdef YYPARSE_PARAM
#if defined __STDC__ || defined __cplusplus
int yyparse (void *YYPARSE_PARAM);
#else
int yyparse ();
#endif
#else /* ! YYPARSE_PARAM */
#if defined __STDC__ || defined __cplusplus
int yyparse (void);
#else
int yyparse ();
#endif
#endif /* ! YYPARSE_PARAM */

#endif /* !YY_YY_ASSIGNMENT_TAB_H_INCLUDED  */

Solution

  • The action in a lex rule must start on the same line as the pattern. So you need to write, for example

    [a-zA-Z]+  {
        yylval.name = strdup(yytext);
        return(ID);
        }
    

    For what it's worth, this requirement is clearly stated in the flex manual section on the format of an input file:

    The rules section of the flex input contains a series of rules of the form:

    pattern   action
    

    where the pattern must be unindented and the action must begin on the same line.

    This restriction is, as far as I know, present in all lex implementations, although the consequences differ. I quoted the Flex manual because I find it to be more readable than the Posix description, and it also describes the many useful flex-only features.