Search code examples
cbisonflex-lexeryacc

Lex&YACC compile error: expected ')' before '.' token


I have to write a parser for a mini language and I have some problems. Here is the YACC file:

%{
#include <stdio.h>
int yylex();
void yyerror(char *s);

%}

%union {int num; char id; double d; char *s;}
%start program
%token <num> DIGIT
%token <s> IDENTIFIER
%token <num> NO
%type <num> term condition
%type <s> expression assignstmt stmt
%%
program : "##LAZY###" "vars" decllist cmpdstmt   {;}
        ;
decllist : declaration                           {;}
        | declaration decllist                  {;}
        ;
declaration : "in" IDENTIFIER                    {int $2;}
            | "in" '[' NO ']' IDENTIFIER         {int $5[$3];}
            ;                   
cmpdstmt : "exec" stmtlist "stop"                {;}
stmtlist : stmt                                  {;}
        | stmt stmtlist                         {;}
        ;
stmt : assignstmt                                {;}
    | ifstmt                                    {;}
    | whilestmt                                 {;} 
    ;
assignstmt : IDENTIFIER '=' expression           {$1 = $3;}
        ;
expression : expression '+' term                 {$$ = $1 + $3;}
        | term '+' term                       {$$ = $1 + $3;}
        ;
term : DIGIT                                     {$$ = $1;}
    | IDENTIFIER                                {$$ = $1;}
    ;
ifstmt : "if" '(' condition ')' '{' stmt '}'     {if($3){$6;}}
    ;
whilestmt : "wh" '(' condition ')' '{' stmt '}'  {while($3){$6;}}
        ;
condition : expression "<" expression            {$$ = ($1 < $3);}
        | expression "<=" expression           {$$ = ($1 <= $3);}
        | expression "==" expression           {$$ = ($1 == $3);}
        | expression "!=" expression           {$$ = ($1 != $3);}
        | expression ">=" expression           {$$ = ($1 >= $3);}
        | expression ">" expression            {$$ = ($1 > $3);}
        ;
%%

int main() {
    printf("WORKING\n");
    return yyparse();
}

void yyerror(char*s) { printf("%s\n", s); }

But when I try to compile it with: cc lex.yy.c y.tab.c I receive the following errors and I don't know how to fix them or why I receive them:

lazy.y: In function ‘yyparse’:
lazy.y:21:19: error: expected ‘)’ before ‘.’ token
declaration : "in" IDENTIFIER                    {int $2;}
                ^
lazy.y:22:19: error: expected ‘)’ before ‘.’ token
            | "in" '[' NO ']' IDENTIFIER         {int $5[$3];}

I will post also the Lex file if is needed.


Solution

  • from

    declaration : "in" IDENTIFIER                    {int $2;}
           | "in" '[' NO ']' IDENTIFIER         {int $5[$3];}
           ;                   
    

    the error comes from {int $2;} and {int $5[$3];}

    what did you expect with them ?

    That is legal :

    declaration : "in" IDENTIFIER                    {char * s = $2;}
                | "in" '[' NO ']' IDENTIFIER         {int i =  $5[$3];}
                ;
    

    except of course these variables are local so just that has no real interrest