peg

Example of how to use PEG.js

徘徊边缘 提交于 2021-02-18 17:09:27
问题 I'm playing around with PEG.js. I created some simple code that accepts inputs in the form [LettersNumbers]: abc123 hello98765 etc. This is the code: start = expression expression = text + number text = a: [a-z]+ {return a.join("");} number = b:[0-9]+ {return b.join("");} Here: Online version the code can be tested and the parser downloaded, additionally I downloaded peg.js itself. Unfortunately, the documentation is very sparse. I tried: <script src="peg-0.9.0.min.js"></script> <script src=

Make a Grammar expression for STRING.STRING.STRING on PEG.js

北城余情 提交于 2020-01-24 11:45:08
问题 I am looking for a peg.js grammar expression for matching against: "variable" # Fails "variable." # Fails "" # Fails "variable.variable" # Ok "variable.variable.variable.variable.variable" #Ok input I expect {PATH: "variable.variable"} {PATH: "variable.variable.variable.variable.variable"} Sample.pegjs start = PATH_EXP STRING_EXP = chars:[0-9a-zA-Z_]+ { return chars.join(""); } PATH_EXP = path:(STRING_EXP "." STRING_EXP) { return {PATH: path.join("")}; } I don't know how to make the

Make a Grammar expression for STRING.STRING.STRING on PEG.js

孤街浪徒 提交于 2020-01-24 11:44:53
问题 I am looking for a peg.js grammar expression for matching against: "variable" # Fails "variable." # Fails "" # Fails "variable.variable" # Ok "variable.variable.variable.variable.variable" #Ok input I expect {PATH: "variable.variable"} {PATH: "variable.variable.variable.variable.variable"} Sample.pegjs start = PATH_EXP STRING_EXP = chars:[0-9a-zA-Z_]+ { return chars.join(""); } PATH_EXP = path:(STRING_EXP "." STRING_EXP) { return {PATH: path.join("")}; } I don't know how to make the

Overcoming infinite left recursion in TextX parser

被刻印的时光 ゝ 提交于 2019-12-25 00:48:58
问题 I am writing a parser for an existing language, using the TextX Python Library (based on the Arpeggio PEG parser) But when I try to use it to parse a file, I get the exception: RecursionError: maximum recursion depth exceeded while calling a Python object Here is a minimal example that raises this exception: #!/usr/bin/env python from textx import metamodel_from_str meta_model_string = "Expr: ( Expr '+' Expr ) | INT ;" model_string = "1 + 1" mm = metamodel_from_str(meta_model_string, debug

Is parsing in multiple passes common for PEG grammars?

一曲冷凌霜 提交于 2019-12-24 12:26:54
问题 I'm designing a music programming language and implementing its syntax as a PEG grammar. The parsing process has ended up being fairly complicated, so what seemed like the simplest approach was to define several, separate grammars, and apply them in sequence. So far I have three grammars: Take the entire contents of source file and strip out the comments. Take the source file (comments removed) and separate it by instrument. This results in pairs of instrument name/definition and the "music

Re-write Parsing Expression Grammar (PEG) without left recursion

≡放荡痞女 提交于 2019-12-22 09:33:30
问题 Using https://github.com/JetBrains/Grammar-Kit how to rewrite grammar without left recursion? grammar ::= exprs exprs::= (sum_expr (';')?)* private sum_expr::= sum_expr_infix | sum_expr_prefix sum_expr_infix ::= number sum_expr_prefix left sum_expr_prefix::= op_plus number private op_plus ::= '+' number ::= float | integer float ::= digit+ '.' digit* integer ::= digit+ private digit ::=('0'|'1'|'2'|'3'|'4'|'5'|'6'|'7'|'8'|'9') Sample input: 10+20+30.0; 10+20+30.0 Answer shall maintain parse

Parsing boolean expression without left hand recursion

烈酒焚心 提交于 2019-12-13 11:43:07
问题 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 be expression operator expression The problem is I can't figure out how to do that without left recursion These are my rules expression = bool_expression / function_call / string / real_number / integer / identifier bool_expression = l:expression space* op:bool

What is the best way to define grammars for a text editor?

心已入冬 提交于 2019-12-10 20:13:42
问题 I'm masochistically writing an open-source text editor for Mac and have finally reached the point at which I want to add syntax highlighting. I've been going back and forth on various solutions for the past few days, and I've finally decided to open the question to a wider audience. Here are the options I see: Define languages basically with a series of regex pattern matching (similar to how TextMate defines its languages) Define languages with a formal grammar like BNF or PEG Using regex

Parsing complete mathematical expressions with PEG.js

随声附和 提交于 2019-12-10 14:54:49
问题 I'm trying to extend the example grammar of PEG.js for parsing mathematical expressions with all the 4 operators for my online BASIC interpreter experiment: http://www.dantonag.it/basicjs/basicjs.html but not all the expressions are parsed correctly. This is my PEG grammar: expression = additive additive = left:multiplicative atag:("+" / "-") right:additive { return {tag: atag, left:left, right:right}; } / multiplicative multiplicative = left:primary atag:("*" / "/") right:multiplicative {

Any PEG parser capable to handle left recursion? [closed]

廉价感情. 提交于 2019-12-10 13:52:28
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 2 years ago . Well, I know it's possible to rewrite the grammar to eliminate left recursion. But this is a very boring process, and sometimes it's very nontrivial to keep correct associativity. Is there any parser capable to handle properly grammars with left recursion? 来源: https://stackoverflow.com/questions/4397039/any-peg