Creating Arrays with Jison

I am currently attempting to enhance my programming language to support arrays, but I am encountering difficulties.

Array
  : '[' Expr ("," Expr)* ']'
     {{ $$ = ['ArrayList', $1]; }}
  | '[' Expr ']'
     {{ $$ = ['Array', $2]; }}
  | '[' ']'
     {{ $$ = ['Empty']; }}
  ;

However, when trying to parse "[1,2,3,4]," Jison notifies me that it was expecting "]" instead of ",". Any suggestions?

Answer №1

The recursion is not interpreted or rejected. To make it work, you need to split it into two elements:

Array
  : '[' Element ']'
     {{ $$ = ['ArrayList', $2]; }}
  ;

Element
  : Element "," Expr
     {{ $$ = $1 + ',' + $3; }}
  | Expr
     {{ $$ = $1; }};

When implemented correctly, this code returns the desired Array:

["ArrayList","1,2,3,4"]

Answer №2

jison does not support EBNF syntax. (It seems to neither accept nor reject it.) Therefore, your rule:

Array
  : '[' Expr ("," Expr)* ']'

will be treated as if it were:

Array
  : '[' Expr "," Expr ']'

To resolve this issue, you should introduce an ExprList production:

Array   : '[' ExprList ']'
        | '[' ']'
        ;
ExprList: Expr
        | ExprList ',' Expr
        ;

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

Utilize Jquery to calculate the total sum of values associated with a particular key in a JSON object based on input

I am facing an issue with my script where I am trying to sum up the clientPrice keys in a JSON object assigned to a form text element. Here is what I have: <input id="clientList" type="hidden" value="[{"clientID":"1","clientType":"0","clientPrice":"450 ...

Comparing obj.hasOwnProperty(key) with directly accessing obj[key]

Consider the scenario where I need to determine if a property exists within an Object. A comparison between two methods caught my attention: if(object.hasOwnProperty(key)) { /* perform this action */ } OR if(object[key]) { /* perform this action */ ...

Utilizing the HTML5 Pattern Attribute in Conjunction with Websockets

Being completely new to web programming, I am struggling to understand why the pattern attribute in the text field below is not validating properly due to my javascript. <form id="aForm"> <input type="text" pattern="^[ a-zA-Z0-9,#.-]+$" id="a ...

Exploring the differences between comma-separated lists in Classic ASP

I am trying to compare two lists of numbers separated by commas: 36,189,47,183,65,50 65,50,189,47 I am looking for a way to identify and retrieve any values that are present in the first list but not in the second list using classic ASP. It should be no ...

Deciphering Parameters from a URL using Angular

My goal is to track specific metrics in Google Analytics by extracting certain parameters, removing sensitive information, and sending them off as a comma-separated string. For example: this=me&that=you should be transmitted to GA as: this,that I h ...

It is impossible for Javascript to access an input element within a gridview

I have developed an asp.net page that allows a site administrator to select a user as the 'systems chair'. The page displays users in a gridview and includes a column of radio buttons to indicate who the current chair is or to change the assigned ...

Synchronizing Data between Elasticsearch and MongoDB to Retrieve Query Results

I'm currently working on a project that involves integrating Elasticsearch with our existing Mongo data source. The team has decided not to use river due to lack of support, and instead, they plan on running a cron job to update all the records in Ela ...

What is the best way to inject a service instance into the implementation of an abstract method?

In my Angular application, I have a service that extends an abstract class and implements an abstract method. @Injectable({ providedIn: 'root', }) export class ClassB extends ClassA { constructor( private service : ExampleService) { s ...

Navigating in React: A Guide to Switching Components

I'm currently working on a landing page project using React. The goal is to have a consistent navigation bar across all sections of the website, with only the active state changing based on user interaction. Within my App.js file, I've implement ...

Pulling data from a MySQL database using AJAX and PHP to convert it into a JavaScript variable, resulting in

I am attempting to retrieve MySQL data using PHP, convert it to JSON, and then pass it into my JS variables for Chart.js. The JSON generated by PHP appears correct. However, when trying to access the data within my variables, the console is displaying them ...

Using jQuery to implement tiered radio buttons styled with Bootstrap

I am currently in the process of learning jQuery, so please excuse my lack of knowledge in this area. I am working on a web form using Bootstrap that involves tiered radio buttons. When the user selects "Yes" for the first option, it should display the ne ...

Replace all existing content on the webpage with an exciting Unity game upon clicking

In an interesting experiment, I am trying to hide a secret href that, once clicked, has the power to wipe out everything on the page, leaving it temporarily blank before replacing it with a captivating Unity game that takes over the entire page. Let me sh ...

I am encountering an issue with adding a new row in a dynamic form in Yii2. The JavaScript does not seem to

I encountered a challenge involving handling dynamic input in Yii2, and after researching various options, I opted for this extension called wbraganca. Below is the code snippet I implemented: <?php $form = ActiveForm::begin(['id' => &apo ...

Stopping data-ajax attributes from running

Here is a link I have along with its corresponding fiddle: <a class="update noClick" data-ajax="true" data-ajax-mode="replace" data-ajax-update="#vote11" href="/Tutors/RateNegative/11" onclick="return window.confirm('Are you sure?');">qwer ...

Navigating through undefined state while iterating through an array of objects extracted from an API in React

When the component mounts, API data is fetched and the state is initialized in the following form: sectionData: { course: null, section_id: null, semester: null, number: null, seats: null, meetings: [], open_seats: null, waitlist: null, ...

Coordinate the timing of CSS animation with the loading of the page

Despite the abundance of similar questions, none have quite addressed my specific query. I've created a preloader using CSS animation and I want it to synchronize perfectly with the page load. While I can trigger the animation on page load, I'm s ...

Encountered a React 16 error: Unexpected TypeError stating that this.state.userInput.map is not a valid

I am working on a simple React app where a user can input text and I want to display each character as a list. Here is the progress I have made so far: App Components import React, { Component } from 'react'; import './App.css ...

Using the power of AJAX, retrieve data from a PHP script and showcase the response within

Within my HTML file, I have implemented a feature that allows users to input a value. To validate this input, I created a PHP script that checks if the entered value exists in the database. The script returns the following JSON response: {"active":true} ...

What is the method for selecting specific indices and then matching them to different indices?

There is a need to execute an API call with specific parameters. Here is an example: const apiFunc = (someId, anotherId) => apiCall(someId, anotherId) However, when the data is stored in an array named someArr, it appears like this: [1, 'someId& ...

Creating a cascading dropdown list box using Java servlets, JavaScript, JSP, a database, and Ajax, all without relying on Jquery or Json

My experience with Ajax is limited, but I am eager to utilize it to create a cascading drop down list box for my project. While I have found resources online that demonstrate the use of Ajax with JSON and JQuery, I am specifically looking to implement Ajax ...