Updated grammar script and fix concat op

This commit is contained in:
Christoffer Lerno
2024-09-14 23:13:06 +02:00
parent c3a5f5c0f0
commit e6d1d66c8f
4 changed files with 256 additions and 237 deletions

View File

@@ -1,7 +1,7 @@
c3yacc: grammar.y c3.l c3yacc: grammar.y c3.l
bison -d grammar.y; cc -c grammar.tab.c flex --header-file=lex.yy.h -8 c3.l
flex c3.l; gcc -c lex.yy.c bison -d grammar.y;
cc -o c3yacc lex.yy.o grammar.tab.o cc -O2 -o c3yacc lex.yy.c grammar.tab.c
clean: clean:
rm -f c3yacc grammar.output *.o grammar.tab.* lex.yy.c rm -f c3yacc grammar.output *.o grammar.tab.* lex.yy.*

View File

@@ -1,4 +1,7 @@
%option yylineno %option yylineno
%option bison-locations
%option reentrant
%option bison-bridge
D [0-9] D [0-9]
DU [0-9_] DU [0-9_]
@@ -31,251 +34,250 @@ BINT {B}(_?{B})*
%{ %{
#include <stdio.h> #include <stdio.h>
#include "grammar.tab.h" #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); } "$alignof" { return(CT_ALIGNOF); }
"$assert" { count(); return(CT_ASSERT); } "$assert" { return(CT_ASSERT); }
"$assignable" { count(); return(CT_ASSIGNABLE); } "$assignable" { return(CT_ASSIGNABLE); }
"$case" { count(); return(CT_CASE); } "$case" { return(CT_CASE); }
"$default" { count(); return(CT_DEFAULT); } "$default" { return(CT_DEFAULT); }
"$defined" { count(); return(CT_DEFINED); } "$defined" { return(CT_DEFINED); }
"$echo" { count(); return(CT_ECHO); } "$echo" { return(CT_ECHO); }
"$else" { count(); return(CT_ELSE); } "$else" { return(CT_ELSE); }
"$endfor" { count(); return(CT_ENDFOR); } "$endfor" { return(CT_ENDFOR); }
"$endforeach" { count(); return(CT_ENDFOREACH); } "$endforeach" { return(CT_ENDFOREACH); }
"$endif" { count(); return(CT_ENDIF); } "$endif" { return(CT_ENDIF); }
"$endswitch" { count(); return(CT_ENDSWITCH); } "$endswitch" { return(CT_ENDSWITCH); }
"$error" { count(); return(CT_ERROR); } "$error" { return(CT_ERROR); }
"$eval" { count(); return(CT_EVAL); } "$eval" { return(CT_EVAL); }
"$evaltype" { count(); return(CT_EVALTYPE); } "$evaltype" { return(CT_EVALTYPE); }
"$exec" { count(); return(CT_EXEC); } "$exec" { return(CT_EXEC); }
"$extnameof" { count(); return(CT_EXTNAMEOF); } "$extnameof" { return(CT_EXTNAMEOF); }
"$feature" { count(); return(CT_FEATURE); } "$feature" { return(CT_FEATURE); }
"$for" { count(); return(CT_FOR); } "$for" { return(CT_FOR); }
"$foreach" { count(); return(CT_FOREACH); } "$foreach" { return(CT_FOREACH); }
"$if" { count(); return(CT_IF); } "$if" { return(CT_IF); }
"$is_const" { count(); return(CT_IS_CONST); } "$is_const" { return(CT_IS_CONST); }
"$include" { count(); return(CT_INCLUDE); } "$include" { return(CT_INCLUDE); }
"$nameof" { count(); return(CT_NAMEOF); } "$nameof" { return(CT_NAMEOF); }
"$offsetof" { count(); return(CT_OFFSETOF); } "$offsetof" { return(CT_OFFSETOF); }
"$qnameof" { count(); return(CT_QNAMEOF); } "$qnameof" { return(CT_QNAMEOF); }
"$sizeof" { count(); return(CT_SIZEOF); } "$sizeof" { return(CT_SIZEOF); }
"$stringify" { count(); return(CT_STRINGIFY); } "$stringify" { return(CT_STRINGIFY); }
"$switch" { count(); return(CT_SWITCH); } "$switch" { return(CT_SWITCH); }
"$typefrom" { count(); return(CT_TYPEFROM); } "$typefrom" { return(CT_TYPEFROM); }
"$typeof" { count(); return(CT_TYPEOF); } "$typeof" { return(CT_TYPEOF); }
"$vaarg" { count(); return(CT_VAARG); } "$vaarg" { return(CT_VAARG); }
"$vaconst" { count(); return(CT_VACONST); } "$vaconst" { return(CT_VACONST); }
"$vacount" { count(); return(CT_VACOUNT); } "$vacount" { return(CT_VACOUNT); }
"$vaexpr" { count(); return(CT_VAEXPR); } "$vaexpr" { return(CT_VAEXPR); }
"$varef" { count(); return(CT_VAREF); } "$varef" { return(CT_VAREF); }
"$vasplat" { count(); return(CT_VASPLAT); } "$vasplat" { return(CT_VASPLAT); }
"$vatype" { count(); return(CT_VATYPE); } "$vatype" { return(CT_VATYPE); }
"/*" { count(); BEGIN(COMMENT); } "/*" { BEGIN(COMMENT); }
<COMMENT>{ <COMMENT>{
"/*" { count(); comment_level++; } "/*" { yyg->yyextra_r.comment_level++; }
"*"+"/" { count(); if (comment_level) { comment_level--; } else { BEGIN(INITIAL); } } "*"+"/" { if (yyg->yyextra_r.comment_level) { yyg->yyextra_r.comment_level--; } else { BEGIN(INITIAL); } }
"*"+ { count(); } "*"+ { }
[^/*\n]+ { count(); } [^/*\n]+ { }
[/] { count(); } [/] { }
\n { count(); } \n { }
} }
\/\/.* { count(); } \/\/.* { }
"any" { count(); return(ANY); } "any" { return(ANY); }
"anyfault" { count(); return(ANYFAULT); } "anyfault" { return(ANYFAULT); }
"asm" { count(); return(ASM); } "asm" { return(ASM); }
"assert" { count(); return(ASSERT); } "assert" { return(ASSERT); }
"bitstruct" { count(); return(BITSTRUCT); } "bitstruct" { return(BITSTRUCT); }
"bool" { count(); return(BOOL); } "bool" { return(BOOL); }
"break" { count(); return(BREAK); } "break" { return(BREAK); }
"case" { count(); return(CASE); } "case" { return(CASE); }
"catch" { count(); return(CATCH); } "catch" { return(CATCH); }
"char" { count(); return(CHAR); } "char" { return(CHAR); }
"const" { count(); return(CONST); } "const" { return(CONST); }
"continue" { count(); return(CONTINUE); } "continue" { return(CONTINUE); }
"def" { count(); return(DEF); } "def" { return(DEF); }
"default" { count(); return(DEFAULT); } "default" { return(DEFAULT); }
"defer" { count(); return(DEFER); } "defer" { return(DEFER); }
"distinct" { count(); return(DISTINCT); } "distinct" { return(DISTINCT); }
"do" { count(); return(DO); } "do" { return(DO); }
"double" { count(); return(DOUBLE); } "double" { return(DOUBLE); }
"else" { count(); return(ELSE); } "else" { return(ELSE); }
"enum" { count(); return(ENUM); } "enum" { return(ENUM); }
"extern" { count(); return(EXTERN); } "extern" { return(EXTERN); }
"false" { count(); return(FALSE); } "false" { return(FALSE); }
"fault" { count(); return(FAULT); } "fault" { return(FAULT); }
"float" { count(); return(FLOAT); } "float" { return(FLOAT); }
"bfloat16" { count(); return(BFLOAT16); } "bfloat16" { return(BFLOAT16); }
"float16" { count(); return(FLOAT16); } "float16" { return(FLOAT16); }
"float128" { count(); return(FLOAT128); } "float128" { return(FLOAT128); }
"fn" { count(); return(FN); } "fn" { return(FN); }
"for" { count(); return(FOR); } "for" { return(FOR); }
"foreach" { count(); return(FOREACH); } "foreach" { return(FOREACH); }
"foreach_r" { count(); return(FOREACH_R); } "foreach_r" { return(FOREACH_R); }
"ichar" { count(); return(ICHAR); } "ichar" { return(ICHAR); }
"if" { count(); return(IF); } "if" { return(IF); }
"import" { count(); return(IMPORT); } "import" { return(IMPORT); }
"inline" { count(); return(INLINE); } "inline" { return(INLINE); }
"int" { count(); return(INT); } "int" { return(INT); }
"int128" { count(); return(INT128); } "int128" { return(INT128); }
"interface" { count(); return(INTERFACE); } "interface" { return(INTERFACE); }
"iptr" { count(); return(IPTR); } "iptr" { return(IPTR); }
"isz" { count(); return(ISZ); } "isz" { return(ISZ); }
"long" { count(); return(LONG); } "long" { return(LONG); }
"macro" { count(); return(MACRO); } "macro" { return(MACRO); }
"module" { count(); return(MODULE); } "module" { return(MODULE); }
"nextcase" { count(); return(NEXTCASE); } "nextcase" { return(NEXTCASE); }
"null" { count(); return(NUL); } "null" { return(NUL); }
"return" { count(); return(RETURN); } "return" { return(RETURN); }
"short" { count(); return(SHORT); } "short" { return(SHORT); }
"struct" { count(); return(STRUCT); } "struct" { return(STRUCT); }
"static" { count(); return(STATIC); } "static" { return(STATIC); }
"switch" { count(); return(SWITCH); } "switch" { return(SWITCH); }
"tlocal" { count(); return(TLOCAL); } "tlocal" { return(TLOCAL); }
"true" { count(); return(TRUE); } "true" { return(TRUE); }
"try" { count(); return(TRY); } "try" { return(TRY); }
"typeid" { count(); return(TYPEID); } "typeid" { return(TYPEID); }
"uint" { count(); return(UINT); } "uint" { return(UINT); }
"uint128" { count(); return(UINT128); } "uint128" { return(UINT128); }
"ulong" { count(); return(ULONG); } "ulong" { return(ULONG); }
"union" { count(); return(UNION); } "union" { return(UNION); }
"uptr" { count(); return(UPTR); } "uptr" { return(UPTR); }
"ushort" { count(); return(USHORT); } "ushort" { return(USHORT); }
"usz" { count(); return(USZ); } "usz" { return(USZ); }
"var" { count(); return(VAR); } "var" { return(VAR); }
"void" { count(); return(VOID); } "void" { return(VOID); }
"while" { count(); return(WHILE); } "while" { return(WHILE); }
@{CONST} { count(); return(AT_CONST_IDENT); } @{CONST} { return(AT_CONST_IDENT); }
#{CONST} { count(); return(HASH_CONST_IDENT); } #{CONST} { return(HASH_CONST_IDENT); }
${CONST} { count(); return(CT_CONST_IDENT); } ${CONST} { return(CT_CONST_IDENT); }
{CONST} { count(); return(CONST_IDENT); } {CONST} { return(CONST_IDENT); }
@{TYPE} { count(); return(AT_TYPE_IDENT); } @{TYPE} { return(AT_TYPE_IDENT); }
#{TYPE} { count(); return(HASH_TYPE_IDENT); } #{TYPE} { return(HASH_TYPE_IDENT); }
${TYPE} { count(); return(CT_TYPE_IDENT); } ${TYPE} { return(CT_TYPE_IDENT); }
{TYPE} { count(); return(TYPE_IDENT); } {TYPE} { return(TYPE_IDENT); }
@{IDENTIFIER} { count(); return(AT_IDENT); } @{IDENTIFIER} { return(AT_IDENT); }
#{IDENTIFIER} { count(); return(HASH_IDENT); } #{IDENTIFIER} { return(HASH_IDENT); }
${IDENTIFIER} { count(); return(CT_IDENT); } ${IDENTIFIER} { return(CT_IDENT); }
{IDENTIFIER} { count(); return(IDENT); } {IDENTIFIER} { return(IDENT); }
0[xX]{HINT}{INTTYPE}? { count(); return(INTEGER); } 0[xX]{HINT}{INTTYPE}? { return(INTEGER); }
0[oO]{OINT}{INTTYPE}? { count(); return(INTEGER); } 0[oO]{OINT}{INTTYPE}? { return(INTEGER); }
0[bB]{BINT}{INTTYPE}? { count(); return(INTEGER); } 0[bB]{BINT}{INTTYPE}? { return(INTEGER); }
{INT}{INTTYPE}? { count(); return(INTEGER); } {INT}{INTTYPE}? { return(INTEGER); }
x\'{HEX}\' { count(); return(BYTES); } x\'{HEX}\' { return(BYTES); }
x\"{HEX}\" { count(); return(BYTES); } x\"{HEX}\" { return(BYTES); }
x\`{HEX}\` { count(); return(BYTES); } x\`{HEX}\` { return(BYTES); }
b64\'{B64}\' { count(); return(BYTES); } b64\'{B64}\' { return(BYTES); }
b64\"{B64}\" { count(); return(BYTES); } b64\"{B64}\" { return(BYTES); }
b64\`{B64}\` { count(); return(BYTES); } b64\`{B64}\` { return(BYTES); }
{INT}{REALTYPE} { count(); return(REAL); } {INT}{REALTYPE} { return(REAL); }
{INT}{E}{REALTYPE}? { count(); return(REAL); } {INT}{E}{REALTYPE}? { return(REAL); }
0[xX]{HINT}{P}{REALTYPE}? { count(); return(REAL); } 0[xX]{HINT}{P}{REALTYPE}? { return(REAL); }
{INT}"."{INT}{E}?{REALTYPE}? { count(); return(REAL); } {INT}"."{INT}{E}?{REALTYPE}? { return(REAL); }
0[xX]{HINT}"."{HINT}{P}{REALTYPE}? { count(); return(REAL); } 0[xX]{HINT}"."{HINT}{P}{REALTYPE}? { return(REAL); }
\"(\\.|[^\\"])*\" { count(); return(STRING_LITERAL); } \"(\\.|[^\\"])*\" { return(STRING_LITERAL); }
\'(\\[ux]{HEX}|\\.|[^\\'])\' { count(); return(CHAR_LITERAL); } \'(\\[ux]{HEX}|\\.|[^\\']+)\' { return(CHAR_LITERAL); }
"`" { count(); BEGIN(RAW_STRING); } "`" { BEGIN(RAW_STRING); }
<RAW_STRING>{ <RAW_STRING>{
"``" { count(); } "``" { }
"`" { count(); BEGIN(INITIAL); return(STRING_LITERAL); } "`" { BEGIN(INITIAL); return(STRING_LITERAL); }
"[^`]"+ { count(); } "[^`]"+ { }
<<EOF>> { BEGIN(INITIAL); return(RAW_STRING); } <<EOF>> { BEGIN(INITIAL); return(RAW_STRING); }
} }
"!!" { count(); return(BANGBANG); } "!!" { return(BANGBANG); }
"..." { count(); return(ELLIPSIS); } "..." { return(ELLIPSIS); }
".." { count(); return(DOTDOT); } ".." { return(DOTDOT); }
"&&&" { count(); return(CT_AND_OP); } "&&&" { return(CT_AND_OP); }
"|||" { count(); return(CT_OR_OP); } "|||" { return(CT_OR_OP); }
"+++" { count(); return(CT_CONCAT_OP); } "+++" { return(CT_CONCAT_OP); }
">>=" { count(); return(SHR_ASSIGN); } ">>=" { return(SHR_ASSIGN); }
"<<=" { count(); return(SHL_ASSIGN); } "<<=" { return(SHL_ASSIGN); }
"+=" { count(); return(ADD_ASSIGN); } "+=" { return(ADD_ASSIGN); }
"-=" { count(); return(SUB_ASSIGN); } "-=" { return(SUB_ASSIGN); }
"*=" { count(); return(MUL_ASSIGN); } "*=" { return(MUL_ASSIGN); }
"/=" { count(); return(DIV_ASSIGN); } "/=" { return(DIV_ASSIGN); }
"%=" { count(); return(MOD_ASSIGN); } "%=" { return(MOD_ASSIGN); }
"&=" { count(); return(AND_ASSIGN); } "&=" { return(AND_ASSIGN); }
"^=" { count(); return(XOR_ASSIGN); } "^=" { return(XOR_ASSIGN); }
"|=" { count(); return(OR_ASSIGN); } "|=" { return(OR_ASSIGN); }
">>" { count(); return(SHR_OP); } ">>" { return(SHR_OP); }
"<<" { count(); return(SHL_OP); } "<<" { return(SHL_OP); }
"++" { count(); return(INC_OP); } "++" { return(INC_OP); }
"--" { count(); return(DEC_OP); } "--" { return(DEC_OP); }
"&&" { count(); return(AND_OP); } "&&" { return(AND_OP); }
"||" { count(); return(OR_OP); } "||" { return(OR_OP); }
"<=" { count(); return(LE_OP); } "<=" { return(LE_OP); }
">=" { count(); return(GE_OP); } ">=" { return(GE_OP); }
"==" { count(); return(EQ_OP); } "==" { return(EQ_OP); }
"!=" { count(); return(NE_OP); } "!=" { return(NE_OP); }
"??" { count(); return(OPTELSE); } "??" { return(OPTELSE); }
"::" { count(); return(SCOPE); } "::" { return(SCOPE); }
"?:" { count(); return(ELVIS); } "?:" { return(ELVIS); }
"=>" { count(); return(IMPLIES); } "=>" { return(IMPLIES); }
"[<" { count(); return(LVEC); } "[<" { return(LVEC); }
">]" { count(); return(RVEC); } ">]" { return(RVEC); }
"(<" { count(); return(LGENPAR); } "(<" { return(LGENPAR); }
">)" { count(); return(RGENPAR); } ">)" { return(RGENPAR); }
"$$" { count(); return(BUILTIN); } "$$" { return(BUILTIN); }
";" { count(); return(';'); } ";" { return(';'); }
("{") { count(); return('{'); } ("{") { return('{'); }
("}") { count(); return('}'); } ("}") { return('}'); }
"," { count(); return(','); } "," { return(','); }
":" { count(); return(':'); } ":" { return(':'); }
"=" { count(); return('='); } "=" { return('='); }
"(" { count(); return('('); } "(" { return('('); }
")" { count(); return(')'); } ")" { return(')'); }
("[") { count(); return('['); } ("[") { return('['); }
("]") { count(); return(']'); } ("]") { return(']'); }
"." { count(); return('.'); } "." { return('.'); }
"&" { count(); return('&'); } "&" { return('&'); }
"!" { count(); return('!'); } "!" { return('!'); }
"~" { count(); return('~'); } "~" { return('~'); }
"-" { count(); return('-'); } "-" { return('-'); }
"+" { count(); return('+'); } "+" { return('+'); }
"*" { count(); return('*'); } "*" { return('*'); }
"/" { count(); return('/'); } "/" { return('/'); }
"%" { count(); return('%'); } "%" { return('%'); }
"<" { count(); return('<'); } "<" { return('<'); }
">" { count(); return('>'); } ">" { return('>'); }
"^" { count(); return('^'); } "^" { return('^'); }
"|" { count(); return('|'); } "|" { return('|'); }
"?" { count(); return('?'); } "?" { return('?'); }
"{|" { count(); return(LBRAPIPE); } "{|" { return(LBRAPIPE); }
"|}" { count(); return(RBRAPIPE); } "|}" { return(RBRAPIPE); }
[ \t\v\n\f] { count(); } [ \t\v\n\f] { }
. { /* ignore bad characters */ } . { /* ignore bad characters */ }
%% %%
int yywrap(void) int yywrap(yyscan_t yyscanner)
{ {
return 1; 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;
}

View File

@@ -0,0 +1,5 @@
for fn in `find ../../../c3c -name '*.c3' | sort`
do
echo -n $fn
cat $fn | ./c3yacc
done

View File

@@ -1,14 +1,15 @@
%{ %{
#include <stdio.h> #include <stdio.h>
#include "grammar.tab.h"
#include "lex.yy.h"
#define YYERROR_VERBOSE #define YYERROR_VERBOSE
int yydebug = 1; void yyerror(YYLTYPE * yylloc_param , yyscan_t yyscanner, const char *yymsgp);
extern char yytext[];
extern int column, yylineno;
int yylex(void);
void yyerror(const char *s);
%} %}
%locations %locations
%pure-parser
%lex-param {void *scanner}
%parse-param {void *scanner}
%token IDENT HASH_IDENT CT_IDENT CONST_IDENT %token IDENT HASH_IDENT CT_IDENT CONST_IDENT
%token TYPE_IDENT CT_TYPE_IDENT %token TYPE_IDENT CT_TYPE_IDENT
@@ -289,6 +290,7 @@ bit_stmt_expr
additive_op additive_op
: '+' : '+'
| '-' | '-'
| CT_CONCAT_OP
; ;
additive_expr 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); 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 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); printf(" -> yyparse return %d\n", rc);
return rc; return rc;
} }