Hatch

Create a programming language of your choice that will accomplish some computational task. Utilizing the C packages Flex and Bison, define a grammar for your language in the Backus-Naur form. Clearly define the tokens of your language and their types, as well as any rules your language will follow.

For my programming language, I chose to create a preprocessor for Python called Hatch. Hatch simplifies the syntax needed to define classes in Python. Before being interpreted as Python code the .Hatch file is converted to a .py file, and in the process Hatch statements are replaced with valid Python code. In order to accomplish this task, regular expressions are used to parse the language and are defined in the .l (Lex) file. A grammar tree for the language also needs to be defined in the .y (Yacc) file.


"#!"                        {yylval.sVal = strdup(yytext); return START;}

","                         {yylval.sVal = strdup(yytext); return COMMA;}

[0-9]+                      {yylval.sVal = strdup(yytext); return NUM_I;}

[1-9]+[0-9]?\.[0-9]+        {yylval.sVal = strdup(yytext); return NUM_F;}

[0]?\.[0-9]+                {yylval.sVal = strdup(yytext); return NUM_F;}

\"[a-zA-Z0-9 \\t\\n]+\"     {yylval.sVal = strdup(yytext); return STRING;}

\'[a-zA-Z0-9 \\t\\n]+\'     {yylval.sVal = strdup(yytext); return STRING;}

"get"                       {yylval.sVal = strdup(yytext); return GET;}

"set"                       {yylval.sVal = strdup(yytext); return SET;}

"class"                     {yylval.sVal = strdup(yytext); return CLASS;}

"str"                       {yylval.sVal = strdup(yytext); return STR;}

[_a-zA-Z][_a-zA-Z0-9]*      {yylval.sVal = strdup(yytext); return VARNAME;}

"="                         {yylval.sVal = strdup(yytext); return EQUALS;}

"("                         {yylval.sVal = strdup(yytext); return LPAREN;}

")"                         {yylval.sVal = strdup(yytext); return RPAREN;}                
                                

Some regular expressions used by Hatch to parse input.



assign: VARNAME EQUALS NUM_I{
    strcat($1, strcat($2, $3));
    $$ = $1;
    //std::cout << "VARNAME EQUALS NUM_I" << std::endl;
}
    |
        VARNAME EQUALS NUM_F{
        strcat($1, strcat($2, $3));
        $$ = $1;
        //std::cout << "VARNAME EQUALS NUM_F" << std::endl;
        }
                                

A small part of the Backus-Naur grammar tree for Hatch.