Questions tagged [pegjs]

PEG.js is a simple parser generator for JavaScript that produces fast parsers with excellent error reporting. Forked to a now-maintained version called Peggy.

PEG.js is a simple parser generator for JavaScript that produces fast parsers with excellent error reporting.

See https://pegjs.org/

The project was forked to create Peggy, which is now maintained at:

https://peggyjs.org/

123 questions
1
vote
2 answers

PEG grammar which accepts three optional elements in any order

Let's assume we have three elements a b and c. A valid expression uses these three elements (and optional whitespace). At least one of these three elements has to be present. All three elements are optional (as long as at least one of the other…
user1924627
  • 349
  • 4
  • 11
1
vote
3 answers

PEGjs: Request Help/guide in defining Arithmetic functions

I'm learning the PEGjs grammar and I request help or guide on the following: I have functions like, PRODUCT(), SUM(), DIVIDE() PRODUCT can take number/PRODUCT()/SUM()/DIVIDE() as parameters (any number but comma separated) ex: PRODUCT(2, 5, SUM(5,…
Reddy
  • 1,403
  • 3
  • 14
  • 27
1
vote
1 answer

How to resolve left recursions in PEG

The problem is, that PEGs (parsing expression grammars) do not allow left-recursive rules. I have read the available answers on this topic, however problem specific (like this one) or quite simple (e.g. x = symbol:(x '.')). I've created the…
Dänu
  • 5,791
  • 9
  • 43
  • 56
1
vote
1 answer

PEGJS : Nested pegjs grammar

start = intExp intExp = andIntExp / orIntExp andIntExp = integer (andExp intExp)* orIntExp = integer (orExp intExp)* andExp = space* "and" space* { return "and";} orExp = space* "or" space* { return "or";} space = [\n…
Anuj Kulkarni
  • 2,261
  • 5
  • 32
  • 42
1
vote
1 answer

Parsing constant and identifier

I want to match a constant, which is basically an all uppercase string. Also, I want to match an identifier, which can contain a mix of lowercase and uppercase letters. Start = Constant / Identifier Identifier = h:[A-Za-z_] t:[a-zA-Z0-9_]* {…
gosukiwi
  • 1,569
  • 1
  • 27
  • 44
1
vote
2 answers

Why is PEGjs / not working correctly?

I have a simple parser in PEGjs start = val ln = [\n\r] float = digits:$[-0-9\.]+ { return parseFloat(digits, 10) } str = str:$(!ln !"\"" .)+ val = float / str and I try to match -this But instead of getting "str" it gives error on parsing…
Kengur
  • 19
  • 4
1
vote
1 answer

PEGJS: How to add NOT (!) logical operator to grammar that parses AND (&&) OR (||) logic statements

I'm very new to writing grammars (first time ever to be exact) and I'd like to create a grammar that can return an AST for basic logic statements. So far I have a grammar that can handle AND, OR logic (I simply modified the basic calculator example…
Sarus
  • 3,303
  • 1
  • 23
  • 27
1
vote
2 answers

Detecting _vars_with_underscores_; why does this not work?

I am trying to write a PEGjs rule to convert Return _a_b_c_. to Return <>a_b_c. My grammar is root = atoms:atom+ { return atoms.join(''); } atom = variable / normalText variable = "_" first:variableSegment rest:$("_" variableSegment)*…
Domenic
  • 110,262
  • 41
  • 219
  • 271
1
vote
1 answer

Peg left recursion removing

I have this pegjs grammar. How can I remove the left recursion from? atom = term / "^" / "_" / "\\" / atom "." / atom "." label / atom ".(" labels ")" term = [a-zA-Z0-9]+ labels = label ("|" label)* label = ("+" /…
Yak O'Poe
  • 760
  • 4
  • 14
1
vote
2 answers

How do I DRY this PEGjs rule?

the following works just fine for what I'm trying to do, but it's obviously very repetitive. It should match the following examples: #id.class1.class2 attr="asdsa" .class1.class2 attr="asdsad" attr="asds" It's tempting to use id:idShortcut?…
1
vote
2 answers

How to transform a simple grammar into something which works in PEG.js (expected "a" but "a" found)

I've just started playing with PEG.js and have a problem with a grammar (vastly simplified for debugging): start = presingle single / preplural plural presingle = "a" / "b" preplural = "b" / "c" single = "d" / "e" plural =…
fadedbee
  • 42,671
  • 44
  • 178
  • 308
0
votes
0 answers

Problem: Integration of Lexer Tokens in PEG Grammar in JavaScript

Motivation I'm learning how to write and use a small lexer and parser in JavaScript. For the lexer, I chose to use the moo library, and for parsing, I decided to use Peggy. I was able to successfully match values from an array during lexing, which…
Suppenterrine
  • 55
  • 1
  • 7
0
votes
1 answer

How to implement "array type" grammar like typescript with pegjs?

my problem is in the implementation of "array type" like typescript. according to my grammar. In "array type" you can use "[]" after any type (eg string or int or even array again like int[][]). this is simplified version of my grammar: start =…
Aidin53
  • 531
  • 4
  • 9
0
votes
1 answer

PEGjs | Multiple output issue

Why we can output once in PEG.js? do anyone know any other way to implement many output? I am using Stack and a function , code below: function evalStack() { for (var i = stack.length - 1; 0 <= i; i--) { stack[i](); } …
0
votes
1 answer

I cannot implement variable support

I am trying to implement 'variable declaration future' to my parsed language. PEG.js source: start =begin line (pl)+ end pl =varx" " left:identifier" "to" "middle:integer line { left=middle;} / print"(" middle:identifier ")" line…
1 2 3
8 9