Parse\Runtime Issues

38 views
Skip to first unread message

PhistucK

unread,
Jan 28, 2011, 7:02:16 AM1/28/11
to v8-u...@googlegroups.com
I have some questions and need some clarifications.

When I try this code -
<!DOCTYPE HTML>
<html>
<script>
var a = "o";
var b = "c";
if (a == "o")
 if (b == "b")
 {
  alert("a is o");
  alert("b is b");
 };
else
{
 alert("a is not o");
};
</script>
</html>

It shows -
Uncaught SyntaxError: Unexpected token else

Should that error be triggered? can you explain why?



Also, running it without that semicolon -
<!DOCTYPE HTML>
<html>
<script>
var a = "o";
var b = "c";
if (a == "o")
 if (b == "b")
 {
  alert("a is o");
  alert("b is b");
 }
else
{
 alert("a is not o");
};
</script>
</html>

Shows an alert - "a is not o".
As far as I know, "if" and "else" count as two statements. If they are, the "else" block is actually part of the first "if" statement.
Am I misinformed, or is it a bug?


Thank you for your time!

PhistucK

Lasse R.H. Nielsen

unread,
Jan 28, 2011, 7:15:30 AM1/28/11
to v8-u...@googlegroups.com
The SyntaxError is correct. If you check other browsers, you will see that they give the same result.

The problem here is the ';' after the then-branch of the second/inner if.

An if-statement has the condition followed by exactly one statement (then then-branch), and then, optionally, the keyword "else" and another single statement (the else-branch). That single statement can be, and often is, a block statement.

The block with the two alerts is the then-statment. The following ';' isn't part of that statement (block statements are not terminated by semicolons), so it must be another empty and unrelated statement. I.e., the then-statement was not followed by the "else" keyword, so the if-statement ends there. Ditto for the outer if statement.
Then comes an "else" keyword that's not part of any if-statement, which is a syntax error.

Best of luck
/Lasse





--
Lasse R.H. Nielsen
l...@google.com
'Faith without judgement merely degrades the spirit divine'
Google Denmark ApS - Frederiksborggade 20B, 1 sal - 1360 København K -
Denmark - CVR nr. 28 86 69 84

Stephan Beal

unread,
Jan 28, 2011, 7:17:29 AM1/28/11
to v8-u...@googlegroups.com
On Fri, Jan 28, 2011 at 1:02 PM, PhistucK <phis...@gmail.com> wrote:
<!DOCTYPE HTML>
...</html>
It shows -
Uncaught SyntaxError: Unexpected token else


It _should_ be saying that the unknown token is the DOCTYPE tag, because v8 does not parse HTML.
 
Also, running it without that semicolon -
<!DOCTYPE HTML>

Stop trying to feed HTML to v8, and the problem should go away. Feed it only the content of your script tag.

- Shows an alert - "a is not o".

The you're not using v8 direcly - v8 has no built-in alert() method.

Are you sure you don't mean Chromium instead of v8?

--
----- stephan beal
http://wanderinghorse.net/home/stephan/

Mikael Helbo Kjær

unread,
Jan 28, 2011, 7:18:15 AM1/28/11
to v8-u...@googlegroups.com

Hi there.

 

This is not the usual thing to ask here (I’d go for some sort of Javascript forum in the future).

 

I think you don’t need to include any sort of HTML here to get ppl to look at your JS.

 

First problem:

You have a ; after the end of your if block (I’ve marked it below), that ends the if else statement too early. Else cannot stand alone so to speak.

 

Second problem: your blocks are not entirely right here. The parser can only follow the rules here. The if sentence either deals with the next statement (line) or block. Only if that line or block is directly followed by an else or else if can that be included. A corrected (untested assumption here) example could be (there is more than one way to do it):

 

var a = “0”

var b = “c”

if (a == “0”)

{

    if (b == “b”)

    {

        alert(“a is o”);

        alert(“b is b”);

    }

}

else

{

    alert(“a is not o”);

}

 

/Mikael

 

When I try this code -

<!DOCTYPE HTML>

<html>

<script>

var a = "o";

var b = "c";

if (a == "o")

 if (b == "b")

 {

  alert("a is o");

  alert("b is b");

 }; ß Problem here

PhistucK

unread,
Jan 28, 2011, 7:31:05 AM1/28/11
to v8-u...@googlegroups.com, sgb...@googlemail.com
I am trying it within Chromium.
I posted it to v8-users, because it is a JavaScript issue, rather than a rendering issue.
The syntax error does not show up on Internet Explorer 8. It does, however, show up on Internet Explorer 9 (one of the platform previews).


PhistucK



PhistucK

unread,
Jan 28, 2011, 7:35:23 AM1/28/11
to v8-u...@googlegroups.com, Lasse R.H. Nielsen
Thank you for the very quick reply!

Got it, I think.. well, not entirely. Which is why I am asking regarding the second code (without the semicolon) I posted in the original message.
Can you, please, clarify the behavior of the second code I posted?



PhistucK

PhistucK

unread,
Jan 28, 2011, 7:39:13 AM1/28/11
to v8-u...@googlegroups.com
I added HTML code because the syntax error does not occur on Internet Explorer 8 (for example) and it is easier to test when you have the entire code ready.

So you are basically saying that "if" and "else" do not count as two statements, right?

I guess I am confused, because I recently found out you can actually run this code without any error -
if (some_condition)
 some_function();
else
 some_other_function();

PhistucK



2011/1/28 Mikael Helbo Kjær <m...@designtech.dk>
--

Lasse R.H. Nielsen

unread,
Jan 28, 2011, 8:22:58 AM1/28/11
to v8-u...@googlegroups.com
If and else are not two statements. It's two different forms of the if-statement.

The if statement is a compound statement, meaning that it can contain other statements as parts of itself. Those statements are either just the mandatory then-branch, or the then-branch and an else-branch. The if-statement still counts as one statement itself.

The code:

  if (some_condition)
   some_function();
  else
   some_other_function();
is an example of this. It has two branches, each being exactly one statement (in this case ExpressionStatements, which are an Expression followed by a semicolon). The presence of the else-branch is marked by the first statement being *immediately* followed by the keyword "else" and then the else-branch statement.

A block statement is another compound statement. It can contain an arbitrary number of other statements. So, 
  if (condition)
    { x = 2; y = 4; }
  else 
    some_function();
is also valid. The then-branch is a single statement, which is a statement block containing two other statements. It's traditionally written
 if (condition) {
   x = 2;
   y = 4;
 } else {
   some_function();
 }
but the "{"'s are not part of the if-statment syntax, but part of the block-statement syntax.

With that in mind, you have code on the form:
  if (c1) if (c2) s1 else s2

In this case, the "else" binds to the closest if, so it's equivalent to:
  if (c1) { if (c2) s1 else s2 }
and not
  if (c1) { if (c2) s1 } else s2 
which is why you get "a is not o".

The syntax for if-statements comes from the C language (or possibly from the precursor B). It's not specific to JavaScript.

Best of luck
/Lasse

PhistucK

unread,
Jan 28, 2011, 8:43:55 AM1/28/11
to v8-u...@googlegroups.com, Lasse R.H. Nielsen
This makes everything much clearer.
Thank you very much for your time.

PhistucK
Reply all
Reply to author
Forward
0 new messages