pegjs

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=

Does the Peg.js engine backstep after a lookahead like regexs do?

邮差的信 提交于 2019-12-25 00:46:58
问题 According to regular-expressions.info on lookarounds, the engine backsteps after a lookahead: Let's take one more look inside, to make sure you understand the implications of the lookahead. Let's apply q(?=u)i to quit. The lookahead is now positive and is followed by another token. Again, q matches q and u matches u. Again, the match from the lookahead must be discarded, so the engine steps back from i in the string to u. The lookahead was successful, so the engine continues with i. But i

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

Pegjs: Don't allow reserved keywords as a variable name

社会主义新天地 提交于 2019-12-12 01:58:47
问题 I am writing my language in Pegjs and as usual, my language has some keywords, like true , false , if , else and today for instance. Now, I want to declare a variable, but apparently, the variable name cannot be one of the reserved keywords. It can be any alpha followed by an alpha-numeric, with the exception of the language keywords. I did the following (testable in Pegjs Online): variable = c:(alpha alphanum*) { var keywords = ["true", "false", "if", "else", "today"]; var res = c[0] for

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 {

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

我怕爱的太早我们不能终老 提交于 2019-12-08 08:59:14
问题 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 = "dd" / "ee" I'm using https://pegjs.org/online This grammar fails to parse bdd . Line 1, column 3: Expected "a" but "d" found. Is this something which PEGs cannot do, or can I transform my grammar into something which will parse this? P.S. If I try to parse the (erroneously