stergosz
stergosz

Reputation: 5860

Bison flex new line is not recognised

This is my input file:

class Box{ 
integer x;
}

The parses recognises the token CLASS, IDENTIFIER, { and then it says next token is $undefined and then it prints syntax error

When the code is in one line like:

class Box{integer x;}

it works as intended...

So the main problem is how to identify the new line

EDIT: here is a part of the flex file

declarations
%%
"\n" {return (NEWLINE);  }
[ ]+ /* blank, tab, new line: eat up whitespace */  
. {return(yytext[0]);}
%%
Auxiliary functions

Do i need to change my blank, tab and new line code to this: ?

[ \t\n]+         /* blank, tab, new line: eat up whitespace */
.                    {return(yytext[0]);}

edit2: my flex file is :

flex
%{
#include "arxeio.tab.h"
%}
KEF [A-Z]
MIK [a-z]
NUM [0-9]
US [_]
ID [A-Z]([A-Z]*[a-z]*[0-9]*[_]*)*|[a-z]([A-Z]*[a-z]*[0-9]*[_]*)*|[_]([A-Z]*[a-z]*[0-9]*[_]*)*
AR [0-9]+
%%
":="  {return (ASSGNOP); }
char {return (CHAR);  }
else { return (ELSE);  }
if {return (IF);  }
integer {return (INTEGER); }
class {return (CLASS); }
new {return (NEW);  }
return {return (RETURN); }
void {return (VOID);  }
while {return (WHILE); }
{ID} {return(IDENTIFIER) ;}
{AR} {return (NUMBER); }
"\n"   { fputs("<<newline>>\n", stderr); return (NEWLINE); }
"\""  {return (ET);  }
"\'"  {return (ET);  }
"\0"  {return (ET);  }
"\t"  {return (ET);  }
"\\"  {return (ET);  }
[ \t\n]+ /* ignore spaces, tabulations,line breaks,blank, tab, new line: eat up whitespace*/
.                    {return(yytext[0]);}
%%

my bison file is:

  %{
    #include  <stdio.h>
    #include  <stdlib.h>
    #include  <string.h>
    #include   <math.h>
    #define YYDEBUG 1
    int yylex(void) ;
    extern FILE *yyout;
    int errors;
    %}
    %start program
    %token CHAR ELSE IF INTEGER CLASS NEW RETURN VOID WHILE ASSGNOP
    %token NUMBER NEWLINE
    %token IDENTIFIER
    %token KEF MIK NUM ET 
    %left  '`''~''@''#''$''^''['']''{''}'',''_''?''.'';'':'
    %left '|''&''!'
    %left '>''<''='
    %left '-''+'
    %left '*''/''%'
    %left '('')'
    %expect 32
    %%
    ...
    ...
    ...
    program : CLASS IDENTIFIER '{'NEWLINE declar'}' 
    ;

    %%

    main (int argc,char *argv[])
    {extern FILE *yyin;
    ++argv;--argc;
    yyin = fopen ( argv[0],"r");
    yydebug = 1;
    errors = 0;
    yyparse();
    }

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

Upvotes: 0

Views: 6876

Answers (2)

Samy Dindane
Samy Dindane

Reputation: 18706

Since you have this rule: "/n" {return (NEWLINE); }, you obviously need to specify that NEWLINE follows { in your grammar; which I guess you didn't and which is understandable.

Thus, you need to use these rules

[ \t\n]+ /* ignore spaces, tabulations and line breaks */
.        {return(yytext[0]);}

Upvotes: 1

Jonathan Leffler
Jonathan Leffler

Reputation: 754090

Assuming the Flex code is copy'n'pasted, you've got a simple typo:

"/n" {return (NEWLINE);  }

should be:

"\n" { return(NEWLINE); }

You could help yourself debug this by using (temporarily):

"\n" { fputs("<<newline>>\n", stderr); return (NEWLINE); }

and then observing that it is never executed.

Upvotes: 1

Related Questions