Try Catch (Fariz)

%{

#include “ccproject.tab.h”
#include<stdlib.h>

%}

%%

[0-9]+ {
yylval=atoi(yytext);
return NUM;
}

“try” { return TRY;}

“int” {return KEYWORD;}

“string” {return KEYWORD;}

“float” {return KEYWORD;}

“catch” { return CATCH;}

“exception” { return KEYWORD;}

“printf” { return KEYWORD;}

“Console.Write” {return KEYWORD;}

“.ToString()” {return CONVERT;}
[a-zA-Z]+ { yylval = *yytext;
return ID;
}
“{” { return OPENBRACE; }

“}” { return CLOSEBRACE;}

” ” { return SPACE;}

[()=;\n] return *yytext;

[\t]

. yyerror (“INVALID INPUT”);

%%
int yywrap(void)
{

return 1;

}

Bison

%{

#include<stdio.h>
void yyerror(char*s);

%}

%token NUM TRY ID CATCH KEYWORD OPENBRACE CLOSEBRACE OPENPAREN CLOSEPAREN SPACE CONVERT

%%

program:
program Statement’\n’
|
;

Statement:project {printf(“SYNTAX ACCEPTED\n”);}
project : TRY ‘\n’ OPENBRACE ‘\n’ E1′;’ ‘\n’ E1′;’ ‘\n’ CLOSEBRACE’\n’ catch_statement ‘\n’ OPENBRACE ‘\n’ E3′(‘ ID CONVERT ‘)”;’ ‘\n’ CLOSEBRACE

E1 : KEYWORD SPACE ID’=’NUM
| KEYWORD SPACE ID’=’ID
| ID
| KEYWORD'(‘ID CONVERT’)’
| NUM
;

E3 :KEYWORD
;
catch_statement : CATCH ‘(‘ type_expression’)’
;
type_expression :KEYWORD SPACE ID
;

%%

void yyerror(char*s) {

printf(“%s\n”,s);

}

int main(void) {

yyparse();
return 1;

}

 

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