HighDots Forums  

Determining the last statement exercise

Javascript JavaScript language (comp.lang.javascript)


Discuss Determining the last statement exercise in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #11  
Old   
Thomas 'PointedEars' Lahn
 
Posts: n/a

Default Re: Determining the last statement exercise - 11-04-2009 , 02:27 PM






VK wrote:

Quote:
Thomas 'PointedEars' Lahn wrote:
Based on which syntax rules? ECMAScript's, JavaScript's, JScripts or
others'?

And if the Function constructor is not supported, the whole thing breaks.

By trying get nasty do not get dorky
Please shut up until you know what you are talking about (suppose
that is ever going to happen). And lose the bogus smileys.


PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16

Reply With Quote
  #12  
Old   
John G Harris
 
Posts: n/a

Default Re: Determining the last statement exercise - 11-04-2009 , 02:35 PM






On Tue, 3 Nov 2009 at 16:42:34, in comp.lang.javascript, Csaba Gabor
wrote:

<snip>
Quote:
code = '{a = 1; b = 2}' =
a = 1;
return b = 2;
snip

b=2 is not the last statement. The last statement is the one that ends
with } .

You do know that a block statement, { ... }, is a statement, don't you ?

John
--
John Harris

Reply With Quote
  #13  
Old   
VK
 
Posts: n/a

Default Re: Determining the last statement exercise - 11-04-2009 , 06:13 PM



Csaba Gabor wrote:
Quote:
So the question, exercise, or problem is: given
a function which will tell you whether or not you
have a syntactically correct javascript, can you
semi reasonably isolate the last statement to
determine whether it should be prefixed with a
return, and to do so in such case.
No, it is not possible because it would violate Rice's Theorem and
respectively would violate the halting problem which is known to be
undecidable. For the full description see http://en.wikipedia.org/wiki/Rice%27s_theorem
in a very simplified yet useful form it states that "Any question
about what an arbitrary script does with an arbitrary input is
undecidable, unless it is trivial".

For "semi reasonably" practical use as a helper for beginners it can
be a script that
1) takes the string input
2) gets guarantees that it is a function body with curly brackets
removed
3) gets guarantees that there are not inner functions in the body
( steps 2 and 3 assure that any closing curly bracket is closing an
inner block of statements and nothing else )
4) find the first line break going from the end that followed by non-
white-space chars
5) if it's return whatever then OK, exit, else go to 6)
6) if the previous line contains "return \s*\n" then remove \s*\n, OK,
exit
(often beginners' mistake of
return
myReturnValue
else go to 7)
7) if 4) contains "}" the return error, the problem is not decidable
8) if 4) contains something else than change to "return something
else", OK, exit.

That is the physical maximum you can get, the actual coding and tune
up is skipped.

Reply With Quote
  #14  
Old   
Csaba Gabor
 
Posts: n/a

Default Re: Determining the last statement exercise - 11-05-2009 , 05:35 AM



On Nov 4, 8:35*pm, John G Harris <j... (AT) nospam (DOT) demon.co.uk> wrote:
Quote:
On Tue, 3 Nov 2009 at 16:42:34, in comp.lang.javascript, Csaba Gabor
wrote:

* <snip>>code = '{a = 1; b = 2}' =
*a = 1;
*return b = 2;

* <snip

b=2 is not the last statement. The last statement is the one that ends
with } .

You do know that a block statement, { ... }, is a statement, don't you ?
A rhetorical question?
Nevertheless, since it has come up a few times in this
thread... This exercise is about determining the last
statement and its location (in possibly transformed code),
and the method is more interesting to me than the exact
definition used to get at an answer. What I am saying
is that I would be just as happy with either the original
{a = 1; b = 2} in the example above being returned, or
the b = 2 portion that FF indicates.

Reply With Quote
  #15  
Old   
Csaba Gabor
 
Posts: n/a

Default Re: Determining the last statement exercise - 11-05-2009 , 06:39 AM



On Nov 5, 12:13*am, VK <schools_r... (AT) yahoo (DOT) com> wrote:
Quote:
Csaba *Gabor wrote:
So the question, exercise, or problem is: given
a function which will tell you whether or not you
have a syntactically correct javascript, can you
semi reasonably isolate the last statement to
determine whether it should be prefixed with a
return, and to do so in such case.

