Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support mutili new line in statement #5

Merged
merged 2 commits into from
Jul 21, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}