Merge pull request #63 from Servostar/parser-devel

Grand Pull-Request for parser rules in main
This commit is contained in:
Filleo 2024-05-12 21:23:00 +02:00 committed by GitHub
commit 9eff5fbdef
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 170 additions and 5 deletions

View File

@ -59,7 +59,7 @@ set(YACC_GENERATED_SOURCE_FILE ${PROJECT_SOURCE_DIR}/src/yacc/parser.tab.c)
add_custom_command(OUTPUT ${YACC_GENERATED_SOURCE_FILE} add_custom_command(OUTPUT ${YACC_GENERATED_SOURCE_FILE}
COMMAND yacc COMMAND yacc
ARGS -d -o ${YACC_GENERATED_SOURCE_FILE} ${YACC_SOURCE_FILE} ARGS -Wcounterexamples -d -o ${YACC_GENERATED_SOURCE_FILE} ${YACC_SOURCE_FILE}
COMMENT "generate C source file for parser" COMMENT "generate C source file for parser"
VERBATIM) VERBATIM)

View File

@ -15,6 +15,8 @@
%% %%
"\n" yyLineNumber++; "\n" yyLineNumber++;
#.* ;
":" {DEBUG("\"%s\" tokenized with \':\'", yytext); return(':');}; ":" {DEBUG("\"%s\" tokenized with \':\'", yytext); return(':');};
"=" {DEBUG("\"%s\" tokenized with \'=\'", yytext); return('=');}; "=" {DEBUG("\"%s\" tokenized with \'=\'", yytext); return('=');};
"+" {DEBUG("\"%s\" tokenized with \'+\'", yytext); return('+');}; "+" {DEBUG("\"%s\" tokenized with \'+\'", yytext); return('+');};
@ -24,7 +26,6 @@
"," {DEBUG("\"%s\" tokenized with \',\'", yytext); return(',');}; "," {DEBUG("\"%s\" tokenized with \',\'", yytext); return(',');};
";" {DEBUG("\"%s\" tokenized with \';\'", yytext); return(';');}; ";" {DEBUG("\"%s\" tokenized with \';\'", yytext); return(';');};
"." {DEBUG("\"%s\" tokenized with \'.\'", yytext); return('.');}; "." {DEBUG("\"%s\" tokenized with \'.\'", yytext); return('.');};
"#" {DEBUG("\"%s\" tokenized with \'#\'", yytext); return('#');};
"(" {DEBUG("\"%s\" tokenized with \'(\'", yytext); return('(');}; "(" {DEBUG("\"%s\" tokenized with \'(\'", yytext); return('(');};
")" {DEBUG("\"%s\" tokenized with \')\'", yytext); return(')');}; ")" {DEBUG("\"%s\" tokenized with \')\'", yytext); return(')');};
@ -35,9 +36,10 @@
">" {DEBUG("\"%s\" tokenized with \'>\'", yytext); return('>');}; ">" {DEBUG("\"%s\" tokenized with \'>\'", yytext); return('>');};
"<" {DEBUG("\"%s\" tokenized with \'<\'", yytext); return('<');}; "<" {DEBUG("\"%s\" tokenized with \'<\'", yytext); return('<');};
"int" {DEBUG("\"%s\" tokenized with \'<KeyInt'", yytext); return(KeyInt);}; "int" {DEBUG("\"%s\" tokenized with \'KeyInt'", yytext); return(KeyInt);};
"float" {DEBUG("\"%s\" tokenized with \'KeyFloat\'", yytext); return(KeyFloat);}; "float" {DEBUG("\"%s\" tokenized with \'KeyFloat\'", yytext); return(KeyFloat);};
"as" {DEBUG("\"%s\" tokenized with \'<KeyAs'", yytext); return (KeyAs);}; "self" {DEBUG("\"%s\" tokenized with \'KeySelf\'", yytext); return(KeySelf);};
"as" {DEBUG("\"%s\" tokenized with \'KeyAs'", yytext); return (KeyAs);};
"short" {DEBUG("\"%s\" tokenized with \'KeyShort\'", yytext); return(KeyShort);}; "short" {DEBUG("\"%s\" tokenized with \'KeyShort\'", yytext); return(KeyShort);};
"long" {DEBUG("\"%s\" tokenized with \'KeyLong\'", yytext); return(KeyLong);}; "long" {DEBUG("\"%s\" tokenized with \'KeyLong\'", yytext); return(KeyLong);};
"half" {DEBUG("\"%s\" tokenized with \'KeyHalf\'", yytext); return(KeyHalf);}; "half" {DEBUG("\"%s\" tokenized with \'KeyHalf\'", yytext); return(KeyHalf);};

