Array Syntax Acceptance Flex Bison

Flex File

%{
#include <stdio.h>
#include <stdlib.h>
#include “cal.tab.h”
%}
%%

“int” return INT;
“char” return CHAR;

[,;()\n] return *yytext;
[a-zA-Z]+ {yylval=*yytext;
return VARIABLE;}
[0-9]+ {yylval = atoi(yytext);
return NUM;}
“{” { return ‘{‘; }
“}” { return ‘}’; }
“[” { return ‘[‘; }
“]” { return ‘]’; }
“=” { return ‘=’; }
%%

int yywrap(void) {
return 1;
}

Bison

%{
#include <stdio.h>
#include <string.h>

void yyerror(const char *str)
{
fprintf(stderr,”error: %s\n”,str);
}

 

%}

%token INT
%token VARIABLE
%token CHAR NUM
%%
program : program statement ‘\n’
|
;

statement : INT VARIABLE ‘[‘NUM’]’ ‘;’ {printf(“ACCEPT\n”);}
| CHAR VARIABLE ‘[‘NUM’]’ ‘;’ {printf(“ACCEPT\n”);}
| INT VARIABLE ‘[‘NUM’]’ ‘=’ ‘{‘ exp ‘}’ ‘;’ {printf(“ACCEPT\n”);}
| CHAR VARIABLE ‘[‘NUM’]’ ‘=’ ‘{‘ expr ‘}’ ‘;’ {printf(“ACCEPT\n”);}
;

 

exp :NUM
|exp’,’ NUM
;

expr :VARIABLE
|expr ‘,’VARIABLE
;
%%
int main()
{
yyparse();
return 0;
}

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s