Questions tagged [peg]

A “Parsing Expression Grammar” (“PEG”) is a formal language to describe formal languages.

A Parsing Expression Grammar (“PEG”) is a formal language that defines a set of rules to describe a certain class of formal languages similar to context-free grammars.

In comparison to context-free grammars, PEGs have the advantage of always being unambiguous, and of mapping naturally to recursive descent parsers, which makes them easy to implement.

288 questions
0
votes
1 answer

Tatsu grammar parser and codegen producing two very different results

I'm working with TatSu, and the results I get from the codegen parser is very different from the one I get from when the parser is build directly. Consider the fairly simple grammar for dice notation: start = expression $; int = /-?\d+/ ; dice =…
0
votes
1 answer

I have produced a parser.js file using PEG.js, however I get an import error when linking to HTML. Help in solving this would be appreciated

I have successfully produced a parser.js file using the peg.js online tool. I have linked it to my html file using: I intended to run parser.parse(txt) from my script however I get ReferenceError: module is not…
0
votes
2 answers

PEG grammar turns computer into hot-plate and never finishes

I'm trying to write a grammar so I can parse a specific type of input file. I've started with the most basic grammar possible, but guile just about melts my computer when trying to match a pattern with this grammar. I'm wondering if there is…
dylanjm
  • 2,011
  • 9
  • 21
0
votes
1 answer

How to define a rule to match with a pattern multiple times in PEG.js?

I'm trying to parse a file where the pattern might be seen multiple times: G04 hello world* G04 foo bar* The corresponding PEG.js grammar is: Comment = "G04" _ content:String* _ EOL { return content } _ "whitespace" = […
ceremcem
  • 3,900
  • 4
  • 28
  • 66
0
votes
2 answers

Are there PEG-based parser generators that support left recursion?

Left recursion seems to be a big problem for many parser generators that are built upon the foundations of recursive descent parsing. I'm looking for a PEG-based parser generator that supports it - in whatever language.
0
votes
1 answer

Can alternative rules overlap in their intial tokens in PEG?

I think I just realized that a problem I've run into several times and for which I've always found some odd work-around might just be fundamental. I'm hoping someone can either validate that understanding or show me what I'm doing wrong. I'm trying…
Scott Sauyet
  • 49,207
  • 4
  • 49
  • 103
0
votes
1 answer

Use PEG.js generated parser to beautify code

I want to create a formatter/linter for a custom program language and was reading about it but seems that im missing something. Was looking/playing with PEG.js and seems that it will do the work. Ive wrote a small parser and when ran - it correctly…
Stefan Stoichev
  • 4,615
  • 3
  • 31
  • 51
0
votes
1 answer

parser unable to match the consequent rule due to error evaluating a former rule

Example: start = name / invocation; name = [a-zA-Z]+ { return text() }; invocation = a:name "()" { return {type: 'inv', value: a } }; If input is abc() I am getting error: Expected [a-zA-Z] or end of input but "(" found However, if start was…
deostroll
  • 11,661
  • 21
  • 90
  • 161
0
votes
1 answer

PEG NodeVisitor

Imagine a small PEG grammar like from parsimonious.grammar import Grammar from parsimonious.nodes import NodeVisitor grammar = Grammar( r""" term = lpar (number comma? ws?)+ rpar number = ~"\d+" lpar = "(" rpar = ")" …
Jan
  • 42,290
  • 8
  • 54
  • 79
0
votes
1 answer

How can I create a PEG.js pattern for my DB queries (X "AND" Y "AND" ...)

So far my parser looks like this. const parser = peg.generate(` eq = left:attribute "=" right:value { return left == right; } and = left:eq "AND" right:eq { return left && right; } It is able to read queries like id = 2 AND createdOn =…
gatsbyz
  • 1,057
  • 1
  • 10
  • 26
0
votes
1 answer

Difference between Peg.js and Regex

I'd like to use Peg.js to parse and validate something I've been doing with Regular Expressions and am struggling with the syntax. My Peg.js program is : start = (var / other) cr d:var {return d.join('')} var =…
user5072412
  • 187
  • 1
  • 10
0
votes
0 answers

parsing ingredient list with parsimonious

from parsimonious.grammar import Grammar grammar = Grammar( ''' # item = ( ( ingredient+ '(' ingredient+ ')' comma ws?) + / comma+ / ws+ / ingredient )+ item = ((ingredient '(' ingredient ')') / ingredient )+ ingredient = ( (…
Dave
  • 390
  • 1
  • 6
  • 16
0
votes
1 answer

PEG-parsing declarations with arbitrary number of qualifiers

I want to parse for instance: const unsigned int varname; where the number of qualifiers is unknown and the list of qualifier keywords is also unknown (since new ones can be introduced using using). But using the PEG grammar declaration <-…
Mircode
  • 432
  • 5
  • 12
0
votes
1 answer

PEG.js - throw error if expression does not match

I am currently writing a PEG.js grammar and I want it to output custom errors. For instance I currently have this structure for creating a function. //Function Declaration FUNCTION_DECLARATION = FUNCTION __ t:(TYPE/VOID) __ n:KEY…
0
votes
1 answer

Sqllite PEG parser to support BigQuery SQL syntax

I'm using SqLite Parser to parse my SQL to JSON. We are now using Google BQ and some SQL commands are failing in this parser, for example: select EXTRACT(MONTH FROM DATE_SUB(CURRENT_DATE(), INTERVAL 1 MONTH)) This can be easily reproduced by using…
Tamir Klein
  • 3,514
  • 1
  • 20
  • 38