Streamlining the creation of JavaScript/ECMAScript array literals

Currently in the process of developing a JavaScript/ECMAScript 5.1 parser using JavaCC and encountering challenges with the ArrayLiteral production.

ArrayLiteral :
    [ Elision_opt ]
    [ ElementList ]
    [ ElementList , Elision_opt ]

ElementList :
    Elision_opt AssignmentExpression
    ElementList , Elision_opt AssignmentExpression

Elision :
    ,
    Elision ,

Three questions will be addressed individually:


An attempt was made to simplify/rewrite the ArrayLiteral production as shown below (pseudo-grammar):

ArrayLiteral:
    "[" ("," | AssignmentExpression ",") * AssignmentExpression ? "]"

Question 1: Is this rewriting accurate?

Other inquiries include:

  • LOOKAHEADs for the JavaScript/ECMAScript array literal production
  • How to implement a negative LOOKAHEAD check for a token in JavaCC?

Answer №1

Acknowledged, that effectively captures the grammar as presented.

Yet, a more optimal revision would read:

"[" AssignmentExpression ? ( "," AssignmentExpression ? ) * "]"

since the initial rewriting in the original post is not LL(1) -- discerning between possibilities requires parsing the entirety of AssignmentExpression-- whereas this revised version allows identifying the appropriate alternative simply by examining the first token.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

javascript get the current date and year

Examining the javascript code below: <script type="text/javascript"> $(function () { var currentDateTime = new Date(); var oneYear = new Date(); oneYear.setYear(oneYear.getYear() + 1); alert(currentDateTime + "_" ...

Extract the array of numbers from the JSON file

My task is to extract an array of numbers from JSON files. I need to handle files that contain only arrays of numbers or one level deeper (referred to as direct arrays). These files may also include other data types such as strings or booleans. The challe ...

Removing a record from a database using ASP.NET MVC 5

Looking for some insight on the behavior of my JavaScript and Action in the Controller. Below are the current code snippets: Index.chtml @model IEnumerable<WebSensoryMvc.Models.SessionData> @{ ViewBag.Title = "Index"; Layou ...

Iterating through each loop and storing the values in an array

After researching various topics related to the issue, I found that the solutions provided are not working for me. It seems like there is a logic problem in my code. It's quite simple - I have two arrays (one containing three strings and the other con ...

Unexpected issue with concatenating JavaScript files in AngularJS using Grunt

example: { src: [ 'app/modules/example/js/example-app.js', 'app/modules/example/js/services/example-service.js', 'app/modules/example/js/controllers/example-enter-qty.js& ...

I'm having trouble locating the module "script!foundation-sites/dist/foundation.min.js on Heroic."

This is the content of my webpack.config.js file: var webpack = require('webpack'); var path = require('path'); process.env.NODE_ENV = process.env.NODE_ENV || 'development'; module.exports = { entry: [ 'script!jque ...

Implement your own styling with i18next

There are two namespaces I am working with: The first namespace is global and contains business-related glossary terms The second namespace is specific to a certain page // Global Namespace { "amendment": "amendment" } // Page Name ...

JavaScript: Dynamically load a script when a particular <a> element is in an active state

<script> function DisplayTag(){ var q1=document.getElementById( 'ctl00_ContentPlaceHolder1_ctl00_ctl00_Showcase' ).childNodes[1].innerHTML; var counter1=0; function executeScript(q1,counter1){ q1= document.getElementById( 'ctl00_Co ...

"Learn how to easily send media files stored on your local server via WhatsApp using Twilio with Node.js and Express

Whenever I attempt to send the PDF file created through WhatsApp, an error pops up preventing me from viewing it. easyinvoice.createInvoice(data, function(result) { //The response will contain a base64 encoded PDF file ...

After updating the INNERHTML, the NAV tag content is no longer functional

I am facing an issue with replacing the contents of a NAV tag that contains UL list items. The original HTML within the NAV tag works perfectly fine, but when I replace it with my own HTML - which matches the original word for word - the dropdown functiona ...

Parsing extremely large JSON data from a remote resource using NodeJS

I am facing a challenge with handling a very large JSON file (150k lines, ~3mb) obtained from an external source within my NodeJS application. Currently, I have been fetching the file using an AJAX call, parsing it, and storing it in cache: var options = ...

Is it unorthodox to utilize constructor instances as prototypes in "WebGL - Up and Running"?

In the book "WebGL - Up and Running," a unique custom geometry is created in a rather straightforward manner. However, what caught my attention was the penultimate line of code. Here's how it looked: Saturn.Rings = function ( innerRadius, outerRadius ...

"Learn how to calculate the total value of a dynamic text box within an ng-repeat loop

I am currently working on a form that dynamically generates number input fields within an ng-repeat loop. Users have the ability to add as many fields as they want by clicking on the "add" button. I now need to calculate the total sum of all values enter ...

Is there a way to dim and deactivate a button after it has been clicked once?

Hello, I am attempting to disable and grey out a button after it has been clicked once in order to prevent the user from clicking it again until they refresh the page. I hope my question is clear enough and that you can understand me. Below is the HTML cod ...

launch a website independently of the original source code

I recently completed a website project using PHP. I'm interested in deploying it without sharing the source code. Is this something that can be done with PHP? Is there a way to convert my website's code into an intermediate form before deploymen ...

Using AngularJS to add an element to an array based on a specified condition

dashboardCtrl Data app.controller('dashboardCtrl', ['$scope','$rootScope', function ($scope, $rootScope) { //Color $scope.Color = [{ colorname: "Red", number: "(3)" }, { colorname: "Brown ...

Is there a way to select only a single line from an HTML document?

Is there a way to extract just one specific line from an HTML file? Let's say we have the following file: <!DOCTYPE html> <html> <head></head> <body> This is a test string. This is another test st ...

Tips for sending functions from client to server in Node.js

I'm working with this code snippet: const http = require('http'); const fs = require('fs'); const handleRequest = (request, response) => { response.writeHead(200, { 'Content-Type': 'text/html' ...

Is there a way to extract a section of a Java XML org.w3c.dom.Document?

I need help with manipulating an XML org.w3c.dom.Document object. Here is a sample structure: <A> <B> <C/> <D/> <E/> <F/> </B> <G> <H/> <H/> <J/> ...

Give a class to the element contained within an anchor tag

One way to add a class to the <a>-tag is through this line of code. $("a[href*='" + location.pathname + "']").addClass("active"); However, I am looking to add the class to an li element inside the <a>-tag. What would be the best ap ...