• Latest
Implementing PEG in JavaScript – DZone

Implementing PEG in JavaScript – DZone

March 13, 2023
The Rise of MKBHD: Marques Brownlee's Journey to YouTube Stardom #shorts #marquesbrownlee #mkbhd

The Rise of MKBHD: Marques Brownlee's Journey to YouTube Stardom #shorts #marquesbrownlee #mkbhd

June 9, 2023
How to watch Tom Holland in The Crowded Room on Apple TV

How to watch Tom Holland in The Crowded Room on Apple TV

June 9, 2023
Sonic Superstars Offering Free “Modern” Amy Outfit To Newsletter Subscribers

Sonic Superstars Offering Free “Modern” Amy Outfit To Newsletter Subscribers

June 9, 2023
(For Southeast Asia) “FINAL FANTASY VII REBIRTH” New Trailer Revealed! – PlayStation.Blog

(For Southeast Asia) “FINAL FANTASY VII REBIRTH” New Trailer Revealed! – PlayStation.Blog

June 9, 2023
Sonic Superstars Includes “New Character” By OG Designer Naoto Ohshima

Sonic Superstars Includes “New Character” By OG Designer Naoto Ohshima

June 9, 2023
9to5Mac Happy Hour 423: iOS 16.4 beta 2, 2018 iPad Pro in hindsight, MLS Season Pass experience

9to5Mac Happy Hour 437: iOS 17, watchOS 10, 15-inch MacBook Air, and more WWDC impressions

June 9, 2023
Two Switch Games Will “No Longer Be Available” On The eShop

Two Switch Games Will “No Longer Be Available” On The eShop

June 9, 2023
Marvel Snap to Get Tournament Mode Aimed at High-Skilled Players

Marvel Snap to Get Tournament Mode Aimed at High-Skilled Players

June 9, 2023
FAKEFRIEND#attitude @adazionofficial @UncleDane @mkbhd @Officialblessingceo

FAKEFRIEND#attitude @adazionofficial @UncleDane @mkbhd @Officialblessingceo

June 9, 2023
Summer Game Fest: Baldur’s Gate 3 Will Feature Jason Isaacs’ Menacing Brand of Villainy

Summer Game Fest: Baldur’s Gate 3 Will Feature Jason Isaacs’ Menacing Brand of Villainy

June 9, 2023
Noob prank on @fizagamingofficial 😂 1vs4 clutch 💪 #freefire #shorts #viral #youtubeshorts

Noob prank on @fizagamingofficial 😂 1vs4 clutch 💪 #freefire #shorts #viral #youtubeshorts

June 9, 2023
1Password teases passkey support coming to its app with iOS 17

1Password teases passkey support coming to its app with iOS 17

June 9, 2023
Advertise with us
Friday, June 9, 2023
Bookmarks
  • Login
  • Register
GetUpdated
  • Game Updates
  • Mobile Gaming
  • Playstation News
  • Xbox News
  • Switch News
  • MMORPG
  • Game News
  • IGN
  • Retro Gaming
  • Tech News
  • Apple Updates
  • Jailbreak News
  • Mobile News
  • Software Development
  • Photography
  • Contact
No Result
View All Result
GetUpdated
No Result
View All Result
GetUpdated
No Result
View All Result
ADVERTISEMENT

Implementing PEG in JavaScript – DZone

March 13, 2023
in Software Development
Reading Time:3 mins read
0 0
0
Share on FacebookShare on WhatsAppShare on Twitter


Parsing is an age-old technique used to analyze and extract meaning from languages (both natural and programming). Parser is a type of compiler that converts the stream of text into syntax or parse tree that conforms to some predefined grammar rules.

There are various classifications to categorize these techniques, and plenty of content is available to explain them. So, for now, I am focusing on Parser Expression Grammar (which is the most recent research in parsing grammar). Also, I will try to explain the ways to implement a PEG parser.

What Is PEG (Parser Expression Grammar)?

