0

This is straight out of a question I had in a school exam. The question goes:

In the Python Programming Language, syntax error is detected by (a) at (b). (a) and (b) are:

  1. (a): interpreter, (b): compile time
  2. (a): interpreter (b): runtime

Syntax errors are found out before runtime, right? So the answer should be 1? My teacher's reasoning, I believe, is that Python is an 'interpreted language', so, 1 becomes wrong. But, afaik, Python is compiled to C after which it is run.

I am unable to find any relevant documentation, some pointing in the right direction will be appreciated.

  • 2
    Compiled to bytecode, not compiled to C. – khelwood Nov 08 '21 at 13:35
  • Unless you're writing directly in machine code, *every* language needs a parse and compile phase. In "interpreted languages" this happens to happen implicitly when you run your program straight from the source file. So… it's debatable, but it definitely happens in the parse/compile *phase*. – deceze Nov 08 '21 at 13:35
  • Yeah, the _Cython_ project compiles Python to C (for building extensions etc.); the regular interpreter(s) do not compile to C. – AKX Nov 08 '21 at 13:36
  • 1
    Related: [Is Python interpreted, or compiled, or both?](https://stackoverflow.com/q/6889747/3890632) – khelwood Nov 08 '21 at 13:36
  • 2
    Python is compiled to bytecode, then the bytecode is interpreted. These types of questions cause more semantic arguments than it's worth. The trick is to build a mindreading device to keep your teacher happy. They probably want you to pick 2 because there's no compilation step to machine code. – byxor Nov 08 '21 at 13:37
  • 1
    All this said – you can get a SyntaxError at runtime with e.g. `eval("(")`, but that's probably splitting hairs – AKX Nov 08 '21 at 13:37
  • 5
    If you like, tell your teacher that Stack Overflow says it's a bad question. – khelwood Nov 08 '21 at 13:42
  • But in the general case, regardless, there is no way to execute code whose syntax is not valid; what code would you generate and run if you cannot parse it? – tripleee Nov 08 '21 at 13:52
  • 1
    @tripleee but what if your language's definition of "valid code" is so broad that [paint splatters](https://www.mcmillen.dev/sigbovik/) are valid code? – Nullman Nov 09 '21 at 13:00
  • Then obviously those do not contain syntax errors. But this was a question about Python. – tripleee Nov 09 '21 at 13:04

0 Answers0