Python Forum

Full Version: trouble with semicolons
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
i'm still having trouble with semicolons. but it does not seem to be the spacing issue i previously thought it was. here is my latest example (avoiding a big 900 line file that has it today) [under Xubuntu 20.04 and Python 3.8]:
Output:
lt1a/phil/6 /home/phil 18> box try_semicolon.py try_fred.py +----<try_semicolon.py>----------------------+ | fred=6 | | print('one') | | print('two') | | print('three');print('four');print('five') | | print('six') | | print('seven') | +--------------------------------------------+ +----<try_fred.py>-----------------------------------------------------+ | fred=8 | | print('one') | | print('two') | | print('three');print('four');print('five');if fred==8: print('fred') | | print('six') | | print('seven') | +----------------------------------------------------------------------+ lt1a/phil/6 /home/phil 19> python3.8 try_semicolon.py one two three four five six seven lt1a/phil/6 /home/phil 20> python3.8 try_fred.py File "try_fred.py", line 4 print('three');print('four');print('five');if fred==8: print('fred') ^ SyntaxError: invalid syntax lt1a/phil/6 /home/phil 21>
what am i doing wrong in this case?
(Apr-25-2023, 07:18 PM)Skaperen Wrote: [ -> ]what am i doing wrong in this case?
It is all contained in Python's grammar specification. Look at the following rules
Output:
simple_stmts: | simple_stmt !';' NEWLINE # Not needed, there for speedup | ';'.simple_stmt+ [';'] NEWLINE # NOTE: assignment MUST precede expression, else parsing a simple assignment # will throw a SyntaxError. simple_stmt: | assignment | star_expressions | return_stmt | import_stmt | raise_stmt | 'pass' | del_stmt | yield_stmt | assert_stmt | 'break' | 'continue' | global_stmt | nonlocal_stmt compound_stmt: | function_def | if_stmt | class_def | with_stmt | for_stmt | try_stmt | while_stmt | match_stmt
As we can see, the semi-colon is used in the simple_stmts syntactic category, and it is used to separate occurrences of simple_stmt. But the if statement falls under the category "compound statement", and not "simple statement".

You are writing something that the grammar doesn't allow.
what is "simple_stmts" vs "simple_stmt"? the grammar is using both, each in different places but no distinct definition.

> # NOTE: assignment MUST precede expression, else parsing a simple assignment
> # will throw a SyntaxError.

what does this effectively mean? if those print calls had been assignments, it would work?
> # * Strings with double quotes (") denote SOFT KEYWORDS
> # * Strings with single quotes (') denote KEYWORDS

i thought ' and " worked the same in Python. they do work different in Bash, for example.
(Apr-25-2023, 09:31 PM)Skaperen Wrote: [ -> ]what is "simple_stmts" vs "simple_stmt"? the grammar is using both, each in different places but no distinct definition.
In the excerpt I posted above, you have the two definitions of simple_stmts and simple_stmt Basically a simple_stmts is a semi-colon separated sequence of simple_stmt.
(Apr-25-2023, 09:31 PM)Skaperen Wrote: [ -> ]# NOTE: assignment MUST precede expression,
This is a note for the developpers of the grammar. The order of the production rules apparently matters to the parser. The note is irrelevant for the Python users.
(Apr-25-2023, 09:36 PM)Skaperen Wrote: [ -> ]i thought ' and " worked the same in Python.
Again, this is about the use of ' and " in the grammar syntax, not the Python syntax. If you look in the grammar, you'll see that normal keywords such as 'for' are written with simple quotes, while soft keywords such as "case" are written with double quotes. This is only relevant to the parser of the grammar, not the Python parser.
Also using semicolons this way is generally not advised,i use it very rarely maybe when use python -c from command line(because you have no way to use actual line breaks in that case).

What PEP-8 say.
Quote:
  • Compound statements (multiple statements on the same line) are generally discouraged:
Quote:
# Correct:
if foo == 'blah':
    do_blah_thing()
do_one()
do_two()
do_three()
Rather not:
# Wrong:
if foo == 'blah': do_blah_thing()
do_one(); do_two(); do_three()
(Apr-26-2023, 03:42 PM)snippsat Wrote: [ -> ]using semicolons this way is generally not advised,i use it very rarely maybe when use python -c from command line(because you have no way to use actual line breaks in that case).
i have used actual line breaks at the command line with python -c many times. in bash this can be done like:
Output:
lt1a/forums/3 /home/forums 10> echo $'foo\nbar' foo bar lt1a/forums/3 /home/forums 11>
(note: this requires single quotes; double quotes do not work)