grammar

Raku: effect of capture markers is lost “higher up”

断了今生、忘了曾经 提交于 2020-08-27 07:28:39
问题 The following Raku script: #!/usr/bin/env raku use v6.d; grammar MyGrammar { rule TOP { <keyword> '=' <value> } token keyword { \w+ } token value { <strvalue> | <numvalue> } token strvalue { '"' <( <-["]>* )> '"' } token numvalue { '-'? \d+ [ '.' \d* ]? } } say MyGrammar.parse('foo = 42'); say MyGrammar.parse('bar = "Hello, World!"'); has the following output: 「foo = 42」 keyword => 「foo」 value => 「42」 numvalue => 「42」 「bar = "Hello, World!"」 keyword => 「bar」 value => 「"Hello, World!"」

Using Visitors in AntLR4 in a Simple Integer List Grammar

左心房为你撑大大i 提交于 2020-08-05 05:50:10
问题 I'm a newbie in AntLR. I'm using AntLR4 version. I wrote the following attribute grammar that recognizes a list of integers and print the sum of the list at the end. list.g4 grammar list; @header { import java.util.List; import java.util.ArrayList; } list : BEGL (elems[new ArrayList<Integer>()])? ENDL { int sum = 0; if($elems.text != null) for(Integer i : $elems.listOut) sum += i; System.out.println("List Sum: " + sum); } ; elems [List<Integer> listIn] returns [List<Integer> listOut] : a=elem

What's the real difference between a token and a rule?

醉酒当歌 提交于 2020-08-03 14:20:14
问题 I was drawn to Raku due to its built-in grammars and figured I'd play around with it and write a simple email address parser, only problem: I couldn't get it to work. I tried countless iterations before landing on something that actually works, and I'm struggling to understand why. All it boiled down to, was changing token to rule . Here's my example code: grammar Email { token TOP { <name> '@' [<subdomain> '.']* <domain> '.' <tld> } token name { \w+ ['.' \w+]* } token domain { \w+ } token

LL(2) language that is not LL(1)

僤鯓⒐⒋嵵緔 提交于 2020-05-25 11:24:50
问题 In order to further my understanding of parsers and grammars, I'm searching for a (hopefully simple) example of a language that is LL(2) but not LL(1). That is, a language that can be generated by an LL(2) grammar but not by any LL(1) grammar. Are there useful languages in that class ? I.e could we imagine a computer language that is LL(2) but not LL(1) ? 回答1: Parsing Techniques by Grune and Jacobs presents an example. An older version of this book is available online at http://dickgrune.com

In Rust, is “as” an operator?

坚强是说给别人听的谎言 提交于 2020-05-14 18:37:06
问题 The Rust Reference presently says the following about the as operator: 7.2.12.5 Type cast expressions A type cast expression is denoted with the binary operator as . Executing an as expression casts the value on the left-hand side to the type on the right-hand side. An example of an as expression: fn average(values: &[f64]) -> f64 { let sum: f64 = sum(values); let size: f64 = len(values) as f64; sum / size } (Also, since it will be relevant: 7.2.12.8 Operator precedence The precedence of Rust

Algorithm for computing FIRST and FOLLOW sets for context-free grammars [closed]

主宰稳场 提交于 2020-05-11 06:40:30
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 2 years ago . I need an algorithm to computing FIRST and FOLLOW sets for a grammar. Is there a simple algorithm or simple code for computing these? 回答1: The standard algorithm for computing FIRST and FOLLOW sets is discussed in most compiler textbooks and books on parsing algorithms. I would be

Parse::RecDescent grammar not working as expected

生来就可爱ヽ(ⅴ<●) 提交于 2020-02-24 08:59:00
问题 All I managed to get working is STRING, PARAMS, VARIABLE and FUNCNAME There seems to be a problem with FUNCTION, but I just can't see it. use strict; use Parse::RecDescent; $::RD_ERRORS = 1; # Make sure the parser dies when it encounters an error $::RD_WARN = 1; # Enable warnings. This will warn on unused rules &c. $::RD_HINT = 1; # Give out hints to help fix problems. my $grammar = <<'_GRAMMAR_'; SCRIPT : INSTRUCTION(s /;/) INSTRUCTION: FUNCTION | VARIABLE "=" FUNCTION FUNCTION : FUNCNAME "[