LangProc / langproc-2021-cw

5 stars 4 forks source link

Question about ANSI C #22

Closed Mengyuan410 closed 2 years ago

Mengyuan410 commented 2 years ago

Hello Sir, may I ask something about the ANSI C specification? This is the standard parser for ANSI C. image Does it mean for ANSI C, declaration can only happen before statement in compound statement? And the following situation (i.e. declaration and statement mixing together) won't happen? image

ymherklotz commented 2 years ago

Yeah exactly, that is the case. however, I wouldn't trust the grammar file to get it right, so I would always double check the standard.

However, you can also try the following command, which can maybe quickly tell you if you are obeying the C89 standard (but it might not always error out if you are not obeying it).

gcc -std=c89 -pedantic -Wall -Wextra -Wundef -Werror main.c
johnwickerson commented 2 years ago

That’s right. In newer versions of C, the programmer has more flexibility about where they can declare variables. But that won’t be exploited in the test cases I give your compiler.