View File

@ -13,6 +13,7 @@
%token KeyInt %token KeyInt
%token KeyFloat %token KeyFloat
%token KeySelf
%token KeyAs %token KeyAs
%token <string> ValInt %token <string> ValInt
%token <string> Ident %token <string> Ident
@ -55,8 +56,170 @@
%token FunLineno %token FunLineno
%token FunExtsupport %token FunExtsupport
/* Operator associativity */
%right '='
%left '+' '-' '*' '/'
%left OpEquals OpNot '<' '>'
%left OpAnd OpOr OpXor
%left OpBitand OpBitor OpBitxor OpBitnot
%% %%
program: ; program: program programbody
| programbody;
programbody: moduleimport
| fundef
| box
| definition
| decl
| typedef;
expr: ValFloat
| ValInt
| ValMultistr
| ValStr
| Ident
| operation
| boxaccess
| boxselfaccess;
exprlist: expr ',' exprlist
| expr;
argumentlist: argumentlist '(' exprlist ')'
| ;
fundef: KeyFun Ident paramlist '{' statementlist'}' { DEBUG("Function");};
paramlist: paramlist '(' params ')'
| paramlist '(' ')'
| '(' params ')'
| '(' ')';
params: IOqualifyier paramdecl ',' params
| IOqualifyier paramdecl;
IOqualifyier: KeyIn
| KeyOut
| KeyIn KeyOut
| KeyOut KeyIn
| ;
paramdecl: type ':' Ident { DEBUG("Param-Declaration"); };
box: KeyType KeyBox ':' Ident '{' boxbody '}' { DEBUG("Box"); }
| KeyType KeyBox ':' Ident '{' '}';
boxbody: boxbody boxcontent
| boxcontent;
boxcontent: decl { DEBUG("Box decl Content"); }
| definition { DEBUG("Box def Content"); }
| fundef { DEBUG("Box fun Content"); };
boxselfaccess: KeySelf '.' Ident
| KeySelf '.' boxaccess;
boxaccess: Ident '.' Ident
| Ident '.' boxaccess;
boxcall: boxaccess argumentlist
| boxselfaccess argumentlist;
funcall: Ident argumentlist { DEBUG("Function call"); };
moduleimport: KeyImport ValStr { DEBUG("Module-Import"); };
statementlist: statement statementlist
| statement;
statement: assign
| decl
| definition
| while
| branch
| funcall
| boxcall;
branchif: KeyIf expr '{' statementlist '}' { DEBUG("if"); };
branchelse: KeyElse '{' statementlist '}' { DEBUG("if-else"); };
branchelseif: KeyElse KeyIf expr '{' statementlist '}' { DEBUG("else-if"); };
branchelseifs: branchelseifs branchelseif
| branchelseif;
branch: branchif branchelseifs
| branchif branchelseifs branchelse;
while: KeyWhile expr '{' statementlist '}' { DEBUG("while"); };
identlist: Ident ',' identlist
| Ident;
decl: type ':' identlist
| storagequalifier type ':' identlist
definition: decl '=' expr { DEBUG("Definition"); };
storagequalifier: KeyGlobal
| KeyStatic
| KeyLocal;
assign: Ident '=' expr { DEBUG("Assignment"); }
| boxaccess '=' expr
| boxselfaccess '=' expr ;
sign: KeySigned
| KeyUnsigned;
typedef: KeyType type':' Ident;
scale: scale KeyShort
| scale KeyHalf
| scale KeyLong
| scale KeyDouble
| KeyShort
| KeyHalf
| KeyLong
| KeyDouble;
typekind: Ident
| KeyInt
| KeyFloat;
type: typekind
| scale typekind
| sign typekind
| sign scale typekind;
operation: oparith
| oplogic
| opbool
| opbit;
oparith: expr '+' expr
| expr '-' expr
| expr '*' expr
| expr '/' expr
| '-' expr %prec '*';
oplogic: expr OpEquals expr
| expr '<' expr
| expr '>' expr;
opbool: expr OpAnd expr
| expr OpOr expr
| expr OpXor expr
| OpNot expr %prec OpAnd;
opbit: expr OpBitand expr
| expr OpBitor expr
| expr OpBitxor expr
| OpBitnot expr %prec OpBitand;
%% %%
int yyerror(char *s) { int yyerror(char *s) {