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
  #1  
Old   
Csaba Gabor
 
Posts: n/a

Default Determining the last statement exercise - 11-03-2009 , 10:21 AM






Suppose you have some javascript statements in a string.
Can you determine whether the entire string is syntactically
valid, and if so, the starting position of the last statement?
In other words,

function lastStatementPos(code) {
// returns the starting position within code of the last
// javascript statement, and -1 if code is not syntactiaclly
// valid.


This came up in a different context today, and I thought
it would make an interesting exercise.

Csaba Gabor from Vienna

Reply With Quote
  #2  
Old   
SAM
 
Posts: n/a

Default Re: Determining the last statement exercise - 11-03-2009 , 10:28 AM






Le 11/3/09 4:21 PM, Csaba Gabor a écrit :
Quote:
Suppose you have some javascript statements in a string.
can you give an example of that ?

Quote:
Can you determine whether the entire string is syntactically
valid, and if so, the starting position of the last statement?
In other words,

function lastStatementPos(code) {
// returns the starting position within code of the last
// javascript statement, and -1 if code is not syntactiaclly
// valid.


This came up in a different context today, and I thought
it would make an interesting exercise.
eval(code); ?


--
sm

Reply With Quote
  #3  
Old   
Evertjan.
 
Posts: n/a

Default Re: Determining the last statement exercise - 11-03-2009 , 10:33 AM



Csaba Gabor wrote on 03 nov 2009 in comp.lang.javascript:

Quote:
Suppose you have some javascript statements in a string.
Can you determine whether the entire string is syntactically
valid, and if so, the starting position of the last statement?
In other words,

function lastStatementPos(code) {
// returns the starting position within code of the last
// javascript statement, and -1 if code is not syntactiaclly
// valid.


This came up in a different context today, and I thought
it would make an interesting exercise.
A string like?

str = "alert(11);{alert('alert(11); is correct Javascript');};"

Would that be usefull?
I don't think it would.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Reply With Quote
  #4  
Old   
Richard Cornford
 
Posts: n/a

Default Re: Determining the last statement exercise - 11-03-2009 , 10:56 AM



On Nov 3, 3:21 pm, Csaba Gabor wrote:
Quote:
Suppose you have some javascript statements in a string.
Can you determine whether the entire string is syntactically
valid, and if so, the starting position of the last statement?
It would be possible to write a javascript tokeniser/parser with
javascript, and have that determine the syntactic correctness of the
source and expose the contained statements in a way that would allow
you to determine the starting position of the last.

Quote:
In other words,

function lastStatementPos(code) {
// returns the starting position within code of the last
// javascript statement, and -1 if code is not syntactiaclly
// valid.

This came up in a different context today, and I thought
it would make an interesting exercise.
Maybe a bit too big a task to be considered an 'exercise'. Quickest
results would probably start with something that was already doing
most of the job like Narcissus or JSLint.

<URL: http://mxr.mozilla.org/mozilla/source/js/narcissus/ >

Richard.

Reply With Quote
  #5  
Old   
Johannes Baagoe
 
Posts: n/a

Default Re: Determining the last statement exercise - 11-03-2009 , 11:19 AM



Csaba Gabor :

Quote:
Suppose you have some javascript statements in a string. Can you
determine whether the entire string is syntactically valid, and if so,
the starting position of the last statement? In other words,

function lastStatementPos(code) {
// returns the starting position within code of the last // javascript
statement, and -1 if code is not syntactiaclly // valid.
What should lastStatementPos('{a = 1; b = 2}') return? 0 or 8?

--
Johannes

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

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



Csaba Gabor wrote:

Quote:
Suppose you have some javascript statements in a string.
Define "javascript".

Quote:
Can you determine whether the entire string is syntactically
valid,
Depends. Short of writing an ECMAScript parser, which would only be able
to cover specified syntax rules, where available you could try-catch the
SyntaxError that the eval() of the specific implementation would throw if
the code would not conform to its syntax rules. However, AFAICS that can
become quite a nuisance in Konqueror 4.3.2 as syntax errors cannot be
catched there (syntax errors caused Konqueror 3.5.x to crash, therefore I
had to modify the ECMAScript Support Matrix.)

Quote:
and if so, the starting position of the last statement?
Depends. What is, in your book, considered "the last statement" in

if (x)
y;
else
z;

(we are looking at the following production here:

/IfStatement/ :
if (/Expression/) /Statement/ else /Statement/

see ES3F, 12.5)


PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm> (404-comp.)

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

Default Re: Determining the last statement exercise - 11-03-2009 , 07:42 PM



On Nov 3, 4:33*pm, "Evertjan." <exjxw.hannivo... (AT) interxnl (DOT) net> wrote:
Quote:
Csaba *Gabor wrote on 03 nov 2009 in comp.lang.javascript:

Suppose you have some javascript statements in a string.
Can you determine whether the entire string is syntactically
valid, and if so, the starting position of the last statement?
In other words,

function lastStatementPos(code) {
* // returns the starting position within code of the last
* // javascript statement, and -1 if code is not syntactiaclly
* // valid.

This came up in a different context today, and I thought
it would make an interesting exercise.

A string like?

str = "alert(11);{alert('alert(11); is correct Javascript');};"

Would that be usefull?
I don't think it would.
On Nov 3, 4:33 pm, "Evertjan." <exjxw.hannivo... (AT) interxnl (DOT) net> wrote:
Quote:
Csaba Gabor wrote on 03 nov 2009 in comp.lang.javascript:

Suppose you have some javascript statements in a string.
Can you determine whether the entire string is syntactically
valid, and if so, the starting position of the last statement?
In other words,

function lastStatementPos(code) {
// returns the starting position within code of the last
// javascript statement, and -1 if code is not syntactiaclly
// valid.
}

This came up in a different context today, and I thought
it would make an interesting exercise.

A string like?

str = "alert(11);{alert('alert(11); is correct Javascript');};"

Would that be usefull?
I don't think it would.
You've made a good example, and yes it can be useful.
But I think I better give context to my question
and recast it, especially in light of the fact that my
proposed solution does not work across browsers.

The question arose in the following context: The
user is asked to enter some javascript statements
to affect a value. I want to ensure that all the
statements are syntactically correct, AND to insert
a return before the last statement, if it doesn't
start with return, but does make syntactic sense.

The validation is fairly straightforward:

function syntax_check(code) {
// returns false is code is not syntactically OK
// returns browser's interpretation of the code if it's OK
try {
var f = new Function(code);
return f.toString(); }
catch (err) { return false; } // syntax error
}


On firefox, the returned string is cleaned of all
comments, and is recast into a 'standard form'.
I was thinking that if the penultimate line of
this returned string did not consist of "}",
then a return could be prefixed, if it wasn't
already there. Any exceptions to this?

However, with IE the returned string is not gussied up
into standard form and is left pretty much as is.

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.


Some examples:
code = "'Fred'" => return 'Fred';

code = "var i=7; i *= 9"; =>
var i=7;
return i *= 9;

code = "x='word'\nx+='s' // making a plural\n" +
" return x /* pluralizing a word */";
=> no change

Evertjan's code =>
alert(11);
return alert('alert(11); is correct Javascript');

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


Two problem cases:
code = "var y=6;\n y*= 2 // difficult; hard case";
code = "var y=6;\n y*= 2; // difficult; hard case";

Reply With Quote
  #8  
Old   
Evertjan.
 
Posts: n/a

Default Re: Determining the last statement exercise - 11-04-2009 , 10:33 AM



Csaba Gabor wrote on 04 nov 2009 in comp.lang.javascript:

Quote:
Evertjan's code =
alert(11);
return alert('alert(11); is correct Javascript');

Which is utter nonsense as

1 alert() does not have a return value

2 return is only sensible in a function

Quote:
The question arose in the following context:
The user is asked to enter some javascript statements
to affect a value.
Sorry, that is not only not very useful, but it could even be dangerous.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

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

Default Re: Determining the last statement exercise - 11-04-2009 , 01:18 PM



Csaba Gabor wrote:

Quote:
The validation is fairly straightforward:
AISB, it is not.

Quote:
function syntax_check(code) {
// returns false is code is not syntactically OK
Based on which syntax rules? ECMAScript's, JavaScript's, JScripts or
others'?

Quote:
// returns browser's interpretation of the code if it's OK
try {
And if this already constitutes a syntax error, the whole thing breaks.

Quote:
var f = new Function(code);
And if the Function constructor is not supported, the whole thing breaks.

Quote:
return f.toString(); }
catch (err) { return false; } // syntax error
}
Does not work reliably. You have failed to realize, among other things,
that syntax is context-sensitive. For example, `return' is allowed in a
function, it is not allowed elsewhere.


PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann

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

Default Re: Determining the last statement exercise - 11-04-2009 , 01:36 PM



Thomas 'PointedEars' Lahn wrote:
Quote:
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

To OP: I am wondering if you are looking for a regexp solution
recreating parser rules - or some creative eval or eval-like approach.
Or it's up to respondents to find the most effective way?

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.