1

I have some nested if statements with the following syntax:

// Comment for condition 1
if(condition1
,expression1
    ,
    // Comment for condition 2
    if(condition2
        ,expression2
        ,else2))

The if statements do not conform to JavaScript syntax. They are written in a language which is "not publicly known, like a mix of VBscript and JavaScript."

Now I would like to parse these if statements to see if they are syntactically correct or not. How could I do that?

6
  • 1
    FYI, this shouldn't be a community wiki, see the FAQ Commented Jan 5, 2010 at 14:30
  • 2
    Why CW? Surely this doesn't qualify. Commented Jan 5, 2010 at 14:31
  • So then, you're looking to use JavaScript to write a parser for the statement? What is the name of the language the if statements are written in? Commented Jan 5, 2010 at 14:35
  • Unfortunately this a "special language", not publicly known, like a mix of vbscript an javascript ... (Fortunately) I do not need to build a parser for the whole language, just for the nested-if-statements ... Commented Jan 5, 2010 at 14:39
  • Actually, the FAQ located under stackoverflow.com/faq doesn't explain what "community wiki" means - it merely mentions the term three times and assumes understanding of it. Commented Jan 5, 2010 at 15:00

4 Answers 4

1

Source code for an existing JS parser can be found here: JSLint Source Code

Aside from that, you would need to build a language parser, possibly using these tools.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your hint, but unfortunately the if-statements are not written in javascript-code, so I cannot use the javascript-paser ... Best regards, Stefan
Ack. You'll need to build a language parser with JavaScript, then. This winds up being non-trivial, as many people have mentioned.
1

Well, to check whether the basics are correct (correct number of arguments, open/close brackets, number of commas, etc) would not be difficult - you could easily cater for the nested IFs using recursive techniques.

But you would also need to tell us whether you also need to check whether the "condition" and "expression" syntax is correct?

And are commas or brackets, for example going to be legal inside an expression or condition? If so, you'll need to define your rules for string qualification, escape characters, etc, so your parser interprets these as being part of the expression or condition and doesn't confuse them as being control characters.

In its most simple form however, some psuedo-code:

var stmt = "IF(condition,expression,IF(condition,expr2,expr3))"
function checkIfSyntax(stmt){
   var tokens = splitStmt(stmt); // careful to take into account where
                                 // control chars (that is, commas and
                                 // brackets) are qualified, escaped,
                                 // or inside a nested IF()
   checkCondition(tokens.condition); //with whatever rules you have, plus [1]
   checkExpression(tokens.expr1); //with whatever rules, plus [1]
   checkExpression(tokens.expr2); //with whatever rules, plus [1]
   /* [1]: if any of the tokens are nested IF() statements, 
           call checkIfSyntax() from within checkCondition()
           or checkExpression()
           - (this is the recursive bit) */
}

This is just hopefully to get you on the right track. To actually answer your question fully would be to code it for you - I don't think anyone is going to do that. Try to understand the above as well as reading the links others have provided, and hopefully you should get on the right track

Comments

0

Parsing Javascript code to see if it is syntactically correct is not a trivial task.. (actually this is true for most programming code)

You might want to consider actually running a Javascript interpreter to verify the syntax of the code. If you're running in a Java enviroment, you can use the Rhino interpreter to do this.

Maybe specify a bit more in which context you want to do this.

2 Comments

Thanks for your hint; unfortunately the if-statements are not javascript-code, they are a kind of special code ... :(
The task isn't actually to parse Javascript - it's to write a parser in Javascript, to parse a DSL.
0

There is a section in the book 'Beautiful Code' which has a tutorial (with a working parser at the end) on writing a Parser in Javascript:

http://www.amazon.co.uk/gp/product/product-description/0596510047/ref=dp_proddesc_0?ie=UTF8&n=266239&s=books

IMHO: really good book as well - its kinda like the Geek's version of "Comic Relief" or "Band Aid"...[In the sense that I seem to remember some of the proceeds go to charity or something - but basically lots of interesting articles and code snippets from great programmers from all generations and in lots of computing-languages].

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.