* doc/misc/eshell.texi: Fill most of the missing sections.
[emacs.git] / etc / grammars / python.wy
blob7c799098df958f36b42f9e868ff2039ebb9da7e7
1 ;;; python.wy -- LALR grammar for Python
3 ;; Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
4 ;;   2011, 2012 Free Software Foundation, Inc.
5 ;; Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
6 ;; 2009, 2010 Python Software Foundation; All Rights Reserved
8 ;; Author: Richard Kim <ryk@dspwiz.com>
9 ;; Maintainer: Richard Kim <ryk@dspwiz.com>
10 ;; Created: June 2002
11 ;; Keywords: syntax
13 ;; This file is part of GNU Emacs.
15 ;; GNU Emacs is free software: you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation, either version 3 of the License, or
18 ;; (at your option) any later version.
20 ;; GNU Emacs is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23 ;; GNU General Public License for more details.
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
28 ;;; Commentary:
30 ;; This is an LALR python parser that follows the official python
31 ;; grammar closely with very few exceptions.  The Python grammar is
32 ;; used and reproduced under the following license:
34 ;; PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2
35 ;; --------------------------------------------
36 ;; 1. This LICENSE AGREEMENT is between the Python Software Foundation
37 ;; ("PSF"), and the Individual or Organization ("Licensee") accessing
38 ;; and otherwise using this software ("Python") in source or binary
39 ;; form and its associated documentation.
41 ;; 2. Subject to the terms and conditions of this License Agreement,
42 ;; PSF hereby grants Licensee a nonexclusive, royalty-free, world-wide
43 ;; license to reproduce, analyze, test, perform and/or display
44 ;; publicly, prepare derivative works, distribute, and otherwise use
45 ;; Python alone or in any derivative version, provided, however, that
46 ;; PSF's License Agreement and PSF's notice of copyright, i.e.,
47 ;; "Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
48 ;; 2009, 2010 Python Software Foundation; All Rights Reserved" are
49 ;; retained in Python alone or in any derivative version prepared by
50 ;; Licensee.
52 ;; 3. In the event Licensee prepares a derivative work that is based
53 ;; on or incorporates Python or any part thereof, and wants to make
54 ;; the derivative work available to others as provided herein, then
55 ;; Licensee hereby agrees to include in any such work a brief summary
56 ;; of the changes made to Python.
58 ;; 4. PSF is making Python available to Licensee on an "AS IS"
59 ;; basis.  PSF MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR
60 ;; IMPLIED.  BY WAY OF EXAMPLE, BUT NOT LIMITATION, PSF MAKES NO AND
61 ;; DISCLAIMS ANY REPRESENTATION OR WARRANTY OF MERCHANTABILITY OR FITNESS
62 ;; FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF PYTHON WILL NOT
63 ;; INFRINGE ANY THIRD PARTY RIGHTS.
65 ;; 5. PSF SHALL NOT BE LIABLE TO LICENSEE OR ANY OTHER USERS OF PYTHON
66 ;; FOR ANY INCIDENTAL, SPECIAL, OR CONSEQUENTIAL DAMAGES OR LOSS AS A
67 ;; RESULT OF MODIFYING, DISTRIBUTING, OR OTHERWISE USING PYTHON, OR
68 ;; ANY DERIVATIVE THEREOF, EVEN IF ADVISED OF THE POSSIBILITY THEREOF.
70 ;; 6. This License Agreement will automatically terminate upon a
71 ;; material breach of its terms and conditions.
73 ;; 7. Nothing in this License Agreement shall be deemed to create any
74 ;; relationship of agency, partnership, or joint venture between PSF
75 ;; and Licensee.  This License Agreement does not grant permission to
76 ;; use PSF trademarks or trade name in a trademark sense to endorse or
77 ;; promote products or services of Licensee, or any third party.
79 ;; 8. By copying, installing or otherwise using Python, Licensee
80 ;; agrees to be bound by the terms and conditions of this License
81 ;; Agreement.
83 ;;; To do:
85 ;; * Verify that semantic-lex-python-number regexp is correct.
87 ;; --------
88 ;; Settings
89 ;; --------
91 %package wisent-python-wy
93 %languagemode python-mode
95 ;; The default start symbol
96 %start goal
97 ;; Alternate entry points
98 ;;    - Needed by partial re-parse
99 %start function_parameter
100 %start paren_class
101 %start indented_block
102 ;;    - Needed by EXPANDFULL clauses
103 %start function_parameters
104 %start paren_classes
105 %start indented_block_body
107 ;; -------------------------------
108 ;; Misc. Python specific terminals
109 ;; -------------------------------
110 ;; The value of these tokens are for documentation only, they are not
111 ;; used by the lexer.
112 %token <charquote>   BACKSLASH    "\\"
113 %token <newline>     NEWLINE      "\n"
114 %token <indentation> INDENT       "^\\s-+"
115 %token <indentation> DEDENT       "[^:INDENT:]" 
116 %token <indentation> INDENT_BLOCK "(INDENT DEDENT)"
118 ;; -----------------------------
119 ;; Block & Parenthesis terminals
120 ;; -----------------------------
121 %type  <block>       ;;syntax "\\s(\\|\\s)" matchdatatype block
123 %token <block>       PAREN_BLOCK "(LPAREN RPAREN)"
124 %token <block>       BRACE_BLOCK "(LBRACE RBRACE)"
125 %token <block>       BRACK_BLOCK "(LBRACK RBRACK)"
127 %token <open-paren>  LPAREN      "("
128 %token <close-paren> RPAREN      ")"
129 %token <open-paren>  LBRACE      "{"
130 %token <close-paren> RBRACE      "}"
131 %token <open-paren>  LBRACK      "["
132 %token <close-paren> RBRACK      "]"
134 ;; ------------------
135 ;; Operator terminals
136 ;; ------------------
137 %type  <punctuation> ;;syntax "\\(\\s.\\|\\s$\\|\\s'\\)+" matchdatatype string
139 %token <punctuation> LTLTEQ     "<<="
140 %token <punctuation> GTGTEQ     ">>="
141 %token <punctuation> EXPEQ      "**="
142 %token <punctuation> DIVDIVEQ   "//="
143 %token <punctuation> DIVDIV     "//"
144 %token <punctuation> LTLT       "<<"
145 %token <punctuation> GTGT       ">>"
146 %token <punctuation> EXPONENT   "**"
147 %token <punctuation> EQ         "=="
148 %token <punctuation> GE         ">="
149 %token <punctuation> LE         "<="
150 %token <punctuation> PLUSEQ     "+="
151 %token <punctuation> MINUSEQ    "-="
152 %token <punctuation> MULTEQ     "*="
153 %token <punctuation> DIVEQ      "/="
154 %token <punctuation> MODEQ      "%="
155 %token <punctuation> AMPEQ      "&="
156 %token <punctuation> OREQ       "|="
157 %token <punctuation> HATEQ      "^="
158 %token <punctuation> LTGT       "<>"
159 %token <punctuation> NE         "!="
160 %token <punctuation> HAT        "^"
161 %token <punctuation> LT         "<"
162 %token <punctuation> GT         ">"
163 %token <punctuation> AMP        "&"
164 %token <punctuation> MULT       "*"
165 %token <punctuation> DIV        "/"
166 %token <punctuation> MOD        "%"
167 %token <punctuation> PLUS       "+"
168 %token <punctuation> MINUS      "-"
169 %token <punctuation> PERIOD     "."
170 %token <punctuation> TILDE      "~"
171 %token <punctuation> BAR        "|"
172 %token <punctuation> COLON      ":"
173 %token <punctuation> SEMICOLON  ";"
174 %token <punctuation> COMMA      ","
175 %token <punctuation> ASSIGN     "="
176 %token <punctuation> BACKQUOTE  "`"
179 ;; -----------------
180 ;; Literal terminals
181 ;; -----------------
182 %token <string>      STRING_LITERAL
184 %type  <number>      ;;syntax semantic-lex-number-expression
185 %token <number>      NUMBER_LITERAL
187 %type  <symbol>      ;;syntax "\\(\\sw\\|\\s_\\)+"
188 %token <symbol>      NAME
190 ;; -----------------
191 ;; Keyword terminals
192 ;; -----------------
193 %type  <keyword> ;;syntax "\\(\\sw\\|\\s_\\)+" matchdatatype keyword
195 %keyword AND         "and"
196 %put     AND summary
197 "Logical AND binary operator ... "
199 %keyword AS          "as"
200 %put     AS summary
201 "EXPR as NAME makes value of EXPR available as variable NAME"
203 %keyword ASSERT      "assert"
204 %put     ASSERT summary
205 "Raise AssertionError exception if <expr> is false"
207 %keyword BREAK       "break"
208 %put     BREAK summary
209 "Terminate 'for' or 'while' loop"
211 %keyword CLASS       "class"
212 %put     CLASS summary
213 "Define a new class"
215 %keyword CONTINUE            "continue"
216 %put     CONTINUE summary
217 "Skip to the next iteration of enclosing 'for' or 'while' loop"
219 %keyword DEF         "def"
220 %put     DEF summary
221 "Define a new function"
223 %keyword DEL         "del"
224 %put     DEL summary
225 "Delete specified objects, i.e., undo what assignment did"
227 %keyword ELIF        "elif"
228 %put     ELIF summary
229 "Shorthand for 'else if' following an 'if' statement"
231 %keyword ELSE        "else"
232 %put     ELSE summary
233 "Start the 'else' clause following an 'if' statement"
235 %keyword EXCEPT      "except"
236 %put     EXCEPT summary
237 "Specify exception handlers along with 'try' keyword"
239 %keyword EXEC        "exec"
240 %put     EXEC summary
241 "Dynamically execute Python code"
243 %keyword FINALLY             "finally"
244 %put     FINALLY summary
245 "Specify code to be executed after 'try' statements whether or not an exception occurred"
247 %keyword FOR         "for"
248 %put     FOR summary
249 "Start a 'for' loop"
251 %keyword FROM        "from"
252 %put     FROM summary
253 "Modify behavior of 'import' statement"
255 %keyword GLOBAL      "global"
256 %put     GLOBAL summary
257 "Declare one or more symbols as global symbols"
259 %keyword IF          "if"
260 %put     IF summary
261 "Start 'if' conditional statement"
263 %keyword IMPORT      "import"
264 %put     IMPORT summary
265 "Load specified modules"
267 %keyword IN          "in"
268 %put     IN summary
269 "Part of 'for' statement "
271 %keyword IS          "is"
272 %put     IS summary
273 "Binary operator that tests for object equality"
275 %keyword LAMBDA      "lambda"
276 %put     LAMBDA summary
277 "Create anonymous function"
279 %keyword NOT         "not"
280 %put     NOT summary
281 "Unary boolean negation operator"
283 %keyword OR          "or"
284 %put     OR summary
285 "Binary logical 'or' operator"
287 %keyword PASS        "pass"
288 %put     PASS summary
289 "Statement that does nothing"
291 %keyword PRINT       "print"
292 %put     PRINT summary
293 "Print each argument to standard output"
295 %keyword RAISE       "raise"
296 %put     RAISE summary
297 "Raise an exception"
299 %keyword RETURN      "return"
300 %put     RETURN summary
301 "Return from a function"
303 %keyword TRY         "try"
304 %put     TRY summary
305 "Start of statements protected by exception handlers"
307 %keyword WHILE       "while"
308 %put     WHILE summary
309 "Start a 'while' loop"
311 %keyword YIELD       "yield"
312 %put     YIELD summary
313 "Create a generator function"
317 ;;;****************************************************************************
318 ;;;@ goal
319 ;;;****************************************************************************
321 ;; simple_stmt are statements that do not involve INDENT tokens
322 ;; compound_stmt are statements that involve INDENT tokens
323 goal
324   : NEWLINE
325   | simple_stmt
326   | compound_stmt
327   ;
329 ;;;****************************************************************************
330 ;;;@ simple_stmt
331 ;;;****************************************************************************
333 ;; simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
334 simple_stmt
335   : small_stmt_list semicolon_opt NEWLINE
336   ;
338 ;; small_stmt (';' small_stmt)*
339 small_stmt_list
340   : small_stmt
341   | small_stmt_list SEMICOLON small_stmt
342   ;
344 small_stmt
345   : expr_stmt
346   | print_stmt
347   | del_stmt
348   | pass_stmt
349   | flow_stmt
350   | import_stmt
351   | global_stmt
352   | exec_stmt
353   | assert_stmt
354   ;
356 ;;;============================================================================
357 ;;;@@ print_stmt
358 ;;;============================================================================
360 ;; print_stmt: 'print' [ test (',' test)* [','] ]
361 ;;           | '>>' test [ (',' test)+ [','] ]
362 print_stmt
363   : PRINT print_stmt_trailer
364     (CODE-TAG $1 nil)
365   ;
367 ;; [ test (',' test)* [','] ] | '>>' test [ (',' test)+ [','] ]
368 print_stmt_trailer
369   : test_list_opt
370     ()
371   | GTGT test trailing_test_list_with_opt_comma_opt
372     ()
373   ;
375 ;; [ (',' test)+ [','] ]
376 trailing_test_list_with_opt_comma_opt
377   : ;;EMPTY
378   | trailing_test_list comma_opt
379     ()
380   ;
382 ;; (',' test)+
383 trailing_test_list
384   : COMMA test
385     ()
386   | trailing_test_list COMMA test
387     ()
388   ;
390 ;;;============================================================================
391 ;;;@@ expr_stmt
392 ;;;============================================================================
394 ;; expr_stmt: testlist (augassign testlist | ('=' testlist)*)
395 expr_stmt
396   : testlist expr_stmt_trailer
397     (if (and $2 (stringp $1) (string-match "^\\(\\sw\\|\\s_\\)+$" $1))
398         ;; If this is an assignment statement and left side is a symbol,
399         ;; then generate a 'variable token, else return 'code token.
400         (VARIABLE-TAG $1 nil nil)
401       (CODE-TAG $1 nil))
402   ;
404 ;; Could be EMPTY because of eq_testlist_zom.
405 ;; (augassign testlist | ('=' testlist)*)
406 expr_stmt_trailer
407   : augassign testlist
408   | eq_testlist_zom
409   ;
411 ;; Could be EMPTY!
412 ;; ('=' testlist)*
413 eq_testlist_zom
414   : ;;EMPTY
415   | eq_testlist_zom ASSIGN testlist
416     (identity $3)
417   ;
419 ;; augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
420 ;;          | '<<=' | '>>=' | '**=' | '//='
421 augassign
422   : PLUSEQ | MINUSEQ | MULTEQ | DIVEQ | MODEQ
423   | AMPEQ  | OREQ    | HATEQ  | LTLTEQ
424   | GTGTEQ | EXPEQ   | DIVDIVEQ
425   ;
427 ;;;============================================================================
428 ;;;@@ del_stmt
429 ;;;============================================================================
431 ;; del_stmt: 'del' exprlist
432 del_stmt
433   : DEL exprlist
434     (CODE-TAG $1 nil)
435   ;
437 ;; exprlist: expr (',' expr)* [',']
438 exprlist
439   : expr_list comma_opt
440     ()
441   ;
443 ;; expr (',' expr)*
444 expr_list
445   : expr
446     ()
447   | expr_list COMMA expr
448     ()
449   ;
451 ;;;============================================================================
452 ;;;@@ pass_stmt
453 ;;;============================================================================
455 ;; pass_stmt: 'pass'
456 pass_stmt
457   : PASS
458     (CODE-TAG $1 nil)
459   ;
461 ;;;============================================================================
462 ;;;@@ flow_stmt
463 ;;;============================================================================
465 flow_stmt
466   : break_stmt
467   | continue_stmt
468   | return_stmt
469   | raise_stmt
470   | yield_stmt
471   ;
473 ;; break_stmt: 'break'
474 break_stmt
475   : BREAK
476     (CODE-TAG $1 nil)
477   ;
479 ;; continue_stmt: 'continue'
480 continue_stmt
481   : CONTINUE
482     (CODE-TAG $1 nil)
483   ;
485 ;; return_stmt: 'return' [testlist]
486 return_stmt
487   : RETURN testlist_opt
488     (CODE-TAG $1 nil)
489   ;
491 ;; [testlist]
492 testlist_opt
493   : ;;EMPTY
494   | testlist
495     ()
496   ;
498 ;; yield_stmt: 'yield' testlist
499 yield_stmt
500   : YIELD
501     (CODE-TAG $1 nil)
502   | YIELD testlist
503     (CODE-TAG $1 nil)
504   ;
506 ;; raise_stmt: 'raise' [test [',' test [',' test]]]
507 raise_stmt
508   : RAISE zero_one_two_or_three_tests
509     (CODE-TAG $1 nil)
510   ;
512 ;; [test [',' test [',' test]]]
513 zero_one_two_or_three_tests
514   : ;;EMPTY
515   | test zero_one_or_two_tests
516     ()
517   ;
519 ;; [',' test [',' test]]
520 zero_one_or_two_tests
521   : ;;EMPTY
522   | COMMA test zero_or_one_comma_test
523     ()
524   ;
526 ;; [',' test]
527 zero_or_one_comma_test
528   : ;;EMPTY
529   | COMMA test
530     ()
531   ;
533 ;;;============================================================================
534 ;;;@@ import_stmt
535 ;;;============================================================================
537 ;; import_stmt : 'import' dotted_as_name (',' dotted_as_name)*
538 ;;             | 'from' dotted_name 'import'
539 ;;               ('*' | import_as_name (',' import_as_name)*)
540 import_stmt
541   : IMPORT dotted_as_name_list
542     (INCLUDE-TAG $2 nil)
543   | FROM dotted_name IMPORT star_or_import_as_name_list
544     (INCLUDE-TAG $2 nil)
545   ;
547 ;; dotted_as_name (',' dotted_as_name)*
548 dotted_as_name_list
549   : dotted_as_name
550   | dotted_as_name_list COMMA dotted_as_name
551   ;
553 ;; ('*' | import_as_name (',' import_as_name)*)
554 star_or_import_as_name_list
555   : MULT
556     ()
557   | import_as_name_list
558     ()
559   ;
561 ;; import_as_name (',' import_as_name)*
562 import_as_name_list
563   : import_as_name
564     ()
565   | import_as_name_list COMMA import_as_name
566     ()
567   ;
569 ;; import_as_name: NAME [NAME NAME]
570 import_as_name
571   : NAME as_name_opt
572     ()
573   ;
575 ;; dotted_as_name: dotted_name [AS NAME]
576 dotted_as_name
577   : dotted_name as_name_opt
578   ;
580 ;; [AS NAME]
581 as_name_opt
582   : ;;EMPTY
583   | AS NAME
584     (identity $2)
585   ;
587 ;; dotted_name: NAME ('.' NAME)*
588 dotted_name
589   : NAME
590   | dotted_name PERIOD NAME
591     (format "%s.%s" $1 $3)
592   ;
594 ;;;============================================================================
595 ;;;@@ global_stmt
596 ;;;============================================================================
598 ;; global_stmt: 'global' NAME (',' NAME)*
599 global_stmt
600   : GLOBAL comma_sep_name_list
601     (CODE-TAG $1 nil)
602   ;
604 ;; NAME (',' NAME)*
605 comma_sep_name_list
606   : NAME
607   | comma_sep_name_list COMMA NAME
608   ;
610 ;;;============================================================================
611 ;;;@@ exec_stmt
612 ;;;============================================================================
614 ;; exec_stmt: 'exec' expr ['in' test [',' test]]
615 exec_stmt
616   : EXEC expr exec_trailer
617     (CODE-TAG $1 nil)
618   ;
620 ;; ['in' test [',' test]]
621 exec_trailer
622   : ;;EMPTY
623   | IN test comma_test_opt
624     ()
625   ;
627 ;; [',' test]
628 comma_test_opt
629   : ;;EMPTY
630   | COMMA test
631     ()
632   ;
634 ;;;============================================================================
635 ;;;@@ assert_stmt
636 ;;;============================================================================
638 ;; assert_stmt: 'assert' test [',' test]
639 assert_stmt
640   : ASSERT test comma_test_opt
641     (CODE-TAG $1 nil)
642   ;
644 ;;;****************************************************************************
645 ;;;@ compound_stmt
646 ;;;****************************************************************************
648 compound_stmt
649   : if_stmt
650   | while_stmt
651   | for_stmt
652   | try_stmt
653   | funcdef
654   | class_declaration
655   ;
657 ;;;============================================================================
658 ;;;@@ if_stmt
659 ;;;============================================================================
661 ;; if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
662 if_stmt
663   : IF test COLON suite elif_suite_pair_list else_suite_pair_opt
664     (CODE-TAG $1 nil)
665   ;
667 ;; ('elif' test ':' suite)*
668 elif_suite_pair_list
669   : ;;EMPTY
670   | elif_suite_pair_list ELIF test COLON suite
671     ()
672   ;
674 ;; ['else' ':' suite]
675 else_suite_pair_opt
676   : ;;EMPTY
677   | ELSE COLON suite
678     ()
679   ;
681 ;; This NT follows the COLON token for most compound statements.
682 ;; suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT
683 suite
684   : simple_stmt
685     (list $1)
686   | NEWLINE indented_block
687     (progn $2)
688   ;
690 indented_block
691   : INDENT_BLOCK
692     (EXPANDFULL $1 indented_block_body)
693   ;
695 indented_block_body
696   : INDENT
697     ()
698   | DEDENT
699     ()
700   | simple_stmt
701   | compound_stmt
702   ;
704 ;;;============================================================================
705 ;;;@@ while_stmt
706 ;;;============================================================================
708 ;; while_stmt: 'while' test ':' suite ['else' ':' suite]
709 while_stmt
710   : WHILE test COLON suite else_suite_pair_opt
711     (CODE-TAG $1 nil)
712   ;
714 ;;;============================================================================
715 ;;;@@ for_stmt
716 ;;;============================================================================
718 ;; for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite]
719 for_stmt
720   : FOR exprlist IN testlist COLON suite else_suite_pair_opt
721     (CODE-TAG $1 nil)
722   ;
724 ;;;============================================================================
725 ;;;@@ try_stmt
726 ;;;============================================================================
728 ;; try_stmt: ('try' ':' suite (except_clause ':' suite)+ #diagram:break
729 ;;            ['else' ':' suite] | 'try' ':' suite 'finally' ':' suite)
730 try_stmt
731   : TRY COLON suite except_clause_suite_pair_list else_suite_pair_opt
732     (CODE-TAG $1 nil)
733   | TRY COLON suite FINALLY COLON suite
734     (CODE-TAG $1 nil)
735   ;
737 ;; (except_clause ':' suite)+
738 except_clause_suite_pair_list
739   : except_clause COLON suite
740     ()
741   | except_clause_suite_pair_list except_clause COLON suite
742     ()
743   ;
745 ;; # NB compile.c makes sure that the default except clause is last
746 ;; except_clause: 'except' [test [',' test]]
747 except_clause
748   : EXCEPT zero_one_or_two_test
749     ()
750   ;
752 ;; [test [',' test]]
753 zero_one_or_two_test
754   : ;;EMPTY
755   | test zero_or_one_comma_test
756     ()
757   ;
759 ;;;============================================================================
760 ;;;@@ funcdef
761 ;;;============================================================================
763 ;; funcdef: 'def' NAME parameters ':' suite
764 funcdef
765   : DEF NAME function_parameter_list COLON suite
766     (FUNCTION-TAG $2 nil $3)
767   ;
769 function_parameter_list
770   : PAREN_BLOCK
771     (let ((wisent-python-EXPANDING-block t))
772       (EXPANDFULL $1 function_parameters))
773   ;
775 ;; parameters: '(' [varargslist] ')'
776 function_parameters
777   : LPAREN
778     ()
779   | RPAREN
780     ()
781   | function_parameter COMMA
782   | function_parameter RPAREN
783   ;
785 function_parameter
786   : fpdef_opt_test
787  ;;  : NAME
788  ;;    (VARIABLE-TAG $1 nil nil)
789   | MULT NAME
790     (VARIABLE-TAG $2 nil nil)
791   | EXPONENT NAME
792     (VARIABLE-TAG $2 nil nil)
793   ;
795 ;;;============================================================================
796 ;;;@@ class_declaration
797 ;;;============================================================================
799 ;; classdef: 'class' NAME ['(' testlist ')'] ':' suite
800 class_declaration
801   : CLASS NAME paren_class_list_opt COLON suite
802     (TYPE-TAG $2 $1             ;; Name "class"
803               $5                ;; Members
804               (cons $3 nil)     ;; (SUPERCLASSES . INTERFACES)
805               )
806   ;
808 ;; ['(' testlist ')']
809 paren_class_list_opt
810   : ;;EMPTY
811   | paren_class_list
812   ;
814 paren_class_list
815   : PAREN_BLOCK
816     (let ((wisent-python-EXPANDING-block t))
817       (mapcar 'semantic-tag-name (EXPANDFULL $1 paren_classes)))
818   ;
820 ;; parameters: '(' [varargslist] ')'
821 paren_classes
822   : LPAREN
823     ()
824   | RPAREN
825     ()
826   | paren_class COMMA
827     (VARIABLE-TAG $1 nil nil)
828   | paren_class RPAREN
829     (VARIABLE-TAG $1 nil nil)
830   ;
832 ;; In general, the base class can be specified by a general expression
833 ;; which evalue to a class object, i.e., base classes are not just names!
834 ;; However base classes are names in most cases.  Thus the
835 ;; non-terminals below work only with simple names.  Even if the
836 ;; parser can parse general expressions, I don't see much benefit in
837 ;; generating a string of expression as base class "name".
838 paren_class
839   : dotted_name
840   ;
842 ;;;****************************************************************************
843 ;;;@ test
844 ;;;****************************************************************************
846 ;; test: and_test ('or' and_test)* | lambdef
847 test
848   : test_test
849   | lambdef
850   ;
852 ;; and_test ('or' and_test)*
853 test_test
854   : and_test
855   | test_test OR and_test
856     ()
857   ;
859 ;; and_test: not_test ('and' not_test)*
860 and_test
861   : not_test
862   | and_test AND not_test
863     ()
864   ;
866 ;; not_test: 'not' not_test | comparison
867 not_test
868   : NOT not_test
869     ()
870   | comparison
871   ;
873 ;; comparison: expr (comp_op expr)*
874 comparison
875   : expr
876   | comparison comp_op expr
877     ()
878   ;
880 ;; comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
881 comp_op
882   : LT | GT | EQ | GE | LE | LTGT | NE | IN | NOT IN | IS | IS NOT
883   ;
885 ;; expr: xor_expr ('|' xor_expr)*
886 expr
887   : xor_expr
888   | expr BAR xor_expr
889     ()
890   ;
892 ;; xor_expr: and_expr ('^' and_expr)*
893 xor_expr
894   : and_expr
895   | xor_expr HAT and_expr
896     ()
897   ;
899 ;; and_expr: shift_expr ('&' shift_expr)*
900 and_expr
901   : shift_expr
902   | and_expr AMP shift_expr
903     ()
904   ;
906 ;; shift_expr: arith_expr (('<<'|'>>') arith_expr)*
907 shift_expr
908   : arith_expr
909   | shift_expr shift_expr_operators arith_expr
910     ()
911   ;
913 ;; ('<<'|'>>')
914 shift_expr_operators
915   : LTLT
916   | GTGT
917   ;
919 ;; arith_expr: term (('+'|'-') term)*
920 arith_expr
921   : term
922   | arith_expr plus_or_minus term
923     ()
924   ;
926 ;; ('+'|'-')
927 plus_or_minus
928   : PLUS
929   | MINUS
930   ;
932 ;; term: factor (('*'|'/'|'%'|'//') factor)*
933 term
934   : factor
935   | term term_operator factor
936     ()
937   ;
939 term_operator
940   : MULT
941   | DIV
942   | MOD
943   | DIVDIV
944   ;
946 ;; factor: ('+'|'-'|'~') factor | power
947 factor
948   : prefix_operators factor
949     ()
950   | power
951   ;
953 ;; ('+'|'-'|'~')
954 prefix_operators
955   : PLUS
956   | MINUS
957   | TILDE
958   ;
960 ;; power: atom trailer* ('**' factor)*
961 power
962   : atom trailer_zom exponent_zom
963     (concat $1
964             (if $2 (concat " " $2 " ") "")
965             (if $3 (concat " " $3) "")
966             )
967   ;
969 trailer_zom
970   : ;;EMPTY
971   | trailer_zom trailer
972     ()
973   ;
975 exponent_zom
976   : ;;EMPTY
977   | exponent_zom EXPONENT factor
978     ()
979   ;
981 ;; trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
982 trailer
983   : PAREN_BLOCK
984     ()
985   | BRACK_BLOCK
986     ()
987   | PERIOD NAME
988     ()
989   ;
991 ;; atom: '(' [testlist] ')' | '[' [listmaker] ']' | '{' [dictmaker] '}'
992 ;;     | '`' testlist '`'   | NAME | NUMBER | STRING+
993 atom
994   : PAREN_BLOCK
995     ()
996   | BRACK_BLOCK
997     ()
998   | BRACE_BLOCK
999     ()
1000   | BACKQUOTE testlist BACKQUOTE
1001     ()
1002   | NAME
1003   | NUMBER_LITERAL
1004   | one_or_more_string
1005   ;
1007 test_list_opt
1008   : ;;EMPTY
1009   | testlist
1010     ()
1011   ;
1013 ;; testlist: test (',' test)* [',']
1014 testlist
1015   : comma_sep_test_list comma_opt
1016   ;
1018 ;; test (',' test)*
1019 comma_sep_test_list
1020   : test
1021   | comma_sep_test_list COMMA test
1022     (format "%s, %s" $1 $3)
1023   ;
1025 ;; (read $1) and (read $2) were done before to peel away the double quotes.
1026 ;; However that does not work for single quotes, so it was taken out.
1027 one_or_more_string
1028   : STRING_LITERAL
1029   | one_or_more_string STRING_LITERAL
1030     (concat $1 $2)
1031   ;
1033 ;;;****************************************************************************
1034 ;;;@ lambdef
1035 ;;;****************************************************************************
1037 ;; lambdef: 'lambda' [varargslist] ':' test
1038 lambdef
1039   : LAMBDA varargslist_opt COLON test
1040     (format "%s %s" $1 (or $2 ""))
1041   ;
1043 ;; [varargslist]
1044 varargslist_opt
1045   : ;;EMPTY
1046   | varargslist
1047   ;
1049 ;; varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME] | '**' NAME)
1050 ;;             | fpdef ['=' test] (',' fpdef ['=' test])* [',']
1051 varargslist
1052   : fpdef_opt_test_list_comma_zom rest_args
1053     (nconc $2 $1)
1054   | fpdef_opt_test_list comma_opt
1055   ;
1057 ;; ('*' NAME [',' '**' NAME] | '**' NAME)
1058 rest_args
1059   : MULT NAME multmult_name_opt
1060     () ;;(VARIABLE-TAG $2 nil nil)
1061   | EXPONENT NAME
1062     () ;;(VARIABLE-TAG $2 nil nil)
1063   ;
1065 ;; [',' '**' NAME]
1066 multmult_name_opt
1067   : ;;EMPTY
1068   | COMMA EXPONENT NAME
1069     (VARIABLE-TAG $3 nil nil)
1070   ;
1072 fpdef_opt_test_list_comma_zom
1073   : ;;EMPTY
1074   | fpdef_opt_test_list_comma_zom fpdef_opt_test COMMA
1075     (nconc $2 $1)
1076   ;
1078 ;; fpdef ['=' test] (',' fpdef ['=' test])*
1079 fpdef_opt_test_list
1080   : fpdef_opt_test
1081   | fpdef_opt_test_list COMMA fpdef_opt_test
1082     (nconc $3 $1)
1083   ;
1085 ;; fpdef ['=' test]
1086 fpdef_opt_test
1087   : fpdef eq_test_opt
1088   ;
1090 ;; fpdef: NAME | '(' fplist ')'
1091 fpdef
1092   : NAME
1093     (VARIABLE-TAG $1 nil nil)
1094  ;; Below breaks the parser.  Don't know why, but my guess is that
1095  ;; LPAREN/RPAREN clashes with the ones in function_parameters.
1096  ;;  | LPAREN fplist RPAREN
1097  ;;    (identity $2)
1098   ;
1100 ;; fplist: fpdef (',' fpdef)* [',']
1101 fplist
1102   : fpdef_list comma_opt
1103   ;
1105 ;; fpdef (',' fpdef)*
1106 fpdef_list
1107   : fpdef
1108   | fpdef_list COMMA fpdef
1109   ;
1111 ;; ['=' test]
1112 eq_test_opt
1113   : ;;EMPTY
1114   | ASSIGN test
1115     ()
1116   ;
1118 ;;;****************************************************************************
1119 ;;;@ Misc
1120 ;;;****************************************************************************
1122 ;; [',']
1123 comma_opt
1124   : ;;EMPTY
1125   | COMMA
1126   ;
1128 ;; [';']
1129 semicolon_opt
1130   : ;;EMPTY
1131   | SEMICOLON
1132   ;
1134 ;;; wisent-python.wy ends here