Parser Expression Grammar (PEG) is the formal grammar that defined a set of recursive rules under the family of top-down parsing language. Its parsers generate an explicitly single parse tree for any input. It is more powerful than regular expressions but might have some performance drawbacks related to memory and time in a few scenarios.

Advantages of Using PEG Parsers

PEG parsers have some advantages over other types of parsers. Most noticeably, they are unambiguous as they choose the first option among choices. Also, it is scanner less, which means it does not require a separate lexing phase. That makes it easier to implement for parsing needs that have smaller usability than that is required to parse a variety of inputs in an enterprise use case.

Understanding the PEG Structure

Let’s try to understand the PEG structure with the following basic example that can be used for parsing arithmetic expressions.

start
  = additive

additive
  = left:multiplicative "+" right:additive 
  / multiplicative

multiplicative
  = left:primary "*" right:multiplicative 
  / primary

primary
  = integer
  / "(" additive:additive ")"

integer "integer"
  = digits:[0-9]+

Here, all the rules are recursive and drill down to literals or character classes with regular expressions. As we can see, an `additive` expression is an expansion of a `multiplicative` expression, and a `multiplicative` expression expands to an integer literal or nested additive expression. The integer literal is one or many occurrences of digits.

QuickStart

The quickest way to write or generate a PEG parser in javascript is to use pegjs. It is the most popular (as per GitHub) library to implement PEG parsers. You can refer to the official documentation for installation instructions. It supports both CLI and API modes for generating the parser.

Command Line

pegjs -o arithmetics-parser.js arithmetics.pegjs

JavaScript API

var peg = require("pegjs");

var parser = peg.generate("start = ('a"https://dzone.com/"b')+");

Online Tool

Apart from these modes, there is an online mode available that allows you not only to validate your grammar but also allows you to quickly test with sample inputs. Once you are done with testing, you can generate your parser on the fly with a speed or a code-optimized version.

Online Tool

Using the Parser

The generated parser can be used in both node and browser environments. You can call the `parse` method with test input, and it will either return a parse tree or an error ( for invalid inputs).

parser.parse("abba"); // returns ["a", "b", "b", "a"]

parser.parse("abcd"); // throws an exception

Sample Implementation

There is plenty of tools and services that use it in some or another way. The most update to date and advanced implementation is node-sql-parser (built by Zhi Tao). This is a pool of parsers for various modern query languages for databases like BigQuery, Hive, and Flink.



Source link

ShareSendTweet
Previous Post

How I Met Marques Brownlee at his Pro Ultimate Game

Next Post

LG Wing: The Swiveling Smartphone!

Related Posts

Breaking Down the Monolith – DZone

June 8, 2023
0
0
Breaking Down the Monolith – DZone
Software Development

This is an article from DZone's 2023 Containers Trend Report.For more: Read the Report Conventionally, software applications were developed using...

Read more

Superior Stream Processing – DZone

June 8, 2023
0
0
Superior Stream Processing – DZone
Software Development

In the era of data-driven decision-making, the Data Lakehouse paradigm has emerged as a promising solution, bringing together the best...

Read more
Next Post
LG Wing: The Swiveling Smartphone!

LG Wing: The Swiveling Smartphone!

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

© 2021 GetUpdated – MW.

  • About
  • Advertise
  • Privacy & Policy
  • Terms & Conditions
  • Contact

No Result
View All Result
  • Game Updates
  • Mobile Gaming
  • Playstation News
  • Xbox News
  • Switch News
  • MMORPG
  • Game News
  • IGN
  • Retro Gaming
  • Tech News
  • Apple Updates
  • Jailbreak News
  • Mobile News
  • Software Development
  • Photography
  • Contact

Welcome Back!

Login to your account below

Forgotten Password? Sign Up

Create New Account!

Fill the forms bellow to register

All fields are required. Log In

Retrieve your password

Please enter your username or email address to reset your password.

Log In
Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?