Skip to content

Commit

Permalink
Merge pull request #5 from Bytom/new_line_in_statement
Browse files Browse the repository at this point in the history
support mutili new line in statement
  • Loading branch information
shenao78 committed Jul 21, 2021
2 parents 83d98d5 + 54c7a82 commit feff545
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions src/grammar.y
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ int yyerror(const char *s);
%%

stmt_list:
stmt {$$ = allocStatementList($1); }
| stmt_list '\n' stmt {$$ = chainStatementList($1, $3); }
stmt {$$ = allocStatementList($1); }
| stmt_list new_line stmt {$$ = chainStatementList($1, $3); }
;

stmt:
Expand All @@ -71,8 +71,12 @@ elseif:
;

block:
'{' stmt_list '}' { $$ = allocBlock($2); }
| '{' '}' { $$ = allocBlock(NULL); }
'{' stmt_list '}' { $$ = allocBlock($2); }
| '{' new_line stmt_list '}' { $$ = allocBlock($3); }
| '{' stmt_list new_line '}' { $$ = allocBlock($2); }
| '{' new_line stmt_list new_line '}' { $$ = allocBlock($3); }
| '{' new_line '}' { $$ = allocBlock(NULL); }
| '{' '}' { $$ = allocBlock(NULL); }
;

expr:
Expand Down Expand Up @@ -101,6 +105,10 @@ bool_expr:
| '!' expr %prec NOT { $$ = allocUnaryExpression(NOT_EXPRESSION, $2); }
;

new_line:
'\n'
| new_line '\n'

%%

int yyerror(char const *str) {
Expand All @@ -109,9 +117,15 @@ int yyerror(char const *str) {
return 0;
}

int main(void) {
if (yyparse()) {
exit(1);
int main(int argc, char *argv[]) {
extern FILE *yyin;
yyin = fopen(argv[1], "r");
if(yyin==NULL)
{
printf("fail to open file:%s\n", argv[1]);
} else
{
yyparse();
}
return 0;
}

0 comments on commit feff545

Please sign in to comment.