# Compiler complains about "unexpected end"

**URL:** <https://discourse.julialang.org/t/compiler-complains-about-unexpected-end/83037>\
**Category:** New to Julia\
**Created:** [June 20, 2022, 2:11am UTC](https://discourse.julialang.org/t/compiler-complains-about-unexpected-end/83037 "2022-06-20T02:11:32Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Steve\_Strongheart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steve_strongheart/32/18675_2.png) [@Steve\_Strongheart](https://discourse.julialang.org/u/Steve_Strongheart)\
**Post date:** [June 20, 2022, 2:11am UTC](https://discourse.julialang.org/t/compiler-complains-about-unexpected-end/83037/1 "2022-06-20T02:11:32Z")

</div>

Compiler complains about “unexpected end” on line 22, but if I remove it I get this error:  
syntax: incomplete: “function” at In[35]:1 requires end

Stacktrace:  
[1] top-level scope at In[35]:1  
[2] include\_string(::Function, ::Module, ::String, ::String) at ./loading.jl:1091

the code should factor numbers return a Dict and print it.

function crack(n)  
dd=Dict(1=\>n)  
d=2  
p=0  
while n\>=d && n%d==0  
n/=d  
p+=1  
end  
if p\>0  
dd[d]=p  
end  
d=1  
while n\>=d  
p=0  
d+=2  
while n\>=d  
if n%d!=0  
break  
end /// this end  
n/=d  
p++  
end  
if p\>0  
dd[d]=p  
end  
end  
return dd  
end

dd=crack(24)

for (d,p) in dd  
println(d,“=\>”,p)  
end

I don’t get it. Every control statement has its own end statement - Jupyter even indented them properly.

---

<div class="post-metadata">

**Author:** ![aramirezreyes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aramirezreyes/32/42573_2.png) [@aramirezreyes](https://discourse.julialang.org/u/aramirezreyes)\
**Post date:** [June 20, 2022, 2:22am UTC](https://discourse.julialang.org/t/compiler-complains-about-unexpected-end/83037/2 "2022-06-20T02:22:59Z")

</div>

Can you write your code between triple backticks so that it is formatted?

Also the line that says `end /// this end` is not valid julia syntax. Did you try to write a comment? In julia comments are indicated with `#`.

Also `p++` is not correct julia syntax, it should be `p = p + 1` (I think this is your actual error).

Did you copy this from code in another language?

---

<div class="post-metadata">

**Author:** ![Steve\_Strongheart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steve_strongheart/32/18675_2.png) [@Steve\_Strongheart](https://discourse.julialang.org/u/Steve_Strongheart)\
**Post date:** [June 20, 2022, 3:19am UTC](https://discourse.julialang.org/t/compiler-complains-about-unexpected-end/83037/3 "2022-06-20T03:19:22Z")

</div>

This post was temporarily hidden by the community for possibly being off-topic, unfocused, inappropriate, or spammy.

---

<div class="post-metadata">

**Author:** ![Steve\_Strongheart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steve_strongheart/32/18675_2.png) [@Steve\_Strongheart](https://discourse.julialang.org/u/Steve_Strongheart)\
**Post date:** [June 20, 2022, 3:24am UTC](https://discourse.julialang.org/t/compiler-complains-about-unexpected-end/83037/4 "2022-06-20T03:24:50Z")

</div>

This post was temporarily hidden by the community for possibly being off-topic, unfocused, inappropriate, or spammy.

---

<div class="post-metadata">

**Author:** ![Steve\_Strongheart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steve_strongheart/32/18675_2.png) [@Steve\_Strongheart](https://discourse.julialang.org/u/Steve_Strongheart)\
**Post date:** [June 20, 2022, 3:26am UTC](https://discourse.julialang.org/t/compiler-complains-about-unexpected-end/83037/5 "2022-06-20T03:26:52Z")

</div>

```julia
function crack(n)
    dd=Dict(1=>n)
    d=2
    p=0
    while n>=d && n%d==0
        n/=d
        p+=1
    end
    if p>0
        dd[d]=p
    end
    d=1
    while n>=d
        p=0
        d+=2
        while n>=d 
            if n%d!=0
                break
            end
            n/=d
            p++
         
        if p>0
            dd[d]=p
        end
    end
    return dd
end
        
        
dd=crack(24)

for (d,p) in dd
    println(d,"=>",p)
end
    

```

---

<div class="post-metadata">

**Author:** ![Steve\_Strongheart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steve_strongheart/32/18675_2.png) [@Steve\_Strongheart](https://discourse.julialang.org/u/Steve_Strongheart)\
**Post date:** [June 20, 2022, 3:50am UTC](https://discourse.julialang.org/t/compiler-complains-about-unexpected-end/83037/6 "2022-06-20T03:50:19Z")

</div>

No that was not a comment, just a marker to show you where the compiler marked the complaint.  
If I delete the end it;s an error- missing end tag.  
If I keep the end tag the compiler barfs.  
I believe I correctly assume that all controls need their own end tags.

---

<div class="post-metadata">

**Author:** ![Steve\_Strongheart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steve_strongheart/32/18675_2.png) [@Steve\_Strongheart](https://discourse.julialang.org/u/Steve_Strongheart)\
**Post date:** [June 20, 2022, 3:51am UTC](https://discourse.julialang.org/t/compiler-complains-about-unexpected-end/83037/7 "2022-06-20T03:51:54Z")

</div>

This is the python code for the same factoring

```julia

from sys import argv

def crack(N):
	ff={}
	n=int(N)
	ff[0]=n;
	d=2
	p=0
	while n>=d and not n%d:
		n/=d
		p+=1
	if p>0:
		ff[d]=p
	
	d=1
	while (n>=d):
		d+=2
		p=0
		while n>=d and not n%d:
			n/=d
			p+=1
		if p>0:
			ff[d]=p
	return ff

def stringy(ff):
	dd=[d for d in ff.keys()]
	x=dd.index(0)
	n=ff[0];
	if n in ff:
		dd.pop(ff[n])
	print(ff)
	print(f'x={x} dd={dd}')
	dd.pop(x)
	print(f'pop dd={dd}')
	
	s=f'{ff[0]} ='
	if len(dd) == 0: 
		return s+"PRIME"
	for d in dd:
		s+=f'{d}^{ff[d]} * '

	return s[0:-2]
	

if __name__ ==' __main__':
	
	n=1
	for a in argv[1:]:
		ff=crack(a)
		print(f'cracking {a}')
		print(f'{n}) {stringy(ff)}')
		n+=1
		print()
``
```

---

<div class="post-metadata">

**Author:** ![melonedo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/melonedo/32/15503_2.png) [@melonedo](https://discourse.julialang.org/u/melonedo)\
**Post date:** [June 20, 2022, 3:59am UTC](https://discourse.julialang.org/t/compiler-complains-about-unexpected-end/83037/8 "2022-06-20T03:59:01Z")

</div>

```julia
    while n>=d
        p=0
        d+=2
        while n>=d 
            if n%d!=0
                break
            end
            n/=d
            p++ # what is p++?
        # no end here?
        if p>0
            dd[d]=p
        end
    end

```

Proper formatting already highlights the issue.

---

<div class="post-metadata">

**Author:** ![aramirezreyes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aramirezreyes/32/42573_2.png) [@aramirezreyes](https://discourse.julialang.org/u/aramirezreyes)\
**Post date:** [June 20, 2022, 4:06am UTC](https://discourse.julialang.org/t/compiler-complains-about-unexpected-end/83037/9 "2022-06-20T04:06:58Z")

</div>

Yes you are correct in that everything needs an end. As explained in my previous response the problem is that `p++` is not correct julia code, that is confusing the compiler. This works:

```julia
function crack(n)
    dd=Dict(1=>n)
    d=2
    p=0
    while n>=d && n%d==0
        n/=d
        p+=1
    end
    if p>0
        dd[d]=p
    end
    d=1
    while n>=d
        p=0
        d+=2
        while n>=d 
            if n%d!=0
                break
            end
            n/=d
            p = p + 1
        end
        if p>0
            dd[d]=p
        end
    end
    return dd
end
        
        
dd=crack(24)

```

---

<div class="post-metadata">

**Author:** ![Steve\_Strongheart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steve_strongheart/32/18675_2.png) [@Steve\_Strongheart](https://discourse.julialang.org/u/Steve_Strongheart)\
**Post date:** [June 20, 2022, 12:31pm UTC](https://discourse.julialang.org/t/compiler-complains-about-unexpected-end/83037/10 "2022-06-20T12:31:49Z")

</div>

This post was temporarily hidden by the community for possibly being off-topic, unfocused, inappropriate, or spammy.

---

<div class="post-metadata">

**Author:** ![StevenWhitaker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevenwhitaker/32/9749_2.png) [@StevenWhitaker](https://discourse.julialang.org/u/StevenWhitaker)\
**Post date:** [June 20, 2022, 1:50pm UTC](https://discourse.julialang.org/t/compiler-complains-about-unexpected-end/83037/11 "2022-06-20T13:50:34Z")

</div>

> [@Steve\_Strongheart](#):
>
> Rather than point to the actual error “p++”

Actually, the error isn’t `p++` in and of itself; the error is that `++` is parsed as an infix operator (that is undefined by default but can be defined by the user), but `p++` (or `p ++`) only provides one argument (`p`) to the function call, whereas two arguments are expected (something like `p ++ a`). Thus, when the parser encounters `p++` followed by `end`, the `end` is unexpected because the parser expected another argument to the `++` function call. You would get the same error if instead of `p++` you had `p+`.

But I agree that it seems like there should be a way to make the error message more informative of the actual error (a missing argument to an infix operation). It probably also would make sense to make a special error message for `p++` to point out that `p++` is not the same as `p += 1`.

* * *

I was curious, and it looks like the same error occurs if you replace `p++` with `++(p,` (i.e., an incomplete function call, which is what `p++` is). I would think the error message could be more informative in this case, too.

Of course, I have no experience with Julia’s parser, so I have no idea how easy it would actually be to improve these error messages.

* * *

> [@Steve\_Strongheart](#):
>
> A somewhat retarded compiler.

Also, a lot of smart people with little time work on Julia (I’m not one of them), so if you could kindly refrain from derogatory comments towards people’s hard work that would be appreciated. It is sufficient to point out what can be improved.

---

<div class="post-metadata">

**Author:** ![StevenWhitaker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevenwhitaker/32/9749_2.png) [@StevenWhitaker](https://discourse.julialang.org/u/StevenWhitaker)\
**Post date:** [June 23, 2022, 2:14pm UTC](https://discourse.julialang.org/t/compiler-complains-about-unexpected-end/83037/12 "2022-06-23T14:14:42Z")

</div>

> [@StevenWhitaker](#):
>
> But I agree that it seems like there should be a way to make the error message more informative of the actual error (a missing argument to an infix operation). It probably also would make sense to make a special error message for `p++` to point out that `p++` is not the same as `p += 1`.

I opened a couple of issues:

> <https://github.com/JuliaLang/julia/issues/45791>
>
> I'm opening this issue based on a \[question on discourse\](https://discourse.juli…alang.org/t/compiler-complains-about-unexpected-end/83037).
> 
> Consider the following code:
> \`\`\`julia
> if true
> x +
> end
> \`\`\`
> 
> This will result in (at least on Julia 1.7.0)
> \`\`\`julia
> ERROR: syntax: unexpected "end"
> \`\`\`
> 
> This error makes sense from the parser's perspective: the \`end\` is unexpected because a second argument to \`+\` is expected. But from the programmer's perspective, one could argue that the \`end\` is exactly where one would expect. The real error is that the \`+\` function call is missing an argument.
> 
> Could the error message be changed to point to the missing argument instead? Something like
> \`\`\`julia
> ERROR: syntax: attempted to call an infix operator with just one argument
> \`\`\`
> 
> \---
> 
> The \`unexpected "end"\` error also occurs when \`x +\` is replaced with, e.g., \`f(x,\`. In this case, a better error message might be something like
> \`\`\`julia
> ERROR: syntax: incomplete function call - missing closing parenthesis ")"
> \`\`\`

> <https://github.com/JuliaLang/JuliaSyntax.jl/issues/376>
>
> I'm opening this issue based on a \[question on discourse\](https://discourse.juli…alang.org/t/compiler-complains-about-unexpected-end/83037).
> 
> Consider the following code:
> \`\`\`julia
> i = 0
> for n = 1:5
> i++
> end
> \`\`\`
> 
> This will result in (at least on Julia 1.7.0)
> \`\`\`julia
> ERROR: syntax: unexpected "end"
> \`\`\`
> 
> This error message could benefit from JuliaLang/julia#45791. However, I'm doing a separate issue for this special case because a new user might write \`i++\` not knowing it is not equivalent to \`i += 1\` (or even that \`++\` is parsed as an infix operator). Would it be possible to have a more specific error message? Something like
> \`\`\`julia
> ERROR: syntax: attempted to call an infix operator with just one argument - perhaps you meant "i += 1"?
> \`\`\`

---

<div class="post-metadata">

**Author:** ![aramirezreyes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aramirezreyes/32/42573_2.png) [@aramirezreyes](https://discourse.julialang.org/u/aramirezreyes)\
**Post date:** [June 23, 2022, 3:20pm UTC](https://discourse.julialang.org/t/compiler-complains-about-unexpected-end/83037/13 "2022-06-23T15:20:03Z")

</div>

Thanks Steven. Also thanks for clarifying that the syntax is allowed. That explains the message.
