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

How do I parse this with peg grammar?

I'm trying to make a parser using pegjs. I need to parse something like: blah blah START Lorem ipsum dolor sit amet, consectetur adipiscing elit END foo bar etc. I have trouble writing the rule to catch the text from "START" to "END".
John Smith
  • 4,402
  • 4
  • 32
  • 34
8
votes
1 answer

pypeg2 - can this expression be parsed using peg grammar?

I need to parse expressions based on following rules: An expression can contain a filter object represented as name:value An expression can contain a string expression An expression can contain Booleans OR,AND Everything inside can be quoted So a…
Jan Vorcak
  • 19,261
  • 14
  • 54
  • 90
8
votes
10 answers

What's the best way to parse a body of text against multiple (15+) regexes on each line?

I have a body of text that I have to scan and each line contains at least 2 and sometimes four parts of information. The problem is that each line can be 1 out of 15-20 different actions. in ruby the current code looks somewhat like…
eyberg
  • 3,160
  • 5
  • 27
  • 43
8
votes
3 answers

Who is faster: PEG or GLR?

I'm trying to create some kind of lint tool for the C/AL programming language. So basically I need to perform syntax and lexical analysis against the source code. I've planned to write parser from the scratch, but then discovered that there are a…
shytikov
  • 9,155
  • 8
  • 56
  • 103
7
votes
1 answer

What is the meaning of `~` in python grammar

I was going through the python grammer specification and find the following statement, for_stmt: | 'for' star_targets 'in' ~ star_expressions ':' [TYPE_COMMENT] block [else_block] What does ~ means in this grammar rule?. The other symbols used…
Abdul Niyas P M
  • 18,035
  • 2
  • 25
  • 46
7
votes
1 answer

BNF grammar definition for file path wildcard (glob)

I'm searching for some widely extended dialect (like this one https://github.com/vmeurisse/wildmatch + globstar **) described with BFN rules. In any format or language. OMeta or PEG would be great.
Ev_genus
  • 135
  • 11
7
votes
1 answer

Need help to understand LPeg and PEGs

The following pattern (from this page) matches only strings with balanced parentheses: b = lpeg.P{ "(" * ((1 - lpeg.S"()") + lpeg.V(1))^0 * ")" } What does 1- in 1 - lpeg.S"()" mean? function gsub (s, patt, repl) patt = lpeg.P(patt) patt =…
Decula
  • 494
  • 4
  • 16
7
votes
2 answers

Error message on match fail in Rebol Parse

PEG-based parser generators usually provide limited error reporting on invalid inputs. From what I read, the parse dialect of rebol is inspired by PEG grammars extended with regular expressions. For example, typing the following in JavaScript: d8>…
Erick
  • 305
  • 2
  • 7
7
votes
1 answer

Parsing boolean expression without left hand recursion

I'm trying to match this f(some_thing) == 'something else' f(some_thing) is a function call, which is an expression == is a boolean operator 'something else' is a string, which also is an expression so the boolean expression should…
gosukiwi
  • 1,569
  • 1
  • 27
  • 44
7
votes
3 answers

Implementing "cut" in a recursive descent parser

I'm implementing a PEG parser generator in Python, and I've had success so far, except with the "cut" feature, of which whomever knows Prolog must know about. The idea is that after a cut (!) symbol has been parsed, then no alternative options…
Apalala
  • 9,017
  • 3
  • 30
  • 48
7
votes
3 answers

Using PEG Parser for BBCode Parsing: pegjs or ... what?

I have a bbcode -> html converter that responds to the change event in a textarea. Currently, this is done using a series of regular expressions, and there are a number of pathological cases. I've always wanted to sharpen the pencil on this grammar,…
Steve Ross
  • 4,134
  • 1
  • 28
  • 40
6
votes
1 answer

Using LPEG (Lua Parser Expression Grammars) like boost::spirit

So I am playing with lpeg to replace a boost spirit grammar, I must say boost::spirit is far more elegant and natural than lpeg. However it is a bitch to work with due to the constraints of current C++ compiler technology and the issues of TMP in…
Hassan Syed
  • 20,075
  • 11
  • 87
  • 171
6
votes
1 answer

Difference between a PEG and recursive descent parser?

I have recently come across PEG parsers, and Guido van Rossum's article on PEG parsers and how to construct them. That article talks about "PEG" parsers but internally it looks exactly like a recursive descent parser (generator). I have a feeling…
Monolith
  • 1,067
  • 1
  • 13
  • 29
6
votes
1 answer

What is the role of the Empty production for PEGs?

The empty production rule nonterminal -> epsilon is useful in lex-yacc LR bottom up parser generators (e.g. PLY). In what context should one use Empty productions in PEG parsers e.g. pyparsing ?
Frankie Ribery
  • 11,933
  • 14
  • 50
  • 64
6
votes
1 answer

How to describe function arguments in PEG grammar

I'm still fighting with ambiguous grammar of Qt's qmake. Now I can't find a way to describe function arguments that can contain parenthesis (e.g. regex): functionName(arg1, "arg2", ^(arg3)+$) I've tried to describe function call like…
eraxillan
  • 1,552
  • 1
  • 19
  • 40
1
2
3
19 20