Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix arithm and assign grammar rules
  • Loading branch information
Dayof committed Oct 30, 2020
commit a192c1efeae0b75ca27690d46ba87fa93feab442
2 changes: 1 addition & 1 deletion src/core/ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ ast_node* print_exp(ast_node* node) {

void print_asts(ast_list* root) {
if (root == NULL) {
printf("Empty AST.\n");
printf("\nEmpty AST.\n");
return;
}

Expand Down
17 changes: 10 additions & 7 deletions src/parser/cppython.y
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,30 @@
%%

input : /* empty */ { create_empy_ast(); }
| input line

| input line { ; }
;

line : NEWLINE { create_empy_ast(); }
| stmt[U] NEWLINE { add_ast($U); }
| error NEWLINE { yyerrok; }
;

stmt : simple_stmt[U] { $$ = print_exp($U); }
;

simple_stmt : var[L] ASSIGN arith_expr[R] { $$ = create_bin_expr("=", $L, $R); }
;

var : ID[U] { $$ = create_var_expr($U); }
var : ID[U] { $$ = create_var_expr($U); }
;

arith_expr : term[L] ADD term[R] { $$ = create_bin_expr("+", $L, $R); }
| term[L] SUB term[R] { $$ = create_bin_expr("-", $L, $R); }
arith_expr : arith_expr[L] ADD term[R] { $$ = create_bin_expr("+", $L, $R); }
| arith_expr[L] SUB term[R] { $$ = create_bin_expr("-", $L, $R); }
| term[U] { $$ = print_exp($U); }
;

term : factor[L] MULT factor[R] { $$ = create_bin_expr("*", $L, $R); }
| factor[L] DIV factor[R] { $$ = create_bin_expr("/", $L, $R); }
term : term[L] MULT factor[R] { $$ = create_bin_expr("*", $L, $R); }
| term[L] DIV factor[R] { $$ = create_bin_expr("/", $L, $R); }
| factor[U] { $$ = print_exp($U); }
;

Expand Down
64 changes: 35 additions & 29 deletions src/parser/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -521,8 +521,8 @@ static const yytype_int8 yytranslate[] =
/* YYRLINE[YYN] -- Source line where rule number YYN was defined. */
static const yytype_int8 yyrline[] =
{
0, 37, 37, 38, 41, 42, 43, 46, 48, 50,
52, 53, 54, 57, 58, 59, 62
0, 37, 37, 38, 41, 42, 43, 46, 49, 52,
55, 56, 57, 60, 61, 62, 65
};
#endif

Expand Down Expand Up @@ -562,8 +562,8 @@ static const yytype_int16 yytoknum[] =
static const yytype_int8 yypact[] =
{
-14, 0, -14, -2, -14, -14, -14, 3, -14, 7,
-14, -14, 8, -14, -14, -1, 1, 8, 8, 8,
8, -14, -14, -14, -14
-14, -14, 8, -14, -1, 1, -14, 8, 8, 8,
8, 1, 1, -14, -14
};

/* YYDEFACT[STATE-NUM] -- Default reduction number in state STATE-NUM.
Expand Down Expand Up @@ -1324,86 +1324,92 @@ yyparse (void)
#line 1325 "parser/parser.c"
break;

case 3:
#line 38 "parser/cppython.y"
{ ; }
#line 1331 "parser/parser.c"
break;

case 4:
#line 41 "parser/cppython.y"
{ create_empy_ast(); }
#line 1331 "parser/parser.c"
#line 1337 "parser/parser.c"
break;

case 5:
#line 42 "parser/cppython.y"
{ add_ast((yyvsp[-1].expression)); }
#line 1337 "parser/parser.c"
#line 1343 "parser/parser.c"
break;

case 6:
#line 43 "parser/cppython.y"
{ yyerrok; }
#line 1343 "parser/parser.c"
#line 1349 "parser/parser.c"
break;

case 7:
#line 46 "parser/cppython.y"
{ (yyval.expression) = print_exp((yyvsp[0].expression)); }
#line 1349 "parser/parser.c"
#line 1355 "parser/parser.c"
break;

case 8:
#line 48 "parser/cppython.y"
#line 49 "parser/cppython.y"
{ (yyval.expression) = create_bin_expr("=", (yyvsp[-2].expression), (yyvsp[0].expression)); }
#line 1355 "parser/parser.c"
#line 1361 "parser/parser.c"
break;

case 9:
#line 50 "parser/cppython.y"
{ (yyval.expression) = create_var_expr((yyvsp[0].var)); }
#line 1361 "parser/parser.c"
#line 52 "parser/cppython.y"
{ (yyval.expression) = create_var_expr((yyvsp[0].var)); }
#line 1367 "parser/parser.c"
break;

case 10:
#line 52 "parser/cppython.y"
#line 55 "parser/cppython.y"
{ (yyval.expression) = create_bin_expr("+", (yyvsp[-2].expression), (yyvsp[0].expression)); }
#line 1367 "parser/parser.c"
#line 1373 "parser/parser.c"
break;

case 11:
#line 53 "parser/cppython.y"
#line 56 "parser/cppython.y"
{ (yyval.expression) = create_bin_expr("-", (yyvsp[-2].expression), (yyvsp[0].expression)); }
#line 1373 "parser/parser.c"
#line 1379 "parser/parser.c"
break;

case 12:
#line 54 "parser/cppython.y"
#line 57 "parser/cppython.y"
{ (yyval.expression) = print_exp((yyvsp[0].expression)); }
#line 1379 "parser/parser.c"
#line 1385 "parser/parser.c"
break;

case 13:
#line 57 "parser/cppython.y"
#line 60 "parser/cppython.y"
{ (yyval.expression) = create_bin_expr("*", (yyvsp[-2].expression), (yyvsp[0].expression)); }
#line 1385 "parser/parser.c"
#line 1391 "parser/parser.c"
break;

case 14:
#line 58 "parser/cppython.y"
#line 61 "parser/cppython.y"
{ (yyval.expression) = create_bin_expr("/", (yyvsp[-2].expression), (yyvsp[0].expression)); }
#line 1391 "parser/parser.c"
#line 1397 "parser/parser.c"
break;

case 15:
#line 59 "parser/cppython.y"
#line 62 "parser/cppython.y"
{ (yyval.expression) = print_exp((yyvsp[0].expression)); }
#line 1397 "parser/parser.c"
#line 1403 "parser/parser.c"
break;

case 16:
#line 62 "parser/cppython.y"
#line 65 "parser/cppython.y"
{ (yyval.expression) = create_int_expr((yyvsp[0].int_value)); }
#line 1403 "parser/parser.c"
#line 1409 "parser/parser.c"
break;


#line 1407 "parser/parser.c"
#line 1413 "parser/parser.c"

default: break;
}
Expand Down Expand Up @@ -1635,7 +1641,7 @@ yyparse (void)
#endif
return yyresult;
}
#line 65 "parser/cppython.y"
#line 68 "parser/cppython.y"


void yyerror(const char *s) {
Expand Down
60 changes: 35 additions & 25 deletions src/parser/parser.output
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ Grammar

8 var: ID

9 arith_expr: term ADD term
10 | term SUB term
9 arith_expr: arith_expr ADD term
10 | arith_expr SUB term
11 | term

12 term: factor MULT factor
13 | factor DIV factor
12 term: term MULT factor
13 | term DIV factor
14 | factor

15 factor: INTEGER
Expand Down Expand Up @@ -61,10 +61,10 @@ Nonterminals, with rules where they appear
on right: 7
arith_expr <expression> (17)
on left: 9 10 11
on right: 7
on right: 7 9 10
term <expression> (18)
on left: 12 13 14
on right: 9 10 11
on right: 9 10 11 12 13
factor <expression> (19)
on left: 15
on right: 12 13 14
Expand Down Expand Up @@ -186,37 +186,37 @@ State 13
State 14

7 simple_stmt: var ASSIGN arith_expr .
9 arith_expr: arith_expr . ADD term
10 | arith_expr . SUB term

ADD shift, and go to state 17
SUB shift, and go to state 18

$default reduce using rule 7 (simple_stmt)


State 15

9 arith_expr: term . ADD term
10 | term . SUB term
11 | term .
11 arith_expr: term .
12 term: term . MULT factor
13 | term . DIV factor

ADD shift, and go to state 17
SUB shift, and go to state 18
MULT shift, and go to state 19
DIV shift, and go to state 20

$default reduce using rule 11 (arith_expr)


State 16

12 term: factor . MULT factor
13 | factor . DIV factor
14 | factor .

MULT shift, and go to state 19
DIV shift, and go to state 20
14 term: factor .

$default reduce using rule 14 (term)


State 17

9 arith_expr: term ADD . term
9 arith_expr: arith_expr ADD . term

INTEGER shift, and go to state 13

Expand All @@ -226,7 +226,7 @@ State 17

State 18

10 arith_expr: term SUB . term
10 arith_expr: arith_expr SUB . term

INTEGER shift, and go to state 13

Expand All @@ -236,7 +236,7 @@ State 18

State 19

12 term: factor MULT . factor
12 term: term MULT . factor

INTEGER shift, and go to state 13

Expand All @@ -245,7 +245,7 @@ State 19

State 20

13 term: factor DIV . factor
13 term: term DIV . factor

INTEGER shift, and go to state 13

Expand All @@ -254,27 +254,37 @@ State 20

State 21

9 arith_expr: term ADD term .
9 arith_expr: arith_expr ADD term .
12 term: term . MULT factor
13 | term . DIV factor

MULT shift, and go to state 19
DIV shift, and go to state 20

$default reduce using rule 9 (arith_expr)


State 22

10 arith_expr: term SUB term .
10 arith_expr: arith_expr SUB term .
12 term: term . MULT factor
13 | term . DIV factor

MULT shift, and go to state 19
DIV shift, and go to state 20

$default reduce using rule 10 (arith_expr)


State 23

12 term: factor MULT factor .
12 term: term MULT factor .

$default reduce using rule 12 (term)


State 24

13 term: factor DIV factor .
13 term: term DIV factor .

$default reduce using rule 13 (term)
6 changes: 3 additions & 3 deletions src/tests/parser/valid_1.ppy
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
x = 1
y = 2
z = 5 + 5 / 9
x = 5 + 2 - 1 / 9
z = 4 + 4 * 2 * 5 - 3 / 0
w = 9