No, it is not possible because it would violate Rice's Theorem and
respectively would violate the halting problem which is known to be
undecidable. For the full description seehttp://en.wikipedia.org/wiki/Rice%27s_theorem
in a very simplified yet useful form it states that "Any question
about what an arbitrary script does with an arbitrary input is
undecidable, unless it is trivial".
An interesting approach VK. I am familiar with Rice's Theorem,
but let me show you that it does not apply, and then perhaps
you will be able to see WHY it does not apply.

Let's consider this same question in the PHP world,
where it is easier to handle. In PHP land, all statements
except the last one must end with ; or }.

Thus, ignoring the final character in the code, find
the prior occurrence of either ; or }. Strip the
string from the next character to the end and syntax
check it. If it doesn't pass, keep looking back for
the next prior occurrence of ; or }.

If instead, the syntax check passes, then the final
character is either the end of the prior statement or
at the end of a // comment. The latter is easily
checked by syntax checking the code with ' x y' affixed.
If at the end of a // comment, keep looking back
for the next prior occurrence of ; or }. Otherwise,
we're done.

So, since I've demonstrated a solution to the problem
(in PHP land), it's clear that Rice's Theorem does
not hold here. My question to you is why not?

Javascript syntax is more complicated, however, and
the question remains whether the approach outlined
above is viable in JS land. In particular, how to
differentiate between (arbitrarily messy versions of):

while(truth)
x+="*"

and

whole(truth)
x+="*"

Csaba

Reply With Quote
  #16  
Old   
John G Harris
 
Posts: n/a

Default Re: Determining the last statement exercise - 11-05-2009 , 09:04 AM



On Wed, 4 Nov 2009 at 15:13:36, in comp.lang.javascript, VK wrote:
Quote:
Csaba Gabor wrote:
So the question, exercise, or problem is: given
a function which will tell you whether or not you
have a syntactically correct javascript, can you
semi reasonably isolate the last statement to
determine whether it should be prefixed with a
return, and to do so in such case.

No, it is not possible because it would violate Rice's Theorem and
respectively would violate the halting problem which is known to be
undecidable. For the full description see http://en.wikipedia.org/wiki/
Rice%27s_theorem
in a very simplified yet useful form it states that "Any question
about what an arbitrary script does with an arbitrary input is
undecidable, unless it is trivial".
snip

He isn't trying to check that the code does a particular job, so Rice's
theorem isn't relevant. Nor is he trying to check that the code will
terminate on all inputs, so Turing's halting theorem isn't relevant.

He wants to find the start and end of each statement in the code.
Javascript parsers are able to do that, as is well known.

John
--
John Harris

Reply With Quote
  #17  
Old   
John G Harris
 
Posts: n/a

Default Re: Determining the last statement exercise - 11-05-2009 , 09:24 AM



On Thu, 5 Nov 2009 at 02:35:23, in comp.lang.javascript, Csaba Gabor
wrote:
Quote:
On Nov 4, 8:35*pm, John G Harris <j... (AT) nospam (DOT) demon.co.uk> wrote:
On Tue, 3 Nov 2009 at 16:42:34, in comp.lang.javascript, Csaba Gabor
wrote:

* <snip>>code = '{a = 1; b = 2}' =
*a = 1;
*return b = 2;

* <snip

b=2 is not the last statement. The last statement is the one that ends
with } .

You do know that a block statement, { ... }, is a statement, don't you ?

A rhetorical question?
Nevertheless, since it has come up a few times in this
thread... This exercise is about determining the last
statement and its location (in possibly transformed code),
and the method is more interesting to me than the exact
definition used to get at an answer. What I am saying
is that I would be just as happy with either the original
{a = 1; b = 2} in the example above being returned, or
the b = 2 portion that FF indicates.
The only kind of statement you can put a 'return' in front of is an
expression statement. E.g a=b; becomes return a=b;
So you have to worry about this piece of code :

if ((new Date()).getDay() == 0) a = 23; else a = 42;

Which value do you want to return ?

I can't help thinking that you need to spend more time deciding what you
want, and also whether it's really needed.

John
--
John Harris

