From e6d1d66c8f011b4ab87fd35b62b49aa402ff0168 Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Sat, 14 Sep 2024 23:13:06 +0200 Subject: [PATCH] Updated grammar script and fix concat op --- resources/grammar/Makefile | 8 +- resources/grammar/c3.l | 452 +++++++++++++++-------------- resources/grammar/check_grammar.sh | 5 + resources/grammar/grammar.y | 28 +- 4 files changed, 256 insertions(+), 237 deletions(-) create mode 100755 resources/grammar/check_grammar.sh diff --git a/resources/grammar/Makefile b/resources/grammar/Makefile index 97dd4e220..ff325777e 100644 --- a/resources/grammar/Makefile +++ b/resources/grammar/Makefile @@ -1,7 +1,7 @@ c3yacc: grammar.y c3.l - bison -d grammar.y; cc -c grammar.tab.c - flex c3.l; gcc -c lex.yy.c - cc -o c3yacc lex.yy.o grammar.tab.o + flex --header-file=lex.yy.h -8 c3.l + bison -d grammar.y; + cc -O2 -o c3yacc lex.yy.c grammar.tab.c clean: - rm -f c3yacc grammar.output *.o grammar.tab.* lex.yy.c \ No newline at end of file + rm -f c3yacc grammar.output *.o grammar.tab.* lex.yy.* \ No newline at end of file diff --git a/resources/grammar/c3.l b/resources/grammar/c3.l index ac643645a..4dfc2de78 100644 --- a/resources/grammar/c3.l +++ b/resources/grammar/c3.l @@ -1,4 +1,7 @@ %option yylineno +%option bison-locations +%option reentrant +%option bison-bridge D [0-9] DU [0-9_] @@ -31,251 +34,250 @@ BINT {B}(_?{B})* %{ #include #include "grammar.tab.h" -void count(void); -int comment_level = 0; +typedef struct { + int comment_level; +} YY_Extra_Type; + +#define YY_EXTRA_TYPE YY_Extra_Type + +#define YY_USER_ACTION \ + yylloc->first_line = yylloc->last_line; \ + yylloc->first_column = yylloc->last_column; \ + for(int i = 0; yytext[i] != '\0'; i++) { \ + if(yytext[i] == '\n') { \ + yylloc->last_line++; \ + yylloc->last_column = 0; \ + } \ + else { \ + yylloc->last_column++; \ + } \ + } %} %% -"$alignof" { count(); return(CT_ALIGNOF); } -"$assert" { count(); return(CT_ASSERT); } -"$assignable" { count(); return(CT_ASSIGNABLE); } -"$case" { count(); return(CT_CASE); } -"$default" { count(); return(CT_DEFAULT); } -"$defined" { count(); return(CT_DEFINED); } -"$echo" { count(); return(CT_ECHO); } -"$else" { count(); return(CT_ELSE); } -"$endfor" { count(); return(CT_ENDFOR); } -"$endforeach" { count(); return(CT_ENDFOREACH); } -"$endif" { count(); return(CT_ENDIF); } -"$endswitch" { count(); return(CT_ENDSWITCH); } -"$error" { count(); return(CT_ERROR); } -"$eval" { count(); return(CT_EVAL); } -"$evaltype" { count(); return(CT_EVALTYPE); } -"$exec" { count(); return(CT_EXEC); } -"$extnameof" { count(); return(CT_EXTNAMEOF); } -"$feature" { count(); return(CT_FEATURE); } -"$for" { count(); return(CT_FOR); } -"$foreach" { count(); return(CT_FOREACH); } -"$if" { count(); return(CT_IF); } -"$is_const" { count(); return(CT_IS_CONST); } -"$include" { count(); return(CT_INCLUDE); } -"$nameof" { count(); return(CT_NAMEOF); } -"$offsetof" { count(); return(CT_OFFSETOF); } -"$qnameof" { count(); return(CT_QNAMEOF); } -"$sizeof" { count(); return(CT_SIZEOF); } -"$stringify" { count(); return(CT_STRINGIFY); } -"$switch" { count(); return(CT_SWITCH); } -"$typefrom" { count(); return(CT_TYPEFROM); } -"$typeof" { count(); return(CT_TYPEOF); } -"$vaarg" { count(); return(CT_VAARG); } -"$vaconst" { count(); return(CT_VACONST); } -"$vacount" { count(); return(CT_VACOUNT); } -"$vaexpr" { count(); return(CT_VAEXPR); } -"$varef" { count(); return(CT_VAREF); } -"$vasplat" { count(); return(CT_VASPLAT); } -"$vatype" { count(); return(CT_VATYPE); } -"/*" { count(); BEGIN(COMMENT); } +"$alignof" { return(CT_ALIGNOF); } +"$assert" { return(CT_ASSERT); } +"$assignable" { return(CT_ASSIGNABLE); } +"$case" { return(CT_CASE); } +"$default" { return(CT_DEFAULT); } +"$defined" { return(CT_DEFINED); } +"$echo" { return(CT_ECHO); } +"$else" { return(CT_ELSE); } +"$endfor" { return(CT_ENDFOR); } +"$endforeach" { return(CT_ENDFOREACH); } +"$endif" { return(CT_ENDIF); } +"$endswitch" { return(CT_ENDSWITCH); } +"$error" { return(CT_ERROR); } +"$eval" { return(CT_EVAL); } +"$evaltype" { return(CT_EVALTYPE); } +"$exec" { return(CT_EXEC); } +"$extnameof" { return(CT_EXTNAMEOF); } +"$feature" { return(CT_FEATURE); } +"$for" { return(CT_FOR); } +"$foreach" { return(CT_FOREACH); } +"$if" { return(CT_IF); } +"$is_const" { return(CT_IS_CONST); } +"$include" { return(CT_INCLUDE); } +"$nameof" { return(CT_NAMEOF); } +"$offsetof" { return(CT_OFFSETOF); } +"$qnameof" { return(CT_QNAMEOF); } +"$sizeof" { return(CT_SIZEOF); } +"$stringify" { return(CT_STRINGIFY); } +"$switch" { return(CT_SWITCH); } +"$typefrom" { return(CT_TYPEFROM); } +"$typeof" { return(CT_TYPEOF); } +"$vaarg" { return(CT_VAARG); } +"$vaconst" { return(CT_VACONST); } +"$vacount" { return(CT_VACOUNT); } +"$vaexpr" { return(CT_VAEXPR); } +"$varef" { return(CT_VAREF); } +"$vasplat" { return(CT_VASPLAT); } +"$vatype" { return(CT_VATYPE); } +"/*" { BEGIN(COMMENT); } { - "/*" { count(); comment_level++; } - "*"+"/" { count(); if (comment_level) { comment_level--; } else { BEGIN(INITIAL); } } - "*"+ { count(); } - [^/*\n]+ { count(); } - [/] { count(); } - \n { count(); } + "/*" { yyg->yyextra_r.comment_level++; } + "*"+"/" { if (yyg->yyextra_r.comment_level) { yyg->yyextra_r.comment_level--; } else { BEGIN(INITIAL); } } + "*"+ { } + [^/*\n]+ { } + [/] { } + \n { } } -\/\/.* { count(); } -"any" { count(); return(ANY); } -"anyfault" { count(); return(ANYFAULT); } -"asm" { count(); return(ASM); } -"assert" { count(); return(ASSERT); } -"bitstruct" { count(); return(BITSTRUCT); } -"bool" { count(); return(BOOL); } -"break" { count(); return(BREAK); } -"case" { count(); return(CASE); } -"catch" { count(); return(CATCH); } -"char" { count(); return(CHAR); } -"const" { count(); return(CONST); } -"continue" { count(); return(CONTINUE); } -"def" { count(); return(DEF); } -"default" { count(); return(DEFAULT); } -"defer" { count(); return(DEFER); } -"distinct" { count(); return(DISTINCT); } -"do" { count(); return(DO); } -"double" { count(); return(DOUBLE); } -"else" { count(); return(ELSE); } -"enum" { count(); return(ENUM); } -"extern" { count(); return(EXTERN); } -"false" { count(); return(FALSE); } -"fault" { count(); return(FAULT); } -"float" { count(); return(FLOAT); } -"bfloat16" { count(); return(BFLOAT16); } -"float16" { count(); return(FLOAT16); } -"float128" { count(); return(FLOAT128); } -"fn" { count(); return(FN); } -"for" { count(); return(FOR); } -"foreach" { count(); return(FOREACH); } -"foreach_r" { count(); return(FOREACH_R); } -"ichar" { count(); return(ICHAR); } -"if" { count(); return(IF); } -"import" { count(); return(IMPORT); } -"inline" { count(); return(INLINE); } -"int" { count(); return(INT); } -"int128" { count(); return(INT128); } -"interface" { count(); return(INTERFACE); } -"iptr" { count(); return(IPTR); } -"isz" { count(); return(ISZ); } -"long" { count(); return(LONG); } -"macro" { count(); return(MACRO); } -"module" { count(); return(MODULE); } -"nextcase" { count(); return(NEXTCASE); } -"null" { count(); return(NUL); } -"return" { count(); return(RETURN); } -"short" { count(); return(SHORT); } -"struct" { count(); return(STRUCT); } -"static" { count(); return(STATIC); } -"switch" { count(); return(SWITCH); } -"tlocal" { count(); return(TLOCAL); } -"true" { count(); return(TRUE); } -"try" { count(); return(TRY); } -"typeid" { count(); return(TYPEID); } -"uint" { count(); return(UINT); } -"uint128" { count(); return(UINT128); } -"ulong" { count(); return(ULONG); } -"union" { count(); return(UNION); } -"uptr" { count(); return(UPTR); } -"ushort" { count(); return(USHORT); } -"usz" { count(); return(USZ); } -"var" { count(); return(VAR); } -"void" { count(); return(VOID); } -"while" { count(); return(WHILE); } +\/\/.* { } +"any" { return(ANY); } +"anyfault" { return(ANYFAULT); } +"asm" { return(ASM); } +"assert" { return(ASSERT); } +"bitstruct" { return(BITSTRUCT); } +"bool" { return(BOOL); } +"break" { return(BREAK); } +"case" { return(CASE); } +"catch" { return(CATCH); } +"char" { return(CHAR); } +"const" { return(CONST); } +"continue" { return(CONTINUE); } +"def" { return(DEF); } +"default" { return(DEFAULT); } +"defer" { return(DEFER); } +"distinct" { return(DISTINCT); } +"do" { return(DO); } +"double" { return(DOUBLE); } +"else" { return(ELSE); } +"enum" { return(ENUM); } +"extern" { return(EXTERN); } +"false" { return(FALSE); } +"fault" { return(FAULT); } +"float" { return(FLOAT); } +"bfloat16" { return(BFLOAT16); } +"float16" { return(FLOAT16); } +"float128" { return(FLOAT128); } +"fn" { return(FN); } +"for" { return(FOR); } +"foreach" { return(FOREACH); } +"foreach_r" { return(FOREACH_R); } +"ichar" { return(ICHAR); } +"if" { return(IF); } +"import" { return(IMPORT); } +"inline" { return(INLINE); } +"int" { return(INT); } +"int128" { return(INT128); } +"interface" { return(INTERFACE); } +"iptr" { return(IPTR); } +"isz" { return(ISZ); } +"long" { return(LONG); } +"macro" { return(MACRO); } +"module" { return(MODULE); } +"nextcase" { return(NEXTCASE); } +"null" { return(NUL); } +"return" { return(RETURN); } +"short" { return(SHORT); } +"struct" { return(STRUCT); } +"static" { return(STATIC); } +"switch" { return(SWITCH); } +"tlocal" { return(TLOCAL); } +"true" { return(TRUE); } +"try" { return(TRY); } +"typeid" { return(TYPEID); } +"uint" { return(UINT); } +"uint128" { return(UINT128); } +"ulong" { return(ULONG); } +"union" { return(UNION); } +"uptr" { return(UPTR); } +"ushort" { return(USHORT); } +"usz" { return(USZ); } +"var" { return(VAR); } +"void" { return(VOID); } +"while" { return(WHILE); } -@{CONST} { count(); return(AT_CONST_IDENT); } -#{CONST} { count(); return(HASH_CONST_IDENT); } -${CONST} { count(); return(CT_CONST_IDENT); } -{CONST} { count(); return(CONST_IDENT); } -@{TYPE} { count(); return(AT_TYPE_IDENT); } -#{TYPE} { count(); return(HASH_TYPE_IDENT); } -${TYPE} { count(); return(CT_TYPE_IDENT); } -{TYPE} { count(); return(TYPE_IDENT); } -@{IDENTIFIER} { count(); return(AT_IDENT); } -#{IDENTIFIER} { count(); return(HASH_IDENT); } -${IDENTIFIER} { count(); return(CT_IDENT); } -{IDENTIFIER} { count(); return(IDENT); } -0[xX]{HINT}{INTTYPE}? { count(); return(INTEGER); } -0[oO]{OINT}{INTTYPE}? { count(); return(INTEGER); } -0[bB]{BINT}{INTTYPE}? { count(); return(INTEGER); } -{INT}{INTTYPE}? { count(); return(INTEGER); } -x\'{HEX}\' { count(); return(BYTES); } -x\"{HEX}\" { count(); return(BYTES); } -x\`{HEX}\` { count(); return(BYTES); } -b64\'{B64}\' { count(); return(BYTES); } -b64\"{B64}\" { count(); return(BYTES); } -b64\`{B64}\` { count(); return(BYTES); } +@{CONST} { return(AT_CONST_IDENT); } +#{CONST} { return(HASH_CONST_IDENT); } +${CONST} { return(CT_CONST_IDENT); } +{CONST} { return(CONST_IDENT); } +@{TYPE} { return(AT_TYPE_IDENT); } +#{TYPE} { return(HASH_TYPE_IDENT); } +${TYPE} { return(CT_TYPE_IDENT); } +{TYPE} { return(TYPE_IDENT); } +@{IDENTIFIER} { return(AT_IDENT); } +#{IDENTIFIER} { return(HASH_IDENT); } +${IDENTIFIER} { return(CT_IDENT); } +{IDENTIFIER} { return(IDENT); } +0[xX]{HINT}{INTTYPE}? { return(INTEGER); } +0[oO]{OINT}{INTTYPE}? { return(INTEGER); } +0[bB]{BINT}{INTTYPE}? { return(INTEGER); } +{INT}{INTTYPE}? { return(INTEGER); } +x\'{HEX}\' { return(BYTES); } +x\"{HEX}\" { return(BYTES); } +x\`{HEX}\` { return(BYTES); } +b64\'{B64}\' { return(BYTES); } +b64\"{B64}\" { return(BYTES); } +b64\`{B64}\` { return(BYTES); } -{INT}{REALTYPE} { count(); return(REAL); } -{INT}{E}{REALTYPE}? { count(); return(REAL); } -0[xX]{HINT}{P}{REALTYPE}? { count(); return(REAL); } -{INT}"."{INT}{E}?{REALTYPE}? { count(); return(REAL); } -0[xX]{HINT}"."{HINT}{P}{REALTYPE}? { count(); return(REAL); } +{INT}{REALTYPE} { return(REAL); } +{INT}{E}{REALTYPE}? { return(REAL); } +0[xX]{HINT}{P}{REALTYPE}? { return(REAL); } +{INT}"."{INT}{E}?{REALTYPE}? { return(REAL); } +0[xX]{HINT}"."{HINT}{P}{REALTYPE}? { return(REAL); } -\"(\\.|[^\\"])*\" { count(); return(STRING_LITERAL); } -\'(\\[ux]{HEX}|\\.|[^\\'])\' { count(); return(CHAR_LITERAL); } +\"(\\.|[^\\"])*\" { return(STRING_LITERAL); } +\'(\\[ux]{HEX}|\\.|[^\\']+)\' { return(CHAR_LITERAL); } -"`" { count(); BEGIN(RAW_STRING); } +"`" { BEGIN(RAW_STRING); } { - "``" { count(); } - "`" { count(); BEGIN(INITIAL); return(STRING_LITERAL); } - "[^`]"+ { count(); } + "``" { } + "`" { BEGIN(INITIAL); return(STRING_LITERAL); } + "[^`]"+ { } <> { BEGIN(INITIAL); return(RAW_STRING); } } -"!!" { count(); return(BANGBANG); } -"..." { count(); return(ELLIPSIS); } -".." { count(); return(DOTDOT); } -"&&&" { count(); return(CT_AND_OP); } -"|||" { count(); return(CT_OR_OP); } -"+++" { count(); return(CT_CONCAT_OP); } -">>=" { count(); return(SHR_ASSIGN); } -"<<=" { count(); return(SHL_ASSIGN); } -"+=" { count(); return(ADD_ASSIGN); } -"-=" { count(); return(SUB_ASSIGN); } -"*=" { count(); return(MUL_ASSIGN); } -"/=" { count(); return(DIV_ASSIGN); } -"%=" { count(); return(MOD_ASSIGN); } -"&=" { count(); return(AND_ASSIGN); } -"^=" { count(); return(XOR_ASSIGN); } -"|=" { count(); return(OR_ASSIGN); } -">>" { count(); return(SHR_OP); } -"<<" { count(); return(SHL_OP); } -"++" { count(); return(INC_OP); } -"--" { count(); return(DEC_OP); } -"&&" { count(); return(AND_OP); } -"||" { count(); return(OR_OP); } -"<=" { count(); return(LE_OP); } -">=" { count(); return(GE_OP); } -"==" { count(); return(EQ_OP); } -"!=" { count(); return(NE_OP); } -"??" { count(); return(OPTELSE); } -"::" { count(); return(SCOPE); } -"?:" { count(); return(ELVIS); } -"=>" { count(); return(IMPLIES); } -"[<" { count(); return(LVEC); } -">]" { count(); return(RVEC); } -"(<" { count(); return(LGENPAR); } -">)" { count(); return(RGENPAR); } -"$$" { count(); return(BUILTIN); } -";" { count(); return(';'); } -("{") { count(); return('{'); } -("}") { count(); return('}'); } -"," { count(); return(','); } -":" { count(); return(':'); } -"=" { count(); return('='); } -"(" { count(); return('('); } -")" { count(); return(')'); } -("[") { count(); return('['); } -("]") { count(); return(']'); } -"." { count(); return('.'); } -"&" { count(); return('&'); } -"!" { count(); return('!'); } -"~" { count(); return('~'); } -"-" { count(); return('-'); } -"+" { count(); return('+'); } -"*" { count(); return('*'); } -"/" { count(); return('/'); } -"%" { count(); return('%'); } -"<" { count(); return('<'); } -">" { count(); return('>'); } -"^" { count(); return('^'); } -"|" { count(); return('|'); } -"?" { count(); return('?'); } -"{|" { count(); return(LBRAPIPE); } -"|}" { count(); return(RBRAPIPE); } -[ \t\v\n\f] { count(); } +"!!" { return(BANGBANG); } +"..." { return(ELLIPSIS); } +".." { return(DOTDOT); } +"&&&" { return(CT_AND_OP); } +"|||" { return(CT_OR_OP); } +"+++" { return(CT_CONCAT_OP); } +">>=" { return(SHR_ASSIGN); } +"<<=" { return(SHL_ASSIGN); } +"+=" { return(ADD_ASSIGN); } +"-=" { return(SUB_ASSIGN); } +"*=" { return(MUL_ASSIGN); } +"/=" { return(DIV_ASSIGN); } +"%=" { return(MOD_ASSIGN); } +"&=" { return(AND_ASSIGN); } +"^=" { return(XOR_ASSIGN); } +"|=" { return(OR_ASSIGN); } +">>" { return(SHR_OP); } +"<<" { return(SHL_OP); } +"++" { return(INC_OP); } +"--" { return(DEC_OP); } +"&&" { return(AND_OP); } +"||" { return(OR_OP); } +"<=" { return(LE_OP); } +">=" { return(GE_OP); } +"==" { return(EQ_OP); } +"!=" { return(NE_OP); } +"??" { return(OPTELSE); } +"::" { return(SCOPE); } +"?:" { return(ELVIS); } +"=>" { return(IMPLIES); } +"[<" { return(LVEC); } +">]" { return(RVEC); } +"(<" { return(LGENPAR); } +">)" { return(RGENPAR); } +"$$" { return(BUILTIN); } +";" { return(';'); } +("{") { return('{'); } +("}") { return('}'); } +"," { return(','); } +":" { return(':'); } +"=" { return('='); } +"(" { return('('); } +")" { return(')'); } +("[") { return('['); } +("]") { return(']'); } +"." { return('.'); } +"&" { return('&'); } +"!" { return('!'); } +"~" { return('~'); } +"-" { return('-'); } +"+" { return('+'); } +"*" { return('*'); } +"/" { return('/'); } +"%" { return('%'); } +"<" { return('<'); } +">" { return('>'); } +"^" { return('^'); } +"|" { return('|'); } +"?" { return('?'); } +"{|" { return(LBRAPIPE); } +"|}" { return(RBRAPIPE); } +[ \t\v\n\f] { } . { /* ignore bad characters */ } %% -int yywrap(void) +int yywrap(yyscan_t yyscanner) { return 1; } - -int column = 0; - -void count(void) -{ - int i; - - for (i = 0; yytext[i] != '\0'; i++) - if (yytext[i] == '\n') - column = 0; - else if (yytext[i] == '\t') - column += 8 - (column % 8); - else - column++; - - ECHO; -} diff --git a/resources/grammar/check_grammar.sh b/resources/grammar/check_grammar.sh new file mode 100755 index 000000000..eb48d5f84 --- /dev/null +++ b/resources/grammar/check_grammar.sh @@ -0,0 +1,5 @@ +for fn in `find ../../../c3c -name '*.c3' | sort` +do + echo -n $fn + cat $fn | ./c3yacc +done diff --git a/resources/grammar/grammar.y b/resources/grammar/grammar.y index 606663e65..068b39f50 100644 --- a/resources/grammar/grammar.y +++ b/resources/grammar/grammar.y @@ -1,14 +1,15 @@ %{ #include +#include "grammar.tab.h" +#include "lex.yy.h" #define YYERROR_VERBOSE -int yydebug = 1; -extern char yytext[]; -extern int column, yylineno; -int yylex(void); -void yyerror(const char *s); +void yyerror(YYLTYPE * yylloc_param , yyscan_t yyscanner, const char *yymsgp); %} %locations +%pure-parser +%lex-param {void *scanner} +%parse-param {void *scanner} %token IDENT HASH_IDENT CT_IDENT CONST_IDENT %token TYPE_IDENT CT_TYPE_IDENT @@ -289,6 +290,7 @@ bit_stmt_expr additive_op : '+' | '-' + | CT_CONCAT_OP ; additive_expr @@ -1273,15 +1275,25 @@ top_level %% -void yyerror(const char *s) +void yyerror(YYLTYPE * yylloc_param , yyscan_t yyscanner, const char *yymsgp) { fflush(stdout); - printf(":%d:%d:\n%*s\n%*s\n", yylineno, column, column, "^", column, s); + printf(":%d:%d:\n%*s\n%*s\n", yylloc_param->first_line, + yylloc_param->first_column, + yylloc_param->first_column, "^", yylloc_param->first_column, yymsgp); } int main(int argc, char *argv[]) { - int rc = yyparse(); + int rc; + yyscan_t scanner; + rc = yylex_init (&scanner); + if(rc) { + printf(" Failed to initialize the scanner: %d\n", rc); + return rc; + } + rc = yyparse (scanner); + yylex_destroy (scanner); printf(" -> yyparse return %d\n", rc); return rc; }