mcc-python.opp.exp 22.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942
File "mcc-python.mly", line 144, characters 24-27:
Warning: the token EOF is unused.
File "mcc-python.mly", line 75, characters 24-28:
Warning: the token Exec is unused.
%{
open Python_ast
open Python_ast_util

let no_pos = ("", 0, 0, 0, 0)
let sys_maxint = Symbol.add "sys.maxint"
let ellipsis = Symbol.add "Ellipsis"
let zero = LiteralExpr ((IntLiteral (0, no_pos)), no_pos)
let none = Symbol.add "None"
let none_expr = IdExpr (none, no_pos)

exception Impossible
exception InconsistentDedent

(********** Position stuff **********)
(*
 * Combine two positions.
 *)
let union_pos
    (file1, sline1, schar1, eline1, echar1)
    (file2, sline2, schar2, eline2, echar2) =
   if file1 <> file2 then
      raise (Invalid_argument (**)
                (Printf.sprintf "union_pos: file mistmatch: \"%s\":\"%s\"" (**)
                    (String.escaped file1) (String.escaped file2)));
   let sline, schar =
      if sline1 < sline2 then
         sline1, schar1
      else if sline1 > sline2 then
         sline2, schar2
      else
         sline1, min schar1 schar2
   in
   let eline, echar =
      if eline1 > eline2 then
         eline1, echar1
      else if eline1 < eline2 then
         eline2, echar2
      else
         eline1, max echar1 echar2
   in
      file1, sline, schar, eline, echar

let fatal_error pos str =
   Python_position.print_pos pos;
   print_endline str;
   exit 1

let parse_error str =
   fatal_error
       !Python_position.position
       (Printf.sprintf "ParseError: `%s'" str)

%}
%start file_input
%token <Python_ast.pos> While
%token <Python_ast.pos> Try
%token <Python_ast.pos> Tilde
%token <string * Python_ast.pos> String
%token <Python_ast.pos> StarStarEqual
%token <Python_ast.pos> StarStar
%token <Python_ast.pos> StarEqual
%token <Python_ast.pos> Star
%token <Python_ast.pos> SlashEqual
%token <Python_ast.pos> Slash
%token <Python_ast.pos> Semicolon
%token <Python_ast.pos> RightshiftEqual
%token <Python_ast.pos> Rightshift
%token <Python_ast.pos> Rightparen
%token <Python_ast.pos> Rightbracket
%token <Python_ast.pos> Rightbrace
%token <Python_ast.pos> Return
%token <Python_ast.pos> Raise
%token <Python_ast.pos> Print
%token <Python_ast.pos> PlusEqual
%token <Python_ast.pos> Period
%token <Python_ast.pos> PercentEqual
%token <Python_ast.pos> Percent
%token <Python_ast.pos> Pass
%token <Python_ast.pos> Or
%token <Python_ast.pos> NotEqual
%token <Python_ast.pos> Newline
%token <string * Python_ast.pos> Longinteger
%token <Python_ast.pos> LessthanEq
%token <Python_ast.pos> Lessthan
%token <Python_ast.pos> LeftshiftEqual
%token <Python_ast.pos> Leftshift
%token <Python_ast.pos> Leftbrace
%token <Python_ast.pos> Lambda
%token <Python_ast.pos> Is
%token <int    * Python_ast.pos> Integer
%token <Python_ast.pos> Indent
%token <Python_ast.pos> In
%token <Python_ast.pos> Import
%token <float  * Python_ast.pos> Imaginary
%token <Python_ast.pos> If
%token <Symbol.symbol * Python_ast.pos> Identifier
%token <Python_ast.pos> GreaterthanEq
%token <Python_ast.pos> Greaterthan
%token <Python_ast.pos> Global
%token <Python_ast.pos> From
%token <Python_ast.pos> For
%token <float  * Python_ast.pos> Float
%token <Python_ast.pos> Finally
%token <Python_ast.pos> Exec
%token <Python_ast.pos> Except
%token <Python_ast.pos> EqualEqual
%token <Python_ast.pos> Equal
%token <Python_ast.pos> Else
%token <Python_ast.pos> Ellipsis
%token <Python_ast.pos> Elif
%token <Python_ast.pos> EOF
%token <Python_ast.pos> Del
%token <Python_ast.pos> Def
%token <Python_ast.pos> Dedent
%token <Python_ast.pos> DashEqual
%token <Python_ast.pos> Continue
%token <Python_ast.pos> Comma
%token <Python_ast.pos> Colon
%token <Python_ast.pos> Class
%token <Python_ast.pos> CaretEqual
%token <Python_ast.pos> Caret
%token <Python_ast.pos> Break
%token <Python_ast.pos> BarEqual
%token <Python_ast.pos> Bar
%token <Python_ast.pos> Assert
%token <Python_ast.pos> And
%token <Python_ast.pos> AmpersandEqual
%token <Python_ast.pos> Ampersand
%token <Python_ast.pos> Backquote
%token <Python_ast.pos> Plus
%token <Python_ast.pos> Not
%token <Python_ast.pos> Leftparen
%token <Python_ast.pos> Leftbracket
%token <Python_ast.pos> Dash
%nonassoc Backquote 
%nonassoc Plus Not Leftparen Leftbracket Dash 
%type <Python_ast.stmt list> file_input
%%

