Skip to content

if-conditions: difference in tolerance of syntax variations between Nelson and MATLAB #1547

@nicoFe

Description

@nicoFe

Describe the bug
When taking some working very simple MATLAB code and attempting to run it with Nelson, differences in whitespaces in if-conditions yields parsing issues in Nelson

To Reproduce
Steps to reproduce the behavior:

  1. Execute the following code
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%
    a = rand(100,2);
    t = 1:100;

% that one works
if size(a,2)~=length(t)
fprintf('hello from if\n');
else
fprintf('hello from else\n');
end

% that one works too
if (size(a,2) ~= length(t))
fprintf('hello from if\n');
else
fprintf('hello from else\n');
end

% that one fails
if size(a,2) ~= length(t) % this would be line 19
fprintf('hello from if\n');
else
fprintf('hello from else\n');
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Expected behavior
On MATLAB, as expected, the output is three time the string 'hello, from if'.

Screenshots
Error:
Expecting 'end' to match 'if' statement from line 19
at line 19, column 16 of file ........................... (path to my local file)

Desktop (please complete the following information):

  • OS: Windows 11
  • Browser [firefox, edge]
  • Version [1.16.0.5575]

Additional context
The message "Expecting 'end' to match 'for' statement" is also something I could experience when some syntax error was present within the for-loop. This was confusing for beginners (students) as the interpreter was then complaining about the wrong line.

I am not very knowledgeable in code parsing, but I would think that the parsing logic matching the keyword that opens a new scope (e.g. 'if', 'for') and the keyword 'end' at the end of the block, before attempting to interpret the content of the code between those keywords, is not sufficiently robust.

Metadata

Metadata

Labels

Type

No type

Projects

Status

No status

Relationships

None yet

Development

No branches or pull requests

Issue actions