jonathf/matlab2cpp

Mer problem med \r

Closed this issue · 3 comments

Följande fil:

function [x y] = snbin(A,nmv,m,n)
% Stochastic matrix-free binormalization for nonsymmetric real A.
% [x y] = snbin(A,nmv,m,n)
%   A is a matrix or function handle. If it is a function handle, then
%     v = A(x) returns A*x and v = A(x,'trans') returns A'*x.
%   nmv is the number of forward and transpose matrix-vector product pairs to
%     perform.
%   m,n is the size of the matrix. It is necessary to specify these only if
%     A is a function handle.
%   diag(x) A diag(y) is approximately binormalized.

% Jan 2010. Algorithm and code by Andrew M. Bradley (ambrad@stanford.edu).
% Aug 2010. New omega schedule.

  %op = isa(A,'function_handle');
  %if(~op) [m n] = size(A); end
  r = ones(m,1); c = ones(n,1);
  for(k = 1:nmv)
    % omega^k
    alpha = (k - 1)/nmv;
    omega = (1 - alpha)*1/2 + alpha*1/nmv;
    % rows
    s = randn(n,1)./sqrt(c);
    if(op) y = A(s); else y = A*s; end
    r = (1-omega)*r/sum(r) + omega*y.^2/sum(y.^2);
    % columns
    s = randn(m,1)./sqrt(r);
    if(op) y = A(s,'trans'); else y = (s'*A)'; end
    c = (1-omega)*c/sum(c) + omega*y.^2/sum(y.^2);
  end
  x = 1./sqrt(r);
  y = 1./sqrt(c);

Ger följande output:

reading file...
running preproc...
writing preproc...
building token-tree...
line 2:0 mismatched input 'r' expecting '
}'
line 9:1 missing '
}' at '\n'
line 10:0 mismatched input 'r' expecting <EOF>
writing pickle...
writing backup...
cfg missing!
loading scope...
dumping scope...
generate tree...
iterating 98 nodes
alpha Var TYPE unknown
 Neg TYPE expression
 Int int int
 Int int int
s Var TYPE unknown
A Var TYPE unknown
s Var TYPE unknown
op Var TYPE unknown
nmv Var TYPE unknown
 Int int int
 Int int int
 Int int int
 Plus TYPE expression
 Neg TYPE expression
k Var TYPE unknown
 Mul TYPE expression
y Var TYPE unknown
A Get TYPE unknown
y Var TYPE unknown
 Paren TYPE expression
c Var TYPE unknown
 Int int int
n Var TYPE unknown
 Div TYPE expression
alpha Var TYPE unknown
 Div TYPE expression
 Paren TYPE expression
 Plus TYPE expression
 Assign TYPE unknown
 Assign TYPE unknown
 Cond TYPE code_block
sqrt Get TYPE unknown
randn Get TYPE unknown
 Mul TYPE expression
 Mul TYPE expression
nmv Var TYPE unknown
 Paren TYPE expression
 Else TYPE code_block
 If TYPE code_block
 Eldiv TYPE expression
s Var TYPE unknown
 Plus TYPE expression
omega Var TYPE unknown
 Div TYPE expression
alpha Var TYPE unknown
 Branch TYPE code_block
 Assign TYPE unknown
 Assign TYPE unknown
 Assign TYPE unknown
nmv Var TYPE unknown
 Int int int
 Int int int
n Var TYPE unknown
 Int int int
_retval Var int unknown
 Block TYPE code_block
 Colon TYPE expression
k Var TYPE unknown
ones Get TYPE unknown
c Var TYPE unknown
 Assign TYPE unknown
 For TYPE code_block
 Assign TYPE unknown
argv Var char char
argc Var int int
_retval Var int int
A Declare TYPE unknown
y Declare TYPE unknown
op Declare TYPE unknown
s Declare TYPE unknown
omega Declare TYPE unknown
alpha Declare TYPE unknown
nmv Declare TYPE unknown
k Declare TYPE unknown
n Declare TYPE unknown
c Declare TYPE unknown
_retval Declare int unknown
n Var TYPE unknown
m Var TYPE unknown
nmv Var TYPE unknown
A Var TYPE unknown
y Var TYPE unknown
x Var TYPE unknown
y Declare TYPE unknown
x Declare TYPE unknown
 Block TYPE code_block
 Params TYPE func_return
 Returns TYPE func_return
 Declares TYPE func_return
 Params TYPE func_returns
 Returns TYPE func_returns
 Declares TYPE func_returns
usingarma Include TYPE program
armadillo Include TYPE program
main Func TYPE func_return
snbin Func TYPE func_returns
Traceback (most recent call last):
  File ".\mconvert.py", line 39, in <module>
    tree = matlab2cpp.main(path, opt.suggestion, disp=opt.display)
  File "C:\Python27\lib\site-packages\matlab2cpp\__init__.py", line 222, in main
    tree.generate(disp=disp)
  File "C:\Python27\lib\site-packages\matlab2cpp\node.py", line 147, in generate
    " is misbehaving\n" + value + "\n"+str(prop))
SyntaxError: interpolation in func_returns.Func is misbehaving
void %(name)s(%(1)s, %(2)s)
{
%(0)s
%(3)s
}
{'index': 0, 'name': u'snbin', 'pointers': 0, 'value': '', '1': 'TYPE& x, TYPE& y', '0': 'TYPE x, y ;', '2': 'TYPE A, TY
PE nmv, TYPE m, TYPE n', '-1': 'TYPE A, TYPE nmv, TYPE m, TYPE n', 'ret': "'void %(name)s(%(1)s, %(2)s)\\n{\\n%(0)s\\n%(
3)s\\n}'", 'str': '', '-2': 'TYPE& x, TYPE& y', 'type': <matlab2cpp.datatype.datatype object at 0x02513590>, 'class': 'F
unc', '-3': 'TYPE x, y ;', 'backend': 'func_returns'}

Wouldn't it be possible to let Python handle the line endings automatically?

https://docs.python.org/2/library/functions.html#open :

In addition to the standard fopen() values mode may be 'U' or 'rU'. Python is usually built with universal
newlines support; supplying 'U' opens the file as a text file, but lines may be terminated by any of the
following: the Unix end-of-line convention '\n', the Macintosh convention '\r', or the Windows convention
'\r\n'. All of these external representations are seen as '\n' by the Python program. If Python is built
without universal newlines support a mode with 'U' is the same as normal text mode. Note that file objects so
opened also have an attribute called newlines which has a value of None (if no newlines have yet been seen),
'\n', '\r', '\r\n', or a tuple containing all the newline types seen.

Godt forslag. Implementert det nå.
Har du muligheten til å ta en titt på om det fungerte?

Jag får fortfarande en massa fel från antlr (på alla filer),

line 1:33 token recognition error at: '\r'
line 2:6 token recognition error at: '\r'
line 3:11 token recognition error at: '\r'
....

Men ingenting krashar, och konverteringen verkar fungera bra på många enkla filer nu, förutom att det saknas en semikolon efter "return" (vet inte om det är relaterat).