atom:
| Identifier
    { IdExpr (fst $1, snd $1)      }
| literal
    { LiteralExpr (fst $1, snd $1) }
| enclosure
    { $1			       }

enclosure:
| parenth_form
    { $1 }
| list_display
    { $1 }
| dict_display
    { $1 }
| string_conversion
    { $1 }

literal:
| String
    { StrLiteral     (fst $1, snd $1), snd $1 }
| Integer
    { IntLiteral     (fst $1, snd $1), snd $1 }
| Longinteger
    { LongIntLiteral (fst $1, snd $1), snd $1 }
| Float
    { FloatLiteral   (fst $1, snd $1), snd $1 }
| Imaginary
    { ImagLiteral    (fst $1, snd $1), snd $1 }

parenth_form:
| Leftparen Rightparen
    { TupleExpr ([], union_pos $1 $2) }
| Leftparen expr_list_without_comma Rightparen
    {  let (el,_) = $2 in
	 if List.length el = 1		(* If there was only one expression,  *)
	    then List.hd el		(* then this is just that expression. *)
				        (* Otherwise, it's a tuple.           *)
	    else TupleExpr (el, union_pos $1 $3)
      }
| Leftparen expr_list_without_comma Comma Rightparen
    { TupleExpr (fst $2, union_pos $1 $4) }

list_display:
| Leftbracket Rightbracket
    { ListExpr (List [], union_pos $1 $2) }
| Leftbracket listmaker Rightbracket
    { ListExpr ($2, union_pos $1 $3)      }

listmaker:
| expr_list
    { List (fst $1) }

dict_display:
| Leftbrace Rightbrace
    { DictExpr ([], union_pos $1 $2)     }
| Leftbrace key_datum_list Rightbrace
    { DictExpr (fst $2, union_pos $1 $3) }

key_datum_list:
| key_datum_list_without_comma
    { $1				 }
| key_datum_list_without_comma Comma
    { (fst $1, union_pos (snd $1) $2) }

key_datum_list_without_comma:
| key_datum
    { let (e1,e2,p) = $1 in ([(e1, e2)], p) }
| key_datum_list_without_comma Comma key_datum
    {  let (kdl,p1) = $1 in
	 let (kd1,kd2,p2) = $3 in
	 (kdl @ [(kd1, kd2)], union_pos p1 p2)
      }

key_datum:
| expression Colon expression
    { ($1, $3, union_pos (pos_of_expr $1) (pos_of_expr $3)) }

string_conversion:
| Backquote expr_list_without_comma Backquote
    { StrConvExpr (fst $2, union_pos $1 $3) }

primary:
| atom
    { $1 }
| attributeref
    { $1 }
| slicing
    { $1 }
| call
    { $1 }

attributeref:
| primary Period Identifier
    { AttrRefExpr ($1, fst $3, union_pos (pos_of_expr $1) (snd $3)) }

slicing:
| primary Leftbracket slice_list Rightbracket
    { SliceExpr ($1, fst $3, union_pos (pos_of_expr $1) $4) }

slice_list:
| slice_list_without_comma
    { $1                              }
| slice_list_without_comma Comma
    { (fst $1, union_pos (snd $1) $2) }

slice_list_without_comma:
| slice_item
    { ([fst $1], snd $1) }
| slice_list_without_comma Comma slice_item
    { ((fst $1) @ [fst $3], union_pos (snd $1) (snd $3)) }

slice_item:
| expression
    { ExprSlice ($1), pos_of_expr $1 }
| short_slice
    { $1 }
| short_slice Colon
    {  let ss, pos = $1 in
	 match ss with
	    SimpleSlice(lower, upper) ->
	       ExtSlice([lower; upper; IdExpr (none, no_pos)]), pos
	  | _ ->
	    fatal_error pos "InternalParserError: unexpected short_slice"
      }
| short_slice Colon expression
    {  let ss, pos = $1 in
	 match ss with
	    SimpleSlice(lower, upper) -> ExtSlice([lower; upper; $3]), pos
	  | _ ->
	    fatal_error pos "InternalParserError: unexpected short_slice"
      }
| Ellipsis
    { ExtSlice ([IdExpr (ellipsis, no_pos)]), $1 }

short_slice:
| Colon
    { SimpleSlice(zero, IdExpr(sys_maxint, no_pos)), $1 }
| expression Colon
    { SimpleSlice($1, IdExpr(sys_maxint, no_pos)), union_pos (pos_of_expr $1) $2 }
| Colon expression
    { SimpleSlice(zero, $2), union_pos $1 (pos_of_expr $2) }
| expression Colon expression
    { SimpleSlice($1, $3), union_pos (pos_of_expr $1) (pos_of_expr $3) }

call:
| primary Leftparen Rightparen
    { CallExpr($1, [], union_pos (pos_of_expr $1) $3) }
| primary Leftparen arg_list Rightparen
    { CallExpr ($1, List.rev $3, union_pos (pos_of_expr $1) $4) }

arg_list:
| arg_comma_star argument optional_comma
    { $2 :: $1 }
| arg_comma_star Star atom
    { StarArg ($3, pos_of_expr $3) :: $1 }
| arg_comma_star Star atom Comma StarStar atom
    { StarStarArg ($6, pos_of_expr $6) :: StarArg ($3, pos_of_expr $3) :: $1 }
| arg_comma_star StarStar atom
    { StarStarArg ($3, pos_of_expr $3) :: $1 }

arg_comma_star:
| 
    { [] }
| arg_comma_star argument Comma
    { $2 :: $1 }

argument:
| expression
    { let p = pos_of_expr $1 in PosArg ($1, p) }
| Identifier Equal expression
    {  let pos = union_pos (snd $1) (pos_of_expr $3) in
	 KeywordArg (fst $1, $3, pos)
      }

optional_comma:
| 
    { () }
| Comma
    { () }

power:
| primary
    { $1 }
| primary StarStar u_expr
    { OpExpr ($1, StarStar, $3, union_pos (pos_of_expr $1) (pos_of_expr $3)) }

u_expr:
| power
    { $1                                                      }
| Dash u_expr
    { OpExpr (zero, Dash, $2, union_pos $1 (pos_of_expr $2))  }
| Plus u_expr
    { OpExpr (zero, Plus, $2, union_pos $1 (pos_of_expr $2))  }
| Tilde u_expr
    { OpExpr (zero, Tilde, $2, union_pos $1 (pos_of_expr $2)) }

m_expr:
| u_expr
    { $1 }
| m_expr Star u_expr
    { OpExpr ($1, Star, $3, union_pos (pos_of_expr $1) (pos_of_expr $3))    }
| m_expr Slash u_expr
    { OpExpr ($1, Slash, $3, union_pos (pos_of_expr $1) (pos_of_expr $3))   }
| m_expr Percent u_expr
    { OpExpr ($1, Percent, $3, union_pos (pos_of_expr $1) (pos_of_expr $3)) }

a_expr:
| m_expr
    { $1 }
| a_expr Plus m_expr
    { OpExpr ($1, Plus, $3, union_pos (pos_of_expr $1) (pos_of_expr $3)) }
| a_expr Dash m_expr
    { OpExpr ($1, Dash, $3, union_pos (pos_of_expr $1) (pos_of_expr $3)) }

shift_expr:
| a_expr
    { $1 }
| shift_expr Leftshift a_expr
    { OpExpr ($1, Leftshift, $3, union_pos (pos_of_expr $1) (pos_of_expr $3)) }
| shift_expr Rightshift a_expr
    { OpExpr ($1, Rightshift, $3, union_pos (pos_of_expr $1) (pos_of_expr $3)) }

and_expr:
| shift_expr
    { $1 }
| and_expr Ampersand shift_expr
    { OpExpr ($1, Ampersand, $3, union_pos (pos_of_expr $1) (pos_of_expr $3)) }

xor_expr:
| and_expr
    { $1 }
| xor_expr Caret and_expr
    { OpExpr ($1, Caret, $3, union_pos (pos_of_expr $1) (pos_of_expr $3)) }

or_expr:
| xor_expr
    { $1 }
| or_expr Bar xor_expr
    { OpExpr ($1, Bar, $3, union_pos (pos_of_expr $1) (pos_of_expr $3)) }

comparison:
| or_expr
    { $1 }
| comparison comp_operator or_expr
    { OpExpr($1, $2, $3, union_pos (pos_of_expr $1) (pos_of_expr $3)) }

comp_operator:
| Lessthan
    { Lessthan	}
| Greaterthan
    { Greaterthan	}
| EqualEqual
    { EqualEqual	}
| GreaterthanEq
    { GreaterthanEq	}
| LessthanEq
    { LessthanEq	}
| NotEqual
    { NotEqual	}
| Is
    { Is		}
| Is Not
    { IsNot		}
| In
    { In		}
| Not In
    { NotIn		}

expression:
| or_test
    { $1 }
| lambda_form
    { $1 }

or_test:
| and_test
    { $1 }
| or_test Or and_test
    { OpExpr($1, Or, $3, union_pos (pos_of_expr $1) (pos_of_expr $3)) }

and_test:
| not_test
    { $1 }
| and_test And not_test
    { OpExpr($1, And, $3, union_pos (pos_of_expr $1) (pos_of_expr $3)) }

not_test:
| comparison
    { $1 }
| Not not_test
    { OpExpr(zero, Not, $2, union_pos $1 (pos_of_expr $2)) }

lambda_form:
| Lambda Colon expression
    { FuncExpr([], [ReturnStmt($3, no_pos)], union_pos $1 (pos_of_expr $3)) }
| Lambda parameter_list Colon expression
    { FuncExpr(fst $2, [ReturnStmt($4, no_pos)], union_pos $1 (pos_of_expr $4)) }

expr_list_without_comma:
| expression
    { [$1], pos_of_expr $1 }
| expr_list_without_comma Comma expression
    {  let (el,elp) = $1 in
	 el @ [$3], union_pos elp (pos_of_expr $3)
      }

expr_list:
| expr_list_without_comma
    { $1				    }
| expr_list_without_comma Comma
    { (fst $1), union_pos (snd $1) $2 }

simple_stmt:
| expression_stmt
    { $1 }
| assert_stmt
    { $1 }
| assignment_stmt
    { $1 }
| augmented_assignment_stmt
    { $1 }
| pass_stmt
    { $1 }
| del_stmt
    { $1 }
| print_stmt
    { $1 }
| return_stmt
    { $1 }
| raise_stmt
    { $1 }
| break_stmt
    { $1 }
| continue_stmt
    { $1 }
| import_stmt
    { $1 }
| global_stmt
    { $1 }

expression_stmt:
| expr_list
    { let (el,elp) = $1 in ExprList (el, elp) }

assert_stmt:
| Assert expression
    { AssertStmt($2, None, union_pos $1 (pos_of_expr $2)) }
| Assert expression Comma expression
    { AssertStmt($2, Some $4, union_pos $1 (pos_of_expr $4)) }

assignment_stmt:
| expr_list Equal expr_list
    {  let (el1,p1) = $1 in
	 let (el2,p2) = $3 in
	 AssignStmt ([el1], el2, union_pos p1 p2)
      }
| expr_list Equal assignment_stmt
    {  let (el,elp) = $1 in
         match $3 with
	    AssignStmt (dst,src,pos) ->
	       AssignStmt(dst @ [el], src, union_pos elp pos)
	  | _ ->
	    fatal_error $2 "InternalParserError: unexpected assignment_stmt"
      }

target_list:
| target_list_without_comma
    { $1 }
| target_list_without_comma Comma
    { let (tl,tlp) = $1 in (tl, union_pos tlp $2) }

target_list_without_comma:
| primary
    { [$1], pos_of_expr $1 }
| target_list_without_comma Comma primary
    {  let (tl,tlp) = $1 in
	 (tl @ [$3], union_pos tlp (pos_of_expr $3))
      }

augmented_assignment_stmt:
| primary augop expr_list
    {  let (el,elp) = $3 in
	 AugAssignStmt ($1, $2, el, union_pos (pos_of_expr $1) elp)
      }

augop:
| PlusEqual
    { PlusEqual       }
| DashEqual
    { DashEqual       }
| StarEqual
    { StarEqual       }
| SlashEqual
    { SlashEqual      }
| PercentEqual
    { PercentEqual    }
| StarStarEqual
    { StarStarEqual   }
| RightshiftEqual
    { RightshiftEqual }
| LeftshiftEqual
    { LeftshiftEqual  }
| AmpersandEqual
    { AmpersandEqual  }
| CaretEqual
    { CaretEqual      }
| BarEqual
    { BarEqual        }

pass_stmt:
| Pass
    { PassStmt $1 }

del_stmt:
| Del target_list
    { let (tl,tlp) = $2 in DelStmt (tl, union_pos $1 tlp) }

print_stmt:
| Print
    { PrintStmt (none_expr, [], $1) }
| Print expr_list
    { let (el,elp) = $2 in PrintStmt (none_expr, el, union_pos $1 elp) }
| Print Rightshift expression
    { PrintStmt ($3, [], union_pos $1 (pos_of_expr $3)) }
| Print Rightshift expression Comma expr_list
    { let (el,elp) = $5 in PrintStmt ($3, el, union_pos $1 elp) }

return_stmt:
| Return
    { ReturnStmt (none_expr, $1) }
| Return expr_list
    {  let (el,elp) = $2 in
	 let pos = union_pos $1 elp in
	 if List.length el = 1
	    then ReturnStmt (List.hd el, pos)
	    else ReturnStmt (TupleExpr (el, elp), pos)
      }

raise_stmt:
| Raise
    { RaiseStmt (none_expr, none_expr, none_expr, $1) }
| Raise expression
    { RaiseStmt ($2, none_expr, none_expr, union_pos $1 (pos_of_expr $2)) }
| Raise expression Comma expression
    { RaiseStmt ($2, $4, none_expr, union_pos $1 (pos_of_expr $4)) }
| Raise expression Comma expression Comma expression
    { RaiseStmt ($2, $4, $6, union_pos $1 (pos_of_expr $6)) }

break_stmt:
| Break
    { BreakStmt $1 }

continue_stmt:
| Continue
    { ContStmt $1 }

import_stmt:
| Import module_list
    {  let (ml,mlp) = $2 in
	 ImportStmt ([], ml, union_pos $1 mlp)
      }
| From module_name Import Star
    {  let (mn,mnp) = $2 in
	 ImportStmt (mn, [], union_pos $1 $4)
      }
| From module_name Import import_ident_list
    {  let (mn,mnp) = $2 in
	 let (il,ilp) = $4 in
	 ImportStmt (mn, il, union_pos $1 ilp)
      }

module_list:
| module_name
    { let (mn,mnp) = $1 in [mn, none], mnp }
| module_name Identifier Identifier
    {  let (s,_) = $2 in
	 if Symbol.to_string s = "as"
	    then
	       let (mn,mnp) = $1 in
	       let (id,idp) = $3 in
	       [mn, id], union_pos mnp idp
	    else raise Parsing.Parse_error
      }
| module_list Comma module_name
    {  let (ml,mlp) = $1 in
	 let (mn,mnp) = $3 in
	 ml @ [mn, none], union_pos mlp mnp
      }
| module_list Comma module_name Identifier Identifier
    {  let (s,_) = $4 in
	 if Symbol.to_string s = "as"
	    then
	       let (ml,mlp) = $1 in
	       let (mn,mnp) = $3 in
	       let (id,idp) = $5 in
	       ml @ [mn, id], union_pos mlp idp

	    else raise Parsing.Parse_error
      }

module_name:
| Identifier
    { let (s,p) = $1 in [s], p }
| module_name Period Identifier
    {  let (mn,mnp) = $1 in
	 let (s,p) = $3 in
	 mn @ [s], union_pos mnp p
      }

import_ident_list:
| Identifier
    { let (id,idp) = $1 in [[id], none], idp }
| Identifier Identifier Identifier
    {  let (s,_) = $2 in
	 if Symbol.to_string s = "as"
	    then
	       let (id1,idp1) = $1 in
	       let (id2,idp2) = $3 in
	       [[id1], id2], union_pos idp1 idp2
	    else raise Parsing.Parse_error
      }
| import_ident_list Comma Identifier
    {  let (il,ilp) = $1 in
	 let (id,idp) = $3 in
	 il @ [[id], none], union_pos ilp idp
      }
| import_ident_list Comma Identifier Identifier Identifier
    {  let (s,_) = $4 in
	 if Symbol.to_string s = "as"
	    then
	       let (il,ilp) = $1 in
	       let (id1,idp1) = $3 in
	       let (id2,idp2) = $5 in
	       il @ [[id1], id2], union_pos ilp idp2
	    else raise Parsing.Parse_error
      }

global_stmt:
| Global identifier_list
    { let (il,ilp) = $2 in GlobalStmt (il, union_pos $1 ilp) }

identifier_list:
| Identifier
    { let (id,idp) = $1 in [id], idp }
| identifier_list Comma Identifier
    {  let (il,ilp) = $1 in
	 let (id,idp) = $3 in
	 il @ [id], union_pos ilp idp
      }

compound_stmt:
| if_stmt
    { $1 }
| while_stmt
    { $1 }
| for_stmt
    { $1 }
| try_stmt
    { $1 }
| funcdef
    { $1 }
| classdef
    { $1 }

suite:
| stmt_list Newline
    { $1 }
| Newline Indent statement_plus Dedent
    { $3 }

statement_plus:
| statement
    { [$1], pos_of_stmt $1 }
| statement_plus statement
    {  let (stp,stpp) = $1 in
	 stp @ [$2], union_pos stpp (pos_of_stmt $2)
      }

statement:
| stmt_list Newline
    { let (sl,slp) = $1 in StmtList (sl, slp) }
| compound_stmt
    { $1                                      }

stmt_list:
| stmt_list_without_semicolon
    { $1 }
| stmt_list_without_semicolon Semicolon
    { let (sl,slp) = $1 in sl, union_pos slp $2 }

stmt_list_without_semicolon:
| simple_stmt
    { [$1], pos_of_stmt $1 }
| stmt_list_without_semicolon Semicolon simple_stmt
    { let (sl,slp) = $1 in $3 :: sl, union_pos slp (pos_of_stmt $3) }

if_stmt:
| If expression Colon suite
    { let (s,sp) = $4 in IfStmt ($2, s, [], union_pos $1 sp) }
| If expression Colon suite elif_list
    {  let (s,sp) = $4 in
	 let (el,elp) = $5 in
	 IfStmt ($2, s, el, union_pos $1 elp)
      }

elif_list:
| Else Colon suite
    { let (s,sp) = $3 in s, union_pos $1 sp }
| Elif expression Colon suite
    {  let (s,sp) = $4 in
	 let pos = union_pos $1 sp in
	 [IfStmt ($2, s, [], pos)], pos
      }
| Elif expression Colon suite elif_list
    {  let (s,sp) = $4 in
	 let (el,elp) = $5 in
	 let pos = union_pos $1 elp in
	 [IfStmt ($2, s, el, pos)], pos
      }

while_stmt:
| While expression Colon suite
    { let (s,sp) = $4 in WhileStmt ($2, s, [], sp) }
| While expression Colon suite Else Colon suite
    {  let (s1,sp1) = $4 in
	 let (s2,sp2) = $7 in
	 WhileStmt ($2, s1, s2, union_pos $1 sp2)
      }

for_stmt:
| For target_list In expr_list Colon suite
    {  let (tl,tlp) = $2 in
	 let (el,elp) = $4 in
	 let (s,sp) = $6 in
	 ForStmt (tl, el, s, [], union_pos $1 sp)
      }
| For target_list In expr_list Colon suite Else Colon suite
    {  let (tl,tlp) = $2 in
	 let (el,elp) = $4 in
	 let (s1,sp1) = $6 in
	 let (s2,sp2) = $9 in
	 ForStmt (tl, el, s1, s2, union_pos $1 sp2)
      }

try_stmt:
| try_exc_stmt
    { $1 }
| try_fin_stmt
    { $1 }

try_exc_stmt:
| Try Colon suite exc_list
    {  let (s,sp) = $3 in
	 let (el,elp) = $4 in
	 TryStmt (s, el, [], union_pos $1 elp)
      }
| Try Colon suite exc_list Else Colon suite
    {  let (s1,sp1) = $3 in
	 let (el,elp) = $4 in
	 let (s2,sp2) = $7 in
	 TryStmt (s1, el, s2, union_pos $1 sp2)
      }

exc_list:
| exc_item
    { let a, b, c, d = $1 in [a, b, c], d }
| exc_list exc_item
    {  let (el,elp) = $1 in
	 let (a,b,c,d) = $2 in
	 el @ [a, b, c], union_pos elp d
      }

exc_item:
| Except Colon suite
    { let (s,sp) = $3 in none_expr, none_expr, s, union_pos $1 sp }
| Except expression Colon suite
    { let (s,sp) = $4 in $2, none_expr, s, union_pos $1 sp }
| Except expression Comma primary Colon suite
    { let (s,sp) = $6 in $2, $4, s, union_pos $1 sp }

try_fin_stmt:
| Try Colon suite Finally Colon suite
    {  let (s1,sp1) = $3 in
	 let (s2,sp2) = $6 in
	 TryStmt (s1, [], s2, union_pos $1 sp2)
      }

funcdef:
| Def Identifier Leftparen Rightparen Colon suite
    {  let (id,idp) = $2 in
	 let (s,sp) = $6 in
	 FuncDef (id, [], s, union_pos $1 sp)
      }
| Def Identifier Leftparen parameter_list Rightparen Colon suite
    {  let (id,idp) = $2 in
	 let (pl,plp) = $4 in
	 let (s,sp) = $7 in
	 FuncDef (id, pl, s, union_pos $1 sp)
      }

parameter_list:
| defparams optional_comma
    { $1 }
| defparams Comma Star Identifier
    {  let (dp,dpp) = $1 in
	 let (id,idp) = $4 in
	 dp @ [ExtraPosParam (id, idp)], union_pos dpp idp
      }
| defparams Comma StarStar Identifier
    {  let (dp,dpp) = $1 in
	 let (id,idp) = $4 in
	 dp @ [ExtraKwParam (id, idp)], union_pos dpp idp
      }
| Star Identifier
    { let (id,idp) = $2 in [ExtraPosParam (id, idp)], union_pos $1 idp }
| StarStar Identifier
    { let (id,idp) = $2 in [ExtraKwParam (id, idp)], union_pos $1 idp }
| Star Identifier Comma StarStar Identifier
    {  let (id1,idp1) = $2 in
	 let (id2,idp2) = $5 in
	 [ExtraPosParam (id1, idp1); ExtraPosParam (id2, idp2)],
	    union_pos $1 idp2
      }
| defparams Comma Star Identifier Comma StarStar Identifier
    {  let (dp,dpp) = $1 in
	 let (id1,idp1) = $4 in
	 let (id2,idp2) = $7 in
	 dp @ [ExtraPosParam (id1, idp1); ExtraKwParam (id2, idp2)],
	    union_pos dpp idp2
      }

defparams:
| defparam
    { [$1], pos_of_param $1 }
| defparams Comma defparam
    { let (pl,plp) = $1 in pl @ [$3], union_pos plp (pos_of_param $3) }

defparam:
| parameter
    { $1 }
| parameter Equal expression
    {  DefParam ($1, $3, union_pos (pos_of_param $1) (pos_of_expr $3)) }

parameter:
| Identifier
    { let (id,idp) = $1 in IdParam (id, idp) }
| Leftparen sublist Rightparen
    { let (sl,slp) = $2 in SublistParam (sl, union_pos $1 $3) }

sublist:
| sublist_without_comma
    { $1 }
| sublist_without_comma Comma
    { $1 }

sublist_without_comma:
| parameter
    { [$1], pos_of_param $1 }
| sublist_without_comma Comma parameter
    { let (sl,slp) = $1 in sl @ [$3], union_pos slp (pos_of_param $3) }

classdef:
| Class Identifier Colon suite
    {  let (id,idp) = $2 in
	 let (s,sp) = $4 in
	 ClassDef (id, [], s, union_pos $1 sp)
      }
| Class Identifier inheritance Colon suite
    {  let (id,idp) = $2 in
	 let (s,sp) = $5 in
	 ClassDef (id, $3, s, union_pos $1 sp)
      }

inheritance:
| Leftparen Rightparen
    { []     }
| Leftparen expr_list Rightparen
    { fst $2 }

file_input:
| file_input_list
    { List.rev $1 }

file_input_list:
| Newline
    { []        }
| statement
    { [$1]      }
| file_input_list Newline
    { $1        }
| file_input_list statement
    { $2 :: $1  }

%%