Reply With Quote
  #18  
Old   
VK
 
Posts: n/a

Default Re: Determining the last statement exercise - 11-05-2009 , 01:15 PM



Csaba Gabor wrote:
Quote:
Let's consider this same question in the PHP world,
where it is easier to handle. In PHP land, all statements
except the last one must end with ; or }.
It doesn't matter because Rice's Theorem doesn't depend on a formal
language syntax. The only thing that matters is if it's a general
purpose higher level programming language (skipping for now on exact
formal definitions of these terms). Perl also requires ; after each
statement, so does C++ if I recall properly - and their cases for
Rice's Theorem have been proven 2 and 1 year ago respectively. No one
bothered to make it for PHP yet simply because after the generalized
prove is made it is not so interesting anymore.

Quote:
Thus, ignoring the final character in the code, find
the prior occurrence of either ; or }. Strip the
string from the next character to the end and syntax
check it. If it doesn't pass, keep looking back for
the next prior occurrence of ; or }.
I gave the possible block schema in my previous answer and
demonstrated that for the most trivial cases it is well possible to
write a "return adder". The only extension I would still suggest is to
add correction for cases like:
return
Oops_ReturnOnTheNextLine;

For a general case finding the right place for the return statement
means to algorithmically decide for an arbitrary program with an
arbitrary input if it has a return point and then to find that
intended return point of it, so, unlike Harris claimed, it is
necessary to prove that the halting problem is decidable and
consecutively to resolve the Entscheidungsproblem. Another option is
to write a program that successfully passes the Turing test so having
all qualities of a free-will human identity.
I guess that either of these three tasks from above is way beyond the
humble frames of clj. The future Nobel laureate should post such
solution in a serious scientific preprint journals right away

Reply With Quote
  #19  
Old   
Thomas 'PointedEars' Lahn
 
Posts: n/a

Default Re: Determining the last statement exercise - 11-05-2009 , 01:59 PM



VK wrote:

Quote:
Csaba Gabor wrote:
So the question, exercise, or problem is: given
a function which will tell you whether or not you
have a syntactically correct javascript, can you
semi reasonably isolate the last statement to
determine whether it should be prefixed with a
return, and to do so in such case.

No, it is not possible because it would violate Rice's Theorem and
respectively would violate the halting problem which is known to be
undecidable. For the full description see
http://en.wikipedia.org/wiki/Rice%27s_theorem in a very simplified yet
useful form it states that "Any question about what an arbitrary script
does with an arbitrary input is undecidable, unless it is trivial".
Neither Rice's theorem nor the halting problem applies here.

If they did, no compiler could throw a compile error, and in particular no
script engine could throw a syntax error.

This is neither about deciding whether the result of an algorithm meets a
particular value (as it suffices to get the value) nor whether this
algorithm holds (as it always holds, either with success or failure). It is
simply about whether an input can be produced by a (context-free) grammar,
and that is a decidable problem.

As usual, you do not know what you are talking about.


PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f806at$ail$1$8300dec7 (AT) news (DOT) demon.co.uk>

Reply With Quote
  #20  
Old   
Thomas 'PointedEars' Lahn
 
Posts: n/a

Default Re: Determining the last statement exercise - 11-05-2009 , 02:06 PM



Csaba Gabor wrote:

Quote:
John G Harris wrote:
Csaba Gabor wrote:

snip>>code = '{a = 1; b = 2}' =
a = 1;
return b = 2;

snip

b=2 is not the last statement. The last statement is the one that ends
with } .

You do know that a block statement, { ... }, is a statement, don't you ?

A rhetorical question?
Apparently not.

Quote:
Nevertheless, since it has come up a few times in this
thread... This exercise is about determining the last
statement and its location (in possibly transformed code),
That would be the /Block/ statement, then.

Quote:
and the method is more interesting to me than the exact
definition used to get at an answer.
But the exact definition is a prerequisite for finding an answer that you
would consider correct.

Quote:
What I am saying is that I would be just as happy with either the original
{a = 1; b = 2} in the example above being returned, or the b = 2 portion
that FF indicates.
That would be the last statement *that is executed*, provided
the execution of the statement does not depend on a condition.
As you can see, your oversimplification is not helpful.


PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.