Is there an issue with the precedence of jison rules?

I've been stuck for hours trying to solve what seems like a simple problem but I just can't figure it out :/

I'm working on defining a small javascript-like language in jison. The issue I'm facing is that both the Parameter rule and the AssignStatement rule can start with a CHAR_SEQUENCE, but it always selects the parameter rule. For example, even when the code is a = 5;, it triggers the throw "reached parameter"; so it appears to interpret the a as a Parameter instead of a = 5; as an AssignStatement

Here's the relevant part of my grammar:

Parameter
    : InlineVariable
        { $$ = $1; }
    | CHAR_SEQUENCE
        { throw "reached parameter"; checkUndefined($1, @1); $$ = vars[$1]; }
    ;

InlineVariable
    : NUMBER
        { $$ = new Runtime.Integer(parseInt($1)); }
    | '"' CHAR_SEQUENCE '"'
        { $$ = new Runtime.String($2); }
    | FUNCTION '(' ParameterList ')' Statement
        { $$ = new Container($5); }
    ;

AssignStatement
    : CHAR_SEQUENCE AssignmentOperator Parameter ';'
        {
            $$ = function()
            {
                if((typeof vars[$1] == 'undefined' && $3 instanceof Runtime.Integer) || (vars[$1] instanceof Container && $3 instanceof Runtime.Integer))
                    vars[$1] = new Runtime.Integer(0, $1);
                else if(typeof vars[$1] == 'undefined' || (vars[$1] instanceof Container && !($3 instanceof Container)))
                    vars[$1] = $3.constructor.call();

                $2(vars[$1], vars[$3]);
            }
        }
    | CHAR_SEQUENCE SingleAssignmentOperator ';'
        {
            $$ = function()
            {
                if(typeof vars[$1] == 'undefined')
                    vars[$1] = new Runtime.Integer(0, $1);

                $2(vars[$1]);
            };
        }
    ;

You can find the full grammar at https://gist.github.com/M4GNV5/36c2550946c1a1f6ec91

Is there a solution to this problem? I've already tried using %left, %right, %assoc, and %precedence but it didn't work (or maybe I did something wrong?

Answer №1

Your understanding of the flow control in a bottom-up parser needs some clarification.

In this type of parsing, actions are triggered only after all non-terminals within a production have been successfully reduced, not before.

Take for example the AssignmentStatement production:

AssignStatement
    : CHAR_SEQUENCE AssignmentOperator Parameter ';'

This means that the actions associated with AssignmentStatement can only be performed once the productions for AssignmentOperator and Parameter have been executed. In your specific case, Parameter is responsible for matching the value 5.

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

Conflicting styles arise when using the makeStyles function from Material UI with imported

In working on a React component library using create-react-library, I have incorporated basic components that utilize Material UI components and the material UI hook-based styles pattern. For example (in a simplified form): // LibraryComponent.js const u ...

Combine table cells to improve readability on smaller screens

I currently have a setup designed for larger screens: <table> <thead> <tr> <th>title and image</th> <th>description</th> </tr> </thead> <tbody> ...

The remaining visible portion of a viewport is equivalent to the height of an element

Is there a way to dynamically set a div's height so that it expands from its starting position to the end of its parent div, which is 100% of the viewport minus 20 pixels? Here is an example of how you can achieve this using jQuery: $(document).read ...

Angular ngClass and ngIf directives failing to update upon alterations

In my current Angular project, I am working on a functionality where I need to dynamically change a class based on a variable without having to refresh the page. I have experimented with *ngIf/else and [ngClass] directives, which do work, but unfortunatel ...

Completing the regex properly

When using my editor, I am able to paste a video URL which is then converted by regex into an embed code. The URL in the WYSIWYG-editor looks like this: Once converted, the output HTML appears as: <p>http://emedia.is.ed.ac.uk:8080/JW/wsconfig.xml& ...

Submitting a form automatically in React Native using Redux

Is there a way to automatically submit a Redux Form in React Native based on certain conditions being met? I attempted to do so below, but it resulted in a warning. The documentation provides an example for remote submitting, but it uses HTML form's ...

Tips on incorporating the source path from a JSON file into a Vue component

Is there a way to render images if the path is retrieved from a JSON file? Typically, I use require('../assets/img/item-image.png'). However, I'm uncertain how to handle it in this scenario. Component: <div v-for="(item, index) in i ...

Troubleshooting VueJS, Electron, and Webpack integration with Hot Reload feature

I have been immersed in a new project that involves utilizing Electron, VueJS, and Webpack for HMR functionality. Unfortunately, I am encountering difficulties with the Hot Module Replacement feature not working as expected. Here is my current configurati ...

Utilizing a JSON file for long-term storage of a compact database in JavaScript

As a newcomer to JSON, I wanted my webpage to display a small database of records in a table without using a traditional database like MySQL. Instead, I decided to read data from and write it out to a JSON file for convenient and persistent storage on my w ...

Improving the Roman Numeral Kata with JavaScript

As a newcomer to the world of coding, I have taken on the challenge of mastering the Roman Numeral Kata using Javascript. I am pleased to report that all the specifications are passing successfully. After refactoring the spec file, I am now focusing on re ...

React - How to properly pass a reference to a React portal

I have a Card component that needs to trigger a Modal component. Additionally, there is a versatile Overlay component used to display content above the application. Displayed here is the App component: class App extends Component { /* Some Code */ ...

Navigating through different tabs in an AngularJS application is made simple and efficient with the help of $

I used the angular-authentication-example to create a login page for my project. After logging in, the homepage should display multiple tabs just like in this example on Plunker. <ul class="nav nav-tabs" ng-controller="TabsCtrl"> <li ng-class= ...

Master the art of filtering rows in an HTML table based on a select option when the mouse is clicked

I am trying to create a table that displays only the rows selected in a dropdown menu. Here is an example: If "All" is selected, the table should display all rows. If "2017" is selected, the table should display only the rows that have "2017" in the sec ...

Utilizing a Json.Net object within an Ajax request

Is there a way to pass a .Net object in an Ajax call using the Json.Net javascript library instead of json2.js? You can find more information on passing complex types via Ajax calls at this link: I am familiar with how to serialize and deserialize object ...

Ways to verify if a user is authenticated without relying on request.session

I am currently developing a web application using Express, Docker, and following a Three-layered architecture. In my app, I store user login information in a session and have blogposts as a key resource. To retrieve the blogpostId from the database in the ...

Switching Primary Menu Selection When Scrolling Through Various Menu Options

I need help with changing the active class of links on my single page scrolling website. I found some useful code snippets for smooth scrolling and updating the active class on scroll or click events: $(document).ready(function () { $(document).on(" ...

Step-by-step guide on activating a button only when all form fields are validated

My very first Angular 5 project. I've gone through resources like: https://angular.io/guide/form-validation and various search results I looked up, only to realize that they are all outdated. In my form, I have several input fields such as: <for ...

Populate the AngularJS scope with a dynamically generated array

My Angular Application is functioning properly with <script> var app = angular.module('MyApp', []); app.controller('myCtrl', function ($scope, $sce) { $scope.urls = [ { "url": $sce.t ...

Is there a way to verify user credentials on the server using FeathersJS?

Currently, my single-page app is utilizing feathers client auth and a local strategy for authentication. I have implemented various middleware routes and I am looking to verify if the user is authenticated. If not, I would like to redirect them to /. Bel ...

Create an array using modern ES6 imports syntax

I am currently in the process of transitioning Node javascript code to typescript, necessitating a shift from using require() to import. Below is the initial javascript: const stuff = [ require("./elsewhere/part1"), require("./elsew ...