ChatGPT and JavaScript parser

Yesterday, I asked ChatGPT to write for me a Python code snippet to parse JavaScript, using the pyparsing library. Nothing to be astonished these days. ChatGPT gave me the following output


Sure! Below is a Python code snippet that uses the `pyparsing`
library to parse JavaScript code:

```python
from pyparsing import Word, alphas, nums, Group, Forward, Suppress, \
    Keyword, OneOrMore, ZeroOrMore

# Define the grammar for JavaScript using pyparsing
identifier = Word(alphas, alphas + nums + "_$")
number = Word(nums)
string = (Suppress('"') + Word(alphas + nums + " ") + Suppress('"')) | \
         (Suppress("'") + Word(alphas + nums + " ") + Suppress("'"))
expression = Forward()
function_call = identifier + Suppress("(") + ZeroOrMore(expression) \
    + Suppress(")")
expression << (identifier | number | string | function_call)
statement = Group(expression + Suppress(";"))
block = Forward()
block << Group(Suppress("{") + ZeroOrMore(statement) + Suppress("}"))
function_def = (Keyword("function") + identifier + Suppress("(") \
    + ZeroOrMore(identifier) + Suppress(")") + block)
js_parser = OneOrMore(function_def | statement)

# Sample JavaScript code
js_code = """
function add(a, b) {
    return a + b;
}

var x = 10;
var y = 20;
var result = add(x, y);
console.log("Result:", result);
"""

# Parsing the JavaScript code
parsed_data = js_parser.parseString(js_code)

# Print the parsed data
print(parsed_data)
```

Please make sure you have the `pyparsing` library installed
before running this code. You can install it using
`pip install pyparsing`. This code snippet defines a basic grammar
for JavaScript using pyparsing and then parses the sample
JavaScript code provided in the `js_code` variable. The parsed
data will be printed as a list of Python objects representing the
parsed elements of the JavaScript code.        

I have taken the liberty to format the long sentences for easy readability.

I took this code and ran it. Initially Python gave me an error for the function parameters, which I found after a bit of trial and error. Then I informed ChatGPT that the code was incorrect and was not handling parameters. It apologized and generated new code.

And the apologies continued for quite some time. After solving the parameter issue, we ran into trouble handling the return statement inside the function definition. Each time ChatGPT gave a different solution, but each time we ran into the same issue - return not identified. Unfortunately, I did not copy each interaction.

Finally, after trying out five to six solutions, I gave up and informed ChatGPT that it was not working out. Once again it generated a piece of code that again did not work. This time I informed ChatGPT that things are not working out and I do not want it to generate more solutions. It replied that parsing JavaScript seems to be trickier that anticipated.

While I have no opinion about 'tricky', given my recent experience with parsing SQL (using the solution provided by ChatGPT, which turned out to be the catalyst I needed), defining the grammar is key. So now I have to tweak the code to make it work for my needs.

#chatgpt #genai #generativeai #pyparsing #python #javascript #sql #grammar

100% agree. I faced same issues with pyhton, php and reactjs. In few cases meta's Llama v2 gave better results

Like
Reply

To view or add a comment, sign in

More articles by Bipin Patwardhan

Others also viewed

Explore content categories