Reputation: 163
This interpreter on ubuntu 20.04 has problem with new lines.
s1.l
%%
(a|b)* { strcpy(yylval.string, yytext);
return AB;
}
z return Z;
'\n' return yytext[0];
. return yytext[0];
%%
s1.y
%{
#include <stdio.h>
#include <string.h>
#define check(first, two) \
if (strcmp(first, two)) \
fprintf(stderr, " -------- no word of language S1\n"); \
else \
fprintf(stderr, " -------- word of language S1\n");
int yylex();
void yyerror();
%}
%union {
char string[100];
}
%token Z
%token <string> AB
%%
word: AB Z AB '\n' { check($1, $3); }
| word AB Z AB '\n' { check($2, $4); }
| error '\n'
;
%%
#include "lex.yy.c"
void yyerror(char *s)
{
fprintf(stderr, "no word of language S1\n");
}
When I am running the interpreter the new line character is not recognized.
./s1
aazbb
Upvotes: 0
Views: 291
Reputation: 126498
lex does not treat '
as a special character (denoting a single character), so when you have a pattern '\n'
it matches that literal 3-character sequence. Use "\n"
(or just \n
) instead.
Upvotes: 3
Reputation: 386
Your pattern is a new line surrounded by simple quotes, then lex (or flex) is expecting that to return a new line to the parser and it is normal that the new line is not recognized. You should simply remove the simple quotes as follows:
%%
(a|b)* { strcpy(yylval.string, yytext);
return AB;
}
z return Z;
\n return yytext[0];
. return yytext[0];
%%
Upvotes: 0