0
votes

I am currently working on a flex / bison project that consists of recognizing syntax errors in a given file. I have already written my parser and my lex file containing the rules but I think I have a problem with it because no matter which file I give to my program I get the following message "Syntax error near line 1", even for basic lines like int r1; for example. So here's my .lex file containing all my rules:

    %{
#include <stdlib.h>
#include <stdio.h>
#include "tpc-2019-2020.h"
int lineno = 1; 
%}

%option nounput
%option noinput
%x COMMENT

%%
<COMMENT>"*/" BEGIN INITIAL;
"/*" {printf("ok\n"); BEGIN COMMENT;}
"//".* ; 
<COMMENT>. ;
<COMMENT>\n ; lineno++;
int { return TYPE; }
char { return TYPE; }
";" { return 0; }
[ \t] {};
[0-9]* { return NUM; }
\n { lineno++; }
void { return VOID; }
print { return PRINT; }
while { return WHILE; }
readc { return READC; }
reade { return READE; }
[a-zA-Z] { return CHARACTER; }
[a-zA-Z][a-zA-Z0-9_]* { return IDENT; }
\+|- { return ADDSUB; }
\=\= { return EQ; }
\!\= { return EQ; }
\< { return ORDER; }
\<\= { return ORDER; }
\> { return ORDER; }
\>\= { return ORDER; }

\|\| { return OR; }
\&\& { return AND; }
. return yytext[0];

%%

And my .y file:

%{
/* tpc-2019-2020.y */
/* Syntaxe du TPC pour le projet d'analyse syntaxique de 2019-2020*/
#include <stdio.h>
int yyparse();
int yylex();
void yyerror(char *s);
extern int lineno;
%}

%token CHARACTER
%token NUM
%token IDENT
%token TYPE
%token EQ
%token ORDER
%token ADDSUB
%token OR
%token AND
%token ELSE
%token IF
%token PRINT
%token READC
%token READE
%token RETURN
%token VOID
%token WHILE
%left ELSE 

%%
Prog:  DeclVars DeclFoncts 
    ;
DeclVars:
       DeclVars TYPE Declarateurs ';' 
    |  ;
Declarateurs:
       Declarateurs ',' IDENT 
    |  Declarateurs ',' '*' IDENT 
    |  IDENT 
    |  '*' IDENT 
    ;
DeclFoncts:
       DeclFoncts DeclFonct 
    |  DeclFonct 
    ;
DeclFonct:
       EnTeteFonct Corps 
    ;
EnTeteFonct:
       TYPE IDENT '(' Parametres ')' 
    |  TYPE '*' IDENT '(' Parametres ')' 
    |  VOID IDENT '(' Parametres ')' 
    ;
Parametres:
       VOID 
    |  ListTypVar 
    ;
ListTypVar:
       ListTypVar ',' TYPE IDENT 
    |  ListTypVar ',' TYPE '*' IDENT 
    |  TYPE IDENT 
    |  TYPE '*' IDENT 
    ;
Corps: '{' DeclVars SuiteInstr '}' 
    ;
SuiteInstr:
       SuiteInstr Instr 
    |  ;
Instr:
       LValue '=' Exp ';'
    |  READE '(' IDENT ')' ';'
    |  READC '(' IDENT ')' ';'
    |  PRINT '(' Exp ')' ';'
    |  IF '(' Exp ')' Instr 
    |  IF '(' Exp ')' Instr ELSE Instr
    |  WHILE '(' Exp ')' Instr
    |  IDENT '(' Arguments  ')' ';'
    |  RETURN Exp ';' 
    |  RETURN ';' 
    |  '{' SuiteInstr '}' 
    |  ';' 
    ;
Exp :  Exp OR TB 
    |  TB 
    ;
TB  :  TB AND FB 
    |  FB 
    ;
FB  :  FB EQ M
    |  M
    ;
M   :  M ORDER E 
    |  E 
    ;
E   :  E ADDSUB T 
    |  T 
    ;    
T   :  T '*' F 
    |  T '/' F 
    |  T '%' F 
    |  F 
    ;
F   :  ADDSUB F 
    |  '!' F 
    |  '&' IDENT 
    |  '(' Exp ')' 
    |  NUM 
    |  CHARACTER
    |  LValue
    |  IDENT '(' Arguments  ')' 
    |  '*' IDENT '(' Arguments  ')' 
    ;
LValue:
       IDENT 
    |  '*' IDENT 
    ;
Arguments:
       ListExp 
    | ;
ListExp:
       ListExp ',' Exp 
    |  Exp 
    ;
%%
int main(int argc, char** argv) {
    yyparse();
    return 0;
}
void yyerror(char *s){
    fprintf(stderr, "%s near line %d\n", s, lineno);
}

And with those files even if my input file is just: int r1, r2;

It give me "Syntax error line 1"

I tried to put only some comments lines into my file and I get the "OK" message wich mean my rules recognize comments lines but even tho I always get the same "Syntax error near line 1" each time.

Thank you for your help !

1

1 Answers

0
votes

Your grammar says a declaration looks like this:

DeclVars:
   DeclVars TYPE Declarateurs ';' 

In other words, each declaration ends with a semicolon.

But your scanner never returns a semicolon:

";" { return 0; }

When the scanner reaches a semicolon, it returns an end-of-input indicator. So the parse fails for lack of the semicolon.

Enabling bison traces would make this more evident.