[erlang-questions] Why is this expression illegal?

Dieter Schön dieter@REDACTED
Fri Jan 18 18:37:12 CET 2019


Hi,

in

http://erlang.org/doc/programming_examples/bit_syntax.html#constructing-binaries-and-bitstrings

I found:

<quote>

As mentioned earlier, segments have the following general syntax:

Value:Size/TypeSpecifierList

When constructing binaries, Value and Size can be any Erlang expression. 
However, for syntactical reasons, both Value and Size must be enclosed 
in parenthesis if the expression consists of anything more than a single 
literal or a variable. The following gives a compiler syntax error:

<<X+1:8>>

This expression must be rewritten into the following, to be accepted by 
the compiler:

<<(X+1):8>>
</quote>

Seems to me that this condition makes live for the parser a bit easier.

kind regards,
Dieter

On 18.01.19 15:25, Jeroen Koops wrote:
> Thanks Jachym and Brujo,
>
> I did try
>
> << (case foo of foo -> 1; bar -> 2 end) bor (case foo of foo -> 8; bar 
> -> 16 end) >>.
>
> which didn't work, but
>
> << (case foo of foo -> 1; bar -> 2 end bor case foo of foo -> 8; bar 
> -> 16 end) >>.
>
> indeed does work.
>
>
>
> On Fri, Jan 18, 2019 at 3:19 PM Jáchym Holeček <freza@REDACTED 
> <mailto:freza@REDACTED>> wrote:
>
>     Hi Joeren,
>
>     My guess: allowing entirely arbitrary expression within binary
>     constructor
>     syntax could lead to ambiguities with respect to `/` and `-` and
>     `:` tokens
>     which have special meaning in this context? Therefore only select
>     expression
>     forms are permitted, which are free of ambiguities, one of them
>     being `( E )`
>     which in turn opens access to arbitrary expressions if one is a
>     bit more explicit.
>
>     So this will work:
>
>         4> << (case foo of foo -> 1; bar -> 2 end bor case foo of foo
>     -> 8; bar -> 16 end) >>.
>         <<"\t">>
>
>     The file `stdlib-3.4.4/src/erl_parse.yrl` has the mechanics of it
>     (search for `binary`,
>     `bin_elements`, `bin_element`) though not the underlying reasoning.
>
>     HTH,
>         -- Jachym
>
>     On Fri, Jan 18, 2019, at 15:04, Jeroen Koops wrote:
>>     Hi List!
>>
>>     If this expressions is legal:
>>
>>     case A of foo -> 1; bar -> 2 end bor case A of foo -> 8; bar ->
>>     16 end.
>>
>>
>>     ... and this expression is legal:
>>
>>
>>
>>     << case A of foo -> 1; bar -> 2 end >>.
>>
>>
>>     ... then why is this expression illegal:
>>
>>
>>
>>     << case A of foo -> 1; bar -> 2 end bor case A of foo -> 8; bar
>>     -> 16 end >>.
>>
>>
>>     (shell responds with "* 1: syntax error before: 'bor'")
>>
>>
>>     Thanks,
>>
>>
>>     -- 
>>     Jeroen Koops
>>
>>     M: koops.j@REDACTED <mailto:koops.j@REDACTED>
>>     T: +31-6-55590300
>>     _________________________________________________
>>     erlang-questions mailing list
>>     erlang-questions@REDACTED <mailto:erlang-questions@REDACTED>
>>     http://erlang.org/mailman/listinfo/erlang-questions
>
>     _______________________________________________
>     erlang-questions mailing list
>     erlang-questions@REDACTED <mailto:erlang-questions@REDACTED>
>     http://erlang.org/mailman/listinfo/erlang-questions
>
>
>
> -- 
> Jeroen Koops
>
> M: koops.j@REDACTED <mailto:koops.j@REDACTED>
> T: +31-6-55590300
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20190118/809e75b7/attachment.htm>


More information about the erlang-questions mailing list