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
2
votes
1 answer

Generating correct phrases from PEG grammars

I wrote a PEG parser generator just for fun (I will publish it on NPM some time), and thought it would be easy to add a randomised phrase generator on top of it. The idea is to automatically get correct phrases, given a grammar. So I set the…
ostrebler
  • 940
  • 9
  • 32
2
votes
2 answers

how to write a simple peg grammar for a liquid-like templating language?

edit: you can follow the progress here: https://github.com/simple-updates/template I'm using peg.js and trying to write something that could interpret a template like: hello {{ "world" }} {% if a %} good {{ a }} {% else %} bad {% endif %} I've…
localhostdotdev
  • 1,795
  • 16
  • 21
2
votes
2 answers

Python parser for lambda calculus

For fun, I want to write a parser for the untyped Lambda calculus. The easiest approach is probably write a handwritten parser, but I wonder if there is a more Pythonic way? Specifically, I want to use a Python library that translates the syntax…
Björn Lindqvist
  • 19,221
  • 20
  • 87
  • 122
2
votes
1 answer

Avoiding Left Recursion parsing Lambda Calculus while maintaining Left Associativity

I am trying to parse lambda calculus terms into AST leveraging JavaScript and PEG.JS. The grammar is fairly easy: /***************************************************************** t ::= x …
akaphenom
  • 6,728
  • 10
  • 59
  • 109
2
votes
1 answer

How to define unicode ranges by properties/identifiers in c++ for PEGTL

With PEGTL (https://github.com/taocpp/PEGTL), which is a template based C++11 header-only PEG library, I can define ranges for unicode characters like this: utf8::range<0x0, 0x10FF> //All UTF8 Characters utf8::range<0x41, 0x5A, 0x61, 0x7A> //UTF8…
engelant
  • 121
  • 10
2
votes
1 answer

PEG.js matching word from Array

I have problem with PEG.js and matching words. It looks like this: Words = "stack"/"overflow"/"stackoverflow" - when I try to match "stackoverflow" it shows error Expected end of input but "o" found. so it found stack and thought it was end -…
Filemon279
  • 178
  • 2
  • 11
2
votes
1 answer

Match the string literal containing underscope using PEG.js

i am continue to learning PEG.js, but stuck on the next issue. PEG.js-generated parser unable to match string containing underscopes: CONFIG += stl_off but successfully parse the string without them: CONFIG += static (this is a built-in variable…
eraxillan
  • 1,552
  • 1
  • 19
  • 40
2
votes
1 answer

Save variable value for future use on peg.js

I am implementing a Relational Algebra to SQL converter using Peg.js. I got implementing almost all actions, but I not implement the assigment operator, where a relation is converted to SQL and is saved in a variable for future use. (Ex: A <- Π id…
2
votes
1 answer

Parser expression grammar - how to match any string excluding a single character?

I'd like to write a PEG that matches filesystem paths. A path element is any character except / in posix linux. There is an expression in PEG to match any character, but I cannot figure out how to match any character except one. The peg parser I'm…
marathon
  • 7,881
  • 17
  • 74
  • 137
2
votes
1 answer

Why does the Grako parsing process fail if my grammar contains an expression that consists of many or-concatenated subexpressions?

I am using Grako. In my EBNF grammar, I have an expression that consists of a lot of subexpressions that are concatenated using the OR-operator, like so: expression = subexpressionA | subexpressionB | ... | subexpressionZ; The parsing process…
Matthias
  • 440
  • 3
  • 16
2
votes
1 answer

The element of the two statements are mutually referenced with peg.js

define the block like this compound_stat = '{' decl exp_stat '}' exp_stat = exp ';' decl = decl_specs id ';' decl_specs = 'int'/'float' id =name:[a-z]+ {return name.join("");} exp_stat = left:multiplicative "+" right:exp_stat {…
freyone
  • 349
  • 1
  • 8
2
votes
2 answers

Wanted: example(s) of PEG Grammar for deeply nested Python boolean expressions

I would like an example of a PEG grammar for deeply nested Python boolean expressions. The PEG grammar can't be "very recursive" or this will lead to stack overflow. Support for '|', '&', '(' and ')' required. Example of input:…
SoupMonster
  • 109
  • 1
  • 10
2
votes
1 answer

Parse one or more expressions with helpful errors

I'm using grako (a PEG parser generator library for python) to parse a simple declarative language where a document can contain one or more protocols. Originally, I had the root rule for document written as: document = {protocol}+ ; This…
shader
  • 801
  • 1
  • 7
  • 25
2
votes
2 answers

Parse STEP-file (with javascript)

I'm trying to read the information from a STEP-file (ISO 10303-21) with javascript. I'm not sure how to tackle this at all. I have found open source alternatives but in all languages but Javascript... So I'm not able to learn much from them. Since…
mottosson
  • 3,283
  • 4
  • 35
  • 73
2
votes
1 answer

Matching OR expression using Grappa (Java PEG Parser)

I'm new to PEG parsing and trying to write a simple parser to parse out an expression like: "term1 OR term2 anotherterm" ideally into an AST that would look something like: OR -----------|--------- | | "term1" …
rainkinz
  • 10,082
  • 5
  • 45
  • 73