From 46de25f8cefb7521144ece10c875c24c616e7dcb Mon Sep 17 00:00:00 2001 From: Filleo Date: Mon, 15 Apr 2024 20:36:13 +0200 Subject: [PATCH] first batch of keywords and symbols --- src/lex/lexer.l | 59 ++++++++++++++++++++++++++++++- src/main.c | 7 ++++ src/yacc/parser.y | 88 ++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 152 insertions(+), 2 deletions(-) diff --git a/src/lex/lexer.l b/src/lex/lexer.l index a17888b..c09798d 100644 --- a/src/lex/lexer.l +++ b/src/lex/lexer.l @@ -6,6 +6,63 @@ int yylex(); %} +/*543fgrsdffsd*/ + %% +"\n" yyLineNumber++; + + +":" return(':'); +"=" return('='); +"|" return('|'); +"!" return('!'); +"+" return('+'); +"-" return('-'); +"*" return('*'); +"/" return('/'); + + +"(" return('('); +")" return(')'); +"[" return('['); +"]" return(']'); +"{" return('{'); +"}" return('}'); +">" return('>'); +"<" return('<'); + + +"int" return(KeyInt); +"float" return(KeyFloat); + +"as" return (KeyAs); + + +"short" return(KeyShort); +"long" return(KeyLong); +"half" return(KeyHalf); +"double" return(KeyDouble); +"signed" return(KeySigned); +"unsigned" return(Keyunsigned); + +"ref" return(KeyRef); +"type" return(KeyType); +"local" return(KeyLocal); +"global" return(KeyGlobal); +"static" return(KeyStatic); + +"if" return(KeyIf); +"else" return(KeyElse); + +"while" return(KeyWhile); + + + + + + + +[a-zA-Z_]+ { yylval.string = strdup(yytext); return(Ident); }; +[0-9]+ { yylval.num = atoi(yytext); return(ValInt); }; .; -%% \ No newline at end of file +%% diff --git a/src/main.c b/src/main.c index 6b48e00..27df7ed 100644 --- a/src/main.c +++ b/src/main.c @@ -1,6 +1,13 @@ #include +#include + +extern FILE* yyin; int main() { + FILE* input = fopen("program.gem", "r"); + + yyin = input; + yyparse(); return 0; } \ No newline at end of file diff --git a/src/yacc/parser.y b/src/yacc/parser.y index fbe3d5a..4552fbd 100644 --- a/src/yacc/parser.y +++ b/src/yacc/parser.y @@ -1,15 +1,101 @@ %{ + #include extern int yylineno; int yyerror(char*); extern int yylex(); + + int array[256]; %} +%union { + char *string; + int num; +} + + +%token KeyInt +%token KeyFloat + +%token KeyAs +%token ValInt +%token Ident + +%token KeyShort +%token KeyLong +%token KeyHalf +%token KeyDouble +%token KeySigned +%token Keyunsigned + +%token KeyRef +%token KeyType +%token KeyLocal +%token KeyGlobal +%token KeyStatic + +%token KeyIf +%token KeyElse + +%token KeyWhile + + + + %% -program: ; +program: decllist; + +decllist: decl + | decllist decl; + +decl: '=' {printf("=\n"); }; + | ':' {printf(":\n"); }; + | '|' {printf("|\n"); }; + | '(' {printf("(\n"); }; + | ')' {printf(")\n"); }; + | '[' {printf("[\n"); }; + | ']' {printf("]\n"); }; + | '{' {printf("{\n"); }; + | '}' {printf("}\n"); }; + | '!' {printf("!\n"); }; + + | '+' {printf("+\n"); }; + | '-' {printf("-\n"); }; + | '*' {printf("*\n"); }; + | '/' {printf("/\n"); }; + + + + + | Ident {printf("Ident\n"); }; + | ValInt {printf("ValInt\n"); }; + | KeyInt {printf("KeyInt\n"); }; + | KeyFloat {printf("KeyFloat\n"); }; + | KeyAs {printf("KeyAs\n"); }; + + | KeyShort {printf("KeyShort\n"); }; + | KeyLong {printf("KeyLong\n"); }; + | KeyHalf {printf("KeyHalf\n"); }; + | KeyDouble {printf("KeyDouble\n"); }; + | KeySigned {printf("KeySigned\n"); }; + | Keyunsigned {printf("Keyunsigned\n"); }; + + | KeyRef {printf("KeyRef\n"); }; + | KeyType {printf("KeyType\n"); }; + | KeyLocal {printf("KeyLocal\n"); }; + | KeyGlobal {printf("KeyGlobal\n"); }; + | KeyStatic {printf("KeyStatic\n"); }; + + | KeyIf {printf("KeyIf\n"); }; + | KeyElse {printf("KeyElse\n"); }; + + | KeyWhile {printf("KeyWhile\n"); }; + %% + + int yyerror(char *s) { return 0; } \ No newline at end of file