[Community Puzzle] Simple Pascal (PL/0) Compiler - Puzzle discussion

Coding Games and Programming Challenges to Code Better

Send your feedback or ask for help here!

Created by @JDOnline,validated by @Eulero314,@JeZzElLutin and @TAFITA_Julis.
If you have any issues, feel free to ping them.

Thanks for this great, great puzzle!

My solution fails the remote test 4 “Unknown var”. Is this test similar to the corresponding local test? I cannot think of edge cases, except if a procedure has the same name, but that case is not specified in the puzzle instructions. Or a name that is a keyword or not an identifier.

The output could have included line numbers (and variable/procedure names?) to make the result easier to debug and to read.

The puzzle states: “All math and comparison operations work from left to right (e.g. there won’t be cases like 2 + 7 * 9).”. I find this strange, because the “2+7*9” expression is correctly processed by the grammar, and I would expect a hard puzzle with mathematical expressions and parentheses to require operator priority.

var i;
i:=2+7*9 
.

jmp 0, 1
int 0, 4
lit 0, 2
lit 0, 7
lit 0, 9
opr 0, 4 <-- *
opr 0, 2 <-- +
sto 0, 3
opr 0, 0

Validator 4 still features one unknown variable (not procedure), but there’s one known variable at the same time. Hope this will help you find out what the issue of your code may be.

For operator priority, I’ll let the author answer your question. What I do know is that the statement has almost reached the upper limit of 5000 characters, and it’s difficult to squeeze in more characters to state the operator precedence applicable to PL/0 (as all operators need to be mentioned, not just + - * /), if harder cases have to be included.

Thanks! I sometime raised “Invalid expr” instead of “Unknown var” and did not write the right line number.

1 Like

Hi,
Thanks for the great puzzle!
For begin … end blocks, it seems to me that the last statement should not be followed by a ; but directly by “end”.
The examples do that if begin … end is on single lines, but they have a ; after the last statement if the end is on a new line.
This does not correspond to the grammar or the examples on PL/0 - Wikipedia.

I had to relax my parser to allow handling these cases to pass the test vectors.

I have a second issue, maybe related, maybe not: I pass all test vectors, but when submitting the “; missing validator” fails. Did anyone else have an issue like that?

Hi,

You are totally right ! The PL/0 grammar does not require “;” when followed by “end”. My implementation was too permissive on that.

I updated the puzzle validators to correctly enforce the grammar.

Thanks for your support.

I think these test cases still have a ; to many before “end”:
02: Line 13
03: Line 14
15: Line 1

As well as the validators 02 & 12.
I found the bug I had for validator 05 :slight_smile:

I indeed found 2 cases not updated but all others were not wrong based on what I saw :frowning: ?

What is your issue with validator 05 ?

Could someone kindly explain what exactly is wrong with the expression in test case 6?

Input:

5
const k = 10;
var i;
begin
    i := ( 2 * k * -5 ) / 3
end.

Output:

Line 4: Invalid expr

Thanks!

After the multiplication operator * you need a factor. But for a factor to be a number, it has to be a non-negative integer.

Ah, I see now. Thanks!

Very nice one, even if a bit time-consuming :slight_smile:

As is often the case with these complex problems, it is hard to have a very good test coverage both in Tests and Validators. As it happens, I was happy to be able to access the validators, because I had 4 of them failing ! In case others stumble on the same rocks :

  • there are two validators missing the final ., so that the lexer would fail early, and not catch another (obviously earlier) error.
  • I was reporting the error line as the one of the first token that didn’t match, instead of the last understood. This was also only visible in Validators, in Tests all the errors are within a line.

Also Test #15 is still incorrect (according to the grammar), with an illegal ; before an end.