-1

I'm trying to write my own scripting language using flex and bison. I have a basic parser and I would like to add a for statement very similar to the C language for statement. It is not clear to me how to code the action associated to the for statement

Suppose I have the following production for the 'for' statement:

for_loop: FOR '(' assignment ';' condition ';' compteur')' '{' list_instructions '}' {;}

  • 2
    Perhaps you could provide a clue as to how this differs from any other statement you are trying to parse. – rici Feb 12 '21 at 01:06
  • You might find https://stackoverflow.com/questions/15600056/for-loop-semantics-in-bison-yacc or https://stackoverflow.com/questions/19200808/parsing-a-while-loop-in-bison/19201452#19201452 useful – Chris Dodd Feb 12 '21 at 01:43

1 Answers1

-2
/// Lex program
%{
#include "ex5q1.tab.h" 
%}
aplha[a-zA-Z]
digit[0-9]
%% 
[\t\n] {}
"for" return FOR;
{digit}+ return NUM;
{alpha}({alpha}|{digit})* return id;
"<=" return le;
">=" return ge;
"==" return eq;
"!=" return ne;
"&&" return and;
"||" return or;
. return yytext[0];
%%

int yywrap(){}

/// yacc program
%{
    
#include <stdio.h> 
#include <stdlib.h> 
int yylex(void); 
int yyerror(char* s);

%}

%token NUM id FOR le ge eq ne or and
%right '='
%left or and
%left '>' '<' le ge eq ne
%left '+' '-'
%left '*' '/'
%left '!'

%%
s:st{printf("valid Input");return 0;}
st: FOR'('E';'E2';'E')'DEF
;
DEF: '{'BODY'}'
|E';'
|st
|
;
BODY: BODY BODY
|E';'
|st
|
;
E: id'='E
|E'+'E
|E'-'E
|E'*'E
|E'/'E
|E'<'E
|E'>'E
|EleE
|EgeE
|EeqE
|EneE
|EorE
|EandE
|E'+'+'
|E'-''-'
|id
|NUM
;

E2:E'<'E
|E'>'E
|EleE
|EgeE
|EeqE
|EneE
|EorE
|EandE
;
%%

int main() {

printf("Enter the expression\n");

yyparse();
return 0;
}

int yyerror(char* s) {

printf("Expression is invalid\n");

}