peg

Rule precedence issue with grako

情到浓时终转凉″ 提交于 2019-12-01 09:23:22
I'm redoing a minilanguage I originally built on Perl (see Chessa# on github ), but I'm running into a number of issues when I go to apply semantics. Here is the grammar : (* integers *) DEC = /([1-9][0-9]*|0+)/; int = /(0b[01]+|0o[0-7]+|0x[0-9a-fA-F]+)/ | DEC; (* floats *) pointfloat = /([0-9]*\.[0-9]+|[0-9]+\.)/; expfloat = /([0-9]+\.?|[0-9]*\.)[eE][+-]?[0-9]+/; float = pointfloat | expfloat; list = '[' @+:atom {',' @+:atom}* ']'; (* atoms *) identifier = /[_a-zA-Z][_a-zA-Z0-9]*/; symbol = int | float | identifier | list; (* functions *) arglist = @+:atom {',' @+:atom}*; function =

Rule precedence issue with grako

妖精的绣舞 提交于 2019-12-01 06:10:53
问题 I'm redoing a minilanguage I originally built on Perl (see Chessa# on github), but I'm running into a number of issues when I go to apply semantics. Here is the grammar: (* integers *) DEC = /([1-9][0-9]*|0+)/; int = /(0b[01]+|0o[0-7]+|0x[0-9a-fA-F]+)/ | DEC; (* floats *) pointfloat = /([0-9]*\.[0-9]+|[0-9]+\.)/; expfloat = /([0-9]+\.?|[0-9]*\.)[eE][+-]?[0-9]+/; float = pointfloat | expfloat; list = '[' @+:atom {',' @+:atom}* ']'; (* atoms *) identifier = /[_a-zA-Z][_a-zA-Z0-9]*/; symbol =

Parsing of optionals with PEG (Grako) falling short?

北战南征 提交于 2019-12-01 03:52:54
问题 My colleague PaulS asked me the following: I'm writing a parser for an existing language (SystemVerilog - an IEEE standard), and the specification has a rule in it that is similar in structure to this: cover_point = [[data_type] identifier ':' ] 'coverpoint' identifier ';' ; data_type = 'int' | 'float' | identifier ; identifier = ?/\w+/? ; The problem is that when parsing the following legal string: anIdentifier: coverpoint another_identifier; anIdentifier matches with data_type (via its

PEG for Python style indentation

匆匆过客 提交于 2019-11-28 16:51:34
How would you write a Parsing Expression Grammar in any of the following Parser Generators ( PEG.js , Citrus , Treetop ) which can handle Python/Haskell/CoffeScript style indentation: Examples of a not-yet-existing programming language: square x = x * x cube x = x * square x fib n = if n <= 1 0 else fib(n - 2) + fib(n - 1) # some cheating allowed here with brackets Update: Don't try to write an interpreter for the examples above. I'm only interested in the indentation problem. Another example might be parsing the following: foo bar = 1 baz = 2 tap zap = 3 # should yield (ruby style hashmap): #

Antlr4 how to build a grammar allowed keywords as identifier

人走茶凉 提交于 2019-11-28 08:02:14
问题 This is a demo code label: var id let id = 10 goto label If allowed keyword as identifier will be let: var var let var = 10 goto let This is totally legal code. But it seems very hard to do this in antlr. AFAIK, If antlr match a token let, will never fallback to id token. so for antlr it will see LET_TOKEN : VAR_TOKEN <missing ID_TOKEN>VAR_TOKEN LET_TOKEN <missing ID_TOKEN>VAR_TOKEN = 10 although antlr allowed predicate, I have to control ever token match and problematic. grammar become this

PEG for Python style indentation

China☆狼群 提交于 2019-11-27 09:51:35
问题 How would you write a Parsing Expression Grammar in any of the following Parser Generators (PEG.js, Citrus, Treetop) which can handle Python/Haskell/CoffeScript style indentation: Examples of a not-yet-existing programming language: square x = x * x cube x = x * square x fib n = if n <= 1 0 else fib(n - 2) + fib(n - 1) # some cheating allowed here with brackets Update: Don't try to write an interpreter for the examples above. I'm only interested in the indentation problem. Another example