lex file
%%
^((for)|(while))[(][)] return FORWHILE;
[ ]* return SPACE;
([a-z ]+[;])+ return LINE;
. ;
%%
yacc file
%start s
%token FORWHILE
%token SPACE
%token LINE
%%
s: FORWHILE SPACE '{' SPACE LINE SPACE '}' SPACE
{
printf("Data OK\n");
};
%%
#include <stdio.h>
#include <string.h>
#include "lex.yy.c"
int main()
{
return yyparse();
}
int yyerror()
{
printf("Error in data\n");
return 0;
}
This is what i tried.
I want that input for(){bla bla;bla bla;}
will result Data OK
.
I can't get it to work for some reason.
What is the problem?
Thanks
Update:
%start s
%token FOR
%token WHILE
%token LINE
%token DO
%%
s: forwhile | dowhile { printf("Data OK\n"); };
dowhile: do_stmt '{' lines '}' while_stmt;
forwhile: for_or_while_stmt '{' lines '}';
lines: line | lines line;
line: LINE;
for_or_while_stmt: for_stmt | while_stmt;
for_stmt: FOR'()';
while_stmt: WHILE'()';
do_stmt: DO;
%%
#include <stdio.h>
#include <string.h>
#include "lex.yy.c"
int main()
{
return yyparse();
}
int yyerror()
{
printf("Error in data\n");
return 0;
}
lex
%%
"while" return WHILE;
"for" return FOR;
"do" return DO;
[a-z ]+[;] return LINE;
. return yytext[0];
%%
I also added do while. but still couldn't get it right.