For some reason, it seems that I can’t fix the errrors I’m encountering, which are weird, specially the YYABORT one, since I’m defining %locations. My Flex version is 2.6.4 and my bison is 3.8. I’d love to hear some feedback since I’m being stuck here. Thx a lot in advance!
js.l:
%{
#include "js-parser.h"
#define YY_USER_ACTION
yylloc.first_line = yylloc.last_line;
yylloc.first_column = yylloc.last_column;
yylloc.last_column += yyleng;
if (yytext[yyleng-1] == 'n') {
yylloc.last_line++;
yylloc.last_column = 1;
};
void yyerror(YYLTYPE *loc, const char *s);
%}
%option yylineno
%%
"var" { return VAR; }
"const" { return CONST; }
"let" { return LET; }
[a-zA-Z_][a-zA-Z0-9_]* { yylval.sval = strdup(yytext); return IDENTIFIER; }
[0-9]+(.[0-9]*)?([eE][-+]?[0-9]+)?|.[0-9]+([eE][-+]?[0-9]+)? { yylval.ival = atoi(yytext); return NUMBER; }
"//" { input(); while (input() != 'n'); }
"/*" { do { while(input() != '*'); } while (input() != '/'); }
"+" { return PLUS; }
"-" { return MINUS; }
"*" { return MULTIPLY; }
"/" { return DIVIDE; }
"(" { return LPAREN; }
")" { return RPAREN; }
"=" { return ASSIGN; }
";" { return SEMICOLON; }
[ tn]+ ;
. { return yytext[0]; }
%%
js.y:
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int yylex();
void yyerror(YYLTYPE *loc, const char *s);
int main();
%}
%locations
%union {
int ival;
char *sval;
};
%token <ival> IDENTIFIER
%token <ival> NUMBER
%token VAR CONST LET PLUS MINUS MULTIPLY DIVIDE LPAREN RPAREN ASSIGN SEMICOLON
%left PLUS MINUS
%left MULTIPLY DIVIDE
%right NEG
%type <ival> expr
%%
program:
| program statement SEMICOLON
;
statement:
type IDENTIFIER
| type IDENTIFIER ASSIGN expr
;
type:
VAR
| CONST
| LET
;
expr:
NUMBER
| IDENTIFIER
| expr PLUS expr { $$ = $1 + $3; }
| expr MINUS expr { $$ = $1 - $3; }
| expr MULTIPLY expr { $$ = $1 * $3; }
| expr DIVIDE expr { if ($3) $$ = $1 / $3; else { yyerror(@3, "division by zero"); }; }
| LPAREN expr RPAREN { $$ = $2; }
| MINUS expr %prec NEG { $$ = -$2; }
;
%%
void yyerror(YYLTYPE *loc, const char *s) {
fprintf(stderr, "Error at %d: %sn", loc->first_line, s);
YYABORT;
};
int main() {
printf("Use this js parser");
yyparse();
return 0;
};
And the errors I’m encountering:
js.y:7:14: error: unknown type name ‘YYLTYPE’
7 | void yyerror(YYLTYPE *loc, const char *s);
| ^~~~~~~
js.y: In function ‘yyparse’:
js.y:53:117: warning: implicit declaration of function ‘yyerror’; did you mean ‘yyerrok’? [-Wimplicit-function-declaration]
53 | | expr DIVIDE expr { if ($3) $$ = $1 / $3; else { yyerror(@3, "division by zero"); }; }
| ^
| yyerrok
js.y: At top level:
js.y:60:6: warning: conflicting types for ‘yyerror’; have ‘void(YYLTYPE *, const char *)’
60 | void yyerror(YYLTYPE *loc, const char *s) {
| ^~~~~~~
js.y:53:117: note: previous implicit declaration of ‘yyerror’ with type ‘void(YYLTYPE *, const char *)’
53 | | expr DIVIDE expr { if ($3) $$ = $1 / $3; else { yyerror(@3, "division by zero"); }; }
| ^
js.y: In function ‘yyerror’:
js.y:62:5: error: label ‘yyabortlab’ used but not defined
62 | YYABORT;
| ^~~~~~~
I've tried to declare YYABORT manually among many other tries, but I always end up with the same errors.