You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
80 lines
3.5 KiB
80 lines
3.5 KiB
%{
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include "y.tab.h"
|
|
|
|
void yyerror(char *);
|
|
|
|
extern YYSTYPE yylval;
|
|
extern FILE *yyin;
|
|
int yylineno;
|
|
int _DEBUG;
|
|
|
|
%}
|
|
%%
|
|
[//]* { ; }
|
|
-?[0-9]+ { yylval.num = atoi(yytext); return INTEGER; }
|
|
\'[ -~]\' { yylval.character = yytext[1]; return CHAR; }
|
|
[-()<>=+*/:;,%{}\[\]] { return *yytext; }
|
|
"if" { return IF; }
|
|
"else" { return ELSE; }
|
|
"printf" { return PRINTF; }
|
|
"string" { return KEYWORD_S; }
|
|
"while" { return WHILE; }
|
|
"int" { return KEYWORD_I; }
|
|
"def" { return DEF;}
|
|
"call" { return CALL; }
|
|
"char" { return KEYWORD_C; }
|
|
"++" { return INCREASE; }
|
|
"--" { return DECREASE; }
|
|
\"(\\.|[^"\\])*\" { yytext++; yytext[strlen(yytext)-1] = 0; yylval.str = strdup(yytext); return STRING; }
|
|
[a-zA-Z0-9_]+ { yylval.str = strdup(yytext); return VARNAME; }
|
|
-?[0-9]*\.[0-9]+ { yylval.Double = atof(yytext); return DOUBLEVAR; }
|
|
"\n" { yylineno++; }
|
|
[ ?\t\n\r]
|
|
. printf("invalid character on line %d, '%s'\n", yylineno, yytext);
|
|
<<EOF>> { exit(0); }
|
|
%%
|
|
|
|
int yywrap(void) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
int main(int argc, char* argv[])
|
|
{
|
|
FILE *fh;
|
|
if (argc > 1)
|
|
{
|
|
if (strcmp(argv[1],"help") == 0 || strcmp(argv[1],"-help") == 0
|
|
|| strcmp(argv[1],"--help") == 0 || strcmp(argv[1],"h") == 0
|
|
|| strcmp(argv[1],"-h") == 0)
|
|
{
|
|
printf("[ HELP ] put a file/path as the first flag.\n");
|
|
printf("[ HELP ] -h, -help, --help, help, h: Prints this.\n");
|
|
printf("[ HELP ] -d, d, -debug, debug: Enter REPL mode.\n");
|
|
}
|
|
else if (strcmp(argv[1],"-debug") == 0 || strcmp(argv[1],"-d") == 0
|
|
|| strcmp(argv[1],"d") == 0 || strcmp(argv[1],"debug") == 0)
|
|
{
|
|
_DEBUG = 1;
|
|
printf(">>> ");
|
|
yyparse();
|
|
}
|
|
else if (fh = fopen(argv[1], "r"))
|
|
{
|
|
yyin = fh;
|
|
yyparse();
|
|
fclose(fh);
|
|
}
|
|
else
|
|
{
|
|
printf("[ ERROR ] please enter a correct command. try --help\n");
|
|
fclose(fh);
|
|
}
|
|
} else
|
|
printf("[ ERROR ] missing input file. Try --help?\n");
|
|
return 0;
|
|
} |