I am working on learning lex and the turtle language
I am having problems getting lex to recognize operators as tokens + , < , =
I have them stored in a file
I have tried doing it this way
\+ or "+" or '+' or [+]
I don’t get any errors using single quotes but it doesnt match it either or would i need to do that in the file?.
Here is my file
fd 3x00
bk
setc 100
int xy3 fd 10 rt 90
rt
lt
setxy
setx
sety
home
seth
pd
pu
ht
st
color
xcor
ycor
heading
random
:=
+
<
and here is the program
%{
#include <stdio.h>
#include <stdlib.h>
int number;
%}
%%
fd {printf("Keyword %s\n", yytext);}
[^\t\n\r] {}
[0-9]+[a-z]+[0-9]+ {printf("Illegal: 3x00\n");}
[\r\t\n]+ {}
bk {printf("Keyword: %s\n", yytext);}
setc {printf("Keyword: %s\n", yytext);}
[0-9]+ {printf("Number: %s\n", yytext);}
int {printf(" Keyword: %s\n", yytext);}
xy3 {printf("ID: %s\n", yytext);}
fd[0-9]+ {printf("Keyword: %s\n", yytext);
printf("Number %s\n", yytext);}
rt {printf("Keyword: %s\n", yytext);}
lt {printf("Keyword: %s\n",yytext);}
setxy {printf("Keyword: %s\n", yytext);}
setx {printf("Keyword: %s\n", yytext);}
sety {printf("Keyword: %s\n", yytext);}
home {printf("Keyword: %s\n", yytext);}
seth {printf("Keyword: %s\n", yytext);}
pd {printf("Keyword: %s\n", yytext);}
pu {printf("Keyword: %s\n", yytext);}
ht {printf("Keyword: %s\n", yytext);}
st {printf("Keyword: %s\n", yytext);}
color {printf("Keyword: %s\n", yytext);}
xcor {printf("Keyword: %s\n", yytext);}
ycor {printf("Keyword: %s\n", yytext);}
heading {printf("Keyword: %s\n", yytext);}
random {printf("Keyword: %s\n", yytext);}
:= {printf("Keyword: =\n" );}
\+ {printf("Keyword: + \n" );}
\< {printf("Keyword: < \n");}
%%
main( int argc,char** argv)
{
if(argc > 1)
{
FILE *file;
file = fopen(argv[1], "r");
if(!file)
{
fprintf(stderr, "Could not open %s \n", argv[1]);
exit(1);
}
yyin = file;
}
yylex();
}
Error i get is Warning, rule cannot be matched.
What am i doing wrong?
Thank you
I think I see the problem
The
+and<characters will match both the top rule ([^\t\n\r]) and the\+/\<rule. Since they are the same length, the lexer will take the first match,[^\t\n\r], so it will do nothing ({}) and it can never match the\+/\<rule, as it says. Take the[^\t\n\r]rule out and replace it with. {}at the very end of the token list.