Utilizing Jison/Bison for string analysis

I'm currently diving into the world of Jison, a Javascript parser generator utilizing Bison syntax.

My current code snippet resembles the following:

a: "{{index()}}"
b: "{{blah(2, 'aba')}}"

My goal is to develop a parser that can interpret index() when given string a, and blah(2, 'aba') when given string b. In essence, I aim to parse strings containing method invocations.

I've been working on adapting examples from Jison but struggle due to my limited comprehension of parsing principles!

Below is the grammar file I've been working on:

/* lexical grammar */
%lex
%%

\s+                   /* ignore whitespace */
[a-zA-Z0-9]+          return 'STR'
"{{"                  return '{{'
"}}"                  return '}}'
<<EOF>>               return 'EOF'
.                     return 'INVALID'

/lex

/* operator associations and precedence */
%token '{{' '}}'

%start expressions

%% /* language grammar */

expressions
    : e EOF
        { typeof console !== 'undefined' ? console.log($1) : print($1);
          return $1; }
    ;

e
    : '{{' e '}}'
        {$$ = yytext;}
    | STR
        {$$ = yytext;}
    ;

It is evident that the grammar is not yet complete; it does not recognize parentheses. I'm currently focusing on a basic example of feeding the parser the string {{index}}. When I test this string, my current parser outputs }}. According to my (inaccurate) understanding of the grammar, I anticipated index as the output.

What could be the mistake in my approach?

Answer №1

To retrieve the nth token in an expression, utilize the syntax $n where n represents a specific number:

e
    : '{{' e '}}'
        {$$ = $2;}
    | STR
    ;

One modification needed in your original code is to implement the action $$ = $2; for the '{{' e '}}' expression. No action is required for STR since the default action $$ = $1 suffices for that case.

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

php and javascript

On my sales website, when I press the "sales" button, it opens a new frame. There is a chance for multiple frames to be open at the same time. My issue is that I want to close the frame after clicking enter and have the home page updated while keeping al ...

The form does not display the radio button value upon submission

Using redux form for the form elements, I encountered an issue where the radio button values were not being displayed when submitting the form. Despite getting values for email and password fields, the role value from the radio buttons was missing. In my i ...

Using jQuery to select the next table cell vertically

Is there a way to utilize jQuery to access the cell (td) directly below a given cell in a traditional grid-layout HTML table (where each cell spans only one row and column)? I understand that the code below will assign nextCell to the cell immediately to ...

Utilizing the Input method in Node.js

Transitioning from Python 3 to Node.js has me wondering if there is a similar function in Node.js to Python's input. For example, consider this code snippet: function newUser(user = null, password = null) { if (!user) user = prompt("New user name ...

Is it possible to add animated text using anime.js?

var elements = document.querySelectorAll('.content'); anime({ targets: elements, content: 100, }); <script src="https://raw.githubusercontent.com/juliangarnier/anime/master/lib/anime.min.js"></script> <span class="content"> ...

Structural directive fails to trigger event emission to parent component

Following up on the question posed here: Emit event from Directive to Parent element: Angular2 It appears that when a structural directive emits an event, the parent component does not receive it. @Directive({ selector: '[appWidget]' }) export ...

How to retrieve the third party child component within a Vue parent component

Within my example-component, I have integrated a third-party media upload child component called media-uploader: <example-component> <form :action= "something"> // Other input types <media-upload :ref="'cover_up ...

Add a SlideUp effect to the .removeClass function by using a transition

Looking to incorporate a SlideUp transition while removing the class with .removeClass. This script handles showing/hiding the navigation menu based on page scroll up or down. I am looking to add a transition effect when the navigation menu hides. Check ou ...

What techniques does Angular2 use to handle dependency injection?

When working with Angular2 components, I know that to inject a dependency, you simply annotate an argument in the constructor, like how ThingService is injected here. However, what I am wondering is how Angular actually knows what to inject at runtime. A ...

What is preventing me from translating JS Canvas Image?

I've been struggling with using ctx.translate() to translate images on a canvas. No matter what I do, it just won't work. I've spent a good amount of time trying to troubleshoot the issue. Here's the snippet of code I'm working wit ...

What is the best way to invoke functions with input of type 'number'?

As someone new to HTML, I have an input field with type=number: <input type="number" min="1" value="1" id="amount-input" (click)="chooseStanding(standingspot.priceCategory)"> When the (click) event i ...

Learn the steps to export a constant value in Next.js using React!

I need to export the constant value views from BlogPost.js to blog.js. This is the content of BlogPost.js: import React from 'react'; import useSWR from 'swr'; import format from 'comma-number'; import { useColorMode, He ...

Is there a way to incorporate a Laravel foreach loop within a JavaScript file?

I recently added a select-box using jQuery: <span onclick="createProduct()">Add New<i class="fa fa-plus"></i></span> <script> function createProduct() { var html = ''; html += ' <div clas ...

Display two elements within a single table column by utilizing an array in Vue.js

I have a requirement to display 2 items from an array in one table TD ( column ). Below is the example mock data provided: agendas: [ [ { tag: 'p', class: 'm-agenda__date', value: & ...

Is there a way to enclose a mention within a unique span tag after highlighting it? Similar to how tags are implemented on platforms such

Currently utilizing an AngularJS plugin called ment.io for incorporating mentions. However, I am having difficulty figuring out how to customize the appearance of the selected mention. For example, in Stackoverflow: https://i.sstatic.net/bZrkh.png Or i ...

My application built with React and Flask successfully processes JSON data on one route, but encounters issues on another route

The code I have in place is working quite well, with the frontend being the next area of focus. This code effectively registers a user and updates the database: export default class APIService { static RegisterUser(username, email, password, base_city, ...

React element failing to appear on webpage

I'm having trouble loading the snippetInfo.js file in the HTML, and I can't seem to figure out why. I've searched online extensively for solutions, but none of them have worked for me. Whenever I try adding type='text/javascript' t ...

Unlock the power of AJAX in your WordPress site

I've been exploring the realm of Javascript and AJAX lately. I feel like I'm so close to getting it right, but there's something off with how I'm integrating WordPress ajax functions. I've spent a lot of time going through the docu ...

Guide to creating a key-value pair from an array

Here is the JSON data: [["123", "456", "789", "user1"], ["987", "654", "321", "user2"]] In my code, I stored it like this: var rows = [ ["123","456","789","user1"] , ["987","654","321","user2"] ]; I am trying to figure out how to create key-value ...

Ways to extract the final digit from a format such as an IP address in JavaScript

Is there a way to retrieve the last digits 192.168.1.180 For instance: From the IP address 192.168.1.180, I would like to extract 180. Thank you in advance ...