在Erlang中编译错误:"无法解析文件,放弃"

bpr*_*bpr 1 erlang

我刚开始看Erlang并尝试编译我的第一个程序.与教程相比,一切看起来都不对,但我似乎无法摆脱这个错误.

这是我的代码,保存在'useless.erl'下:

-module(useless).
-export([add/2, hello/0, greet_and_add_two/1]).

add(A,B) ->
A + B. 

%% Shows greetings.
%% io:format/1 is the standard function used to output text.
hello() ->
io:format("Hello, world!~n").

greet_and_add_two(X) ->
hello(),
add(X,2).
Run Code Online (Sandbox Code Playgroud)

我将目录更改为useless.erl的目录:

cd("C:/Users/CP/Documents/Erlang").
Run Code Online (Sandbox Code Playgroud)

但是,当我跑

c(useless).
Run Code Online (Sandbox Code Playgroud)

我明白了

useless.erl:1: cannot parse file, giving up
useless.erl:1: no module definition
error
Run Code Online (Sandbox Code Playgroud)

Lol*_*4t0 5

如果您的文件看起来没问题,但Erlang编译器抱怨它无法解析文件,那么您的代码可能包含无效编码.

根据文档, Erlang只接受源代码UTF-8Latin-1编码.

有效编码是Latin-1和UTF-8,其中字符的大小写可以自由选择

请注意,您仍然只能在字符串和注释中使用扩展字符集:

在Erlang/OTP R16B中,Erlang令牌的语法被扩展为处理Unicode.支持仅限于字符串文字和注释.

如果出现解析错误或非法字符错误,您应该这样做

  1. 确保您的文件已保存UTF-8Latin-1编码
  2. 确保不要在注释和字符串之外使用UTF文字
  3. 使用较旧的Erlang版本(在Erlang 17之前),您可以尝试在文件的前两行中明确指定编码

    %% coding: utf-8
    %%% or
    %% For this file we have chosen encoding = Latin-1
    %%% or
    %% -*- coding: latin-1 -*-
    
    Run Code Online (Sandbox Code Playgroud)