I'm doing some simple lex and yacc exercises, and I've noticed the positions in @1 @2 @3 etc are not getting updated... I thought that was automatic ? Did I misunderstand something ? Do I have to manually update yylloc in my lex rules ?
Using the following code and input file, this is the output :
output
$ main < input.txt
Warning line 1 : invalid date
Warning line 1 : invalid time
Warning line 1 : invalid date
Warning line 1 : invalid time
[...]
The errors are expected, but they shouldn't all be on line 1.
y.tab.y
%{
#include <stdio.h>
int yyerror(char* msg);
int yylex(void);
int table[100][10];
int isValidTime(char *time);
int isValidDate(char *date);
%}
%union {
char * string;
int integer;
}
%token IDF EOL DATE TIME INT
%type<string> DATE TIME
%type<integer> INT
%%
file
: first_line EOL content
;
first_line
: first_line IDF
|
;
content
: content one_line EOL
|
;
one_line
: DATE TIME values {
if (!isValidDate($1)) {
fprintf(
stderr,
"Warning line %d : invalid date\n",
@1.last_line
);
}
if (!isValidTime($2)) {
fprintf(
stderr,
"Warning line %d : invalid time\n",
@2.first_line
);
}
}
;
values
: values INT
|
;
%%
int isValidTime(char *time) {
return 0;
}
int isValidDate(char *date) {
return 0;
}
lex.yy.l
%{
#include "y.tab.h"
%}
c [0-9]
%%
[a-zA-Z][a-zA-Z0-9]* {return IDF;}
{c}{c}:{c}{c}:{c}{c} {
yylval.string = yytext;
return TIME;
}
{c}{c}\/{c}{c}\/{c}{c} {
yylval.string = yytext;
return DATE;
}
{c}* {
yylval.integer = atoi(yytext);
return INT;
}
\n {return EOL;}
. {;}
%%
main.c
#include <stdio.h>
#include <stdlib.h>
#include "y.tab.h"
int main(void) { return yyparse(); }
input.txt
one two three four
01/01/01 00:00:00 1 2 3 4
01/01/01 00:00:00 1 2 3 4
01/01/01 00:00:00 1 2 3 4
01/01/01 00:00:00 1 2 3 4
01/01/01 00:00:00 1 2 3 4
01/01/01 00:00:00 1 2 3 4
01/01/01 00:00:00 1 2 3 4
01/01/01 00:00:00 1 2 3 4
01/01/01 00:00:00 1 2 3 4
01/01/01 00:00:00 1 2 3 4