grammar

How do I generate sentences from a formal grammar?

人走茶凉 提交于 2019-11-27 01:37:59
问题 What's a common way of generating sentences from a grammar? I want an algorithm that's sort of the opposite of a parser. That is, given a formal context-free grammar (say LL), I want to generate an arbitrary sentence that conforms to that grammar. I use sentence here to mean any valid body of text, so it can actually be a whole program (even if it doesn't make any sense—as long as it's syntactially correct). Example grammar: program : <imports> NEWLINE? <namespace> imports : ("import"

How to determine whether a language is LL(1) LR(0) SLR(1)

半世苍凉 提交于 2019-11-27 00:55:34
问题 Is there a simple way to determine whether a grammar is LL(1), LR(0), SLR(1)... just from looking on the grammar without doing any complex analysis? For instance: To decide whether a BNF Grammar is LL(1) you have to calculate First and Follow sets - which can be time consuming in some cases. Has anybody got an idea how to do this faster? Any help would really be appreciated! 回答1: First off, a bit of pedantry. You cannot determine whether a language is LL(1) from inspecting a grammar for it,

Tips for creating “Context Free Grammar”

荒凉一梦 提交于 2019-11-27 00:29:01
I am new to CFG's, Can someone give me tips in creating CFG that generates some language For example L = {a m b n | m >= n} What I got is: S o -> a | aS o | aS 1 | e S 1 -> b | bS 1 | e but I think this area is wrong, because there is a chance that the number of b 's can be greater than a 's. Grijesh Chauhan How to write CFG with example a m b n L = {a m b n | m >= n}. Language description: a m b n consist of a followed by b where number of a are equal or more then number of b . some example strings: {^, a, aa, aab, aabb, aaaab, ab......} So there is always one a for one b but extra a are

In ANTLR, is there a shortcut notation for expressing alternation of all the permutations of some set of rules?

柔情痞子 提交于 2019-11-26 22:26:20
问题 In ANTLR I want to define a rule like this: rule : ( a b c | a c b | b a c | b c a | c a b | c b a ); But in my case I have 10 rules instead of three, that I want to permute so it gets very impractical. Is there any way of expressing this in ANTLR without having to write all the permutations? 回答1: I would just match any a , b or c once or more: rule : ( a | b | c )+ ; and then, after parsing, traversing the parse tree and checking if a , b and c all matched exactly once. But Yes, it is

Regex Grammar

狂风中的少年 提交于 2019-11-26 19:30:23
问题 Is there any BNF grammar for regular expression? 回答1: You can see one for Perl regexp (displayed a little more in detail here, as posted by edg) 回答2: To post them on-site: CMPT 384 Lecture Notes Robert D. Cameron November 29 - December 1, 1999 BNF Grammar of Regular Expressions Following the precedence rules given previously, a BNF grammar for Perl-style regular expressions can be constructed as follows. <RE> ::= <union> | <simple-RE> <union> ::= <RE> "|" <simple-RE> <simple-RE> ::=

Repository of BNF Grammars?

回眸只為那壹抹淺笑 提交于 2019-11-26 19:15:20
问题 Is there a place I can find Backus–Naur Form or BNF grammars for popular languages? Whenever I do a search I don't turn up much, but I figure they must be published somewhere. I'm most interested in seeing one for Objective-C and maybe MySQL. 回答1: you have to search on tools used to create grammars: "lex/yacc grammar", "antlr grammar" "railroad diagram" http://www.antlr3.org/grammar/list.html Here's some grammar files objective-c http://www.omnigroup.com/mailman/archive/macosx-dev/2001-March

Why the need for terminals? Is my solution sufficient enough?

自闭症网瘾萝莉.ら 提交于 2019-11-26 18:37:21
问题 I'm trying to get my head around context free grammars and I think I'm close. What is baffling me is this one question (I'm doing practise questions as I have an exam in a month's time): I've come up with this language but I believe it's wrong. S --> aSb | A | B A --> aA | Σ B --> bB | Σ Apparently this is the correct solution: S --> aSb | aA | bB A --> aA | Σ B --> bB | Σ What I don't quite understand is why we have S --> aSb | aA | bB and not just S --> aSb | A | B . What is the need for

History of trailing comma in programming language grammars

我的未来我决定 提交于 2019-11-26 16:50:30
问题 Many programming languages allow trailing commas in their grammar following the last item in a list. Supposedly this was done to simplify automatic code generation, which is understandable. As an example, the following is a perfectly legal array initialization in Java (JLS 10.6 Array Initializers): int[] a = { 1, 2, 3, }; I'm curious if anyone knows which language was first to allow trailing commas such as these. Apparently C had it as far back as 1985. Also, if anybody knows other grammar

Grammatical inference of regular expressions for given finite list of representative strings?

旧时模样 提交于 2019-11-26 16:15:01
问题 I'm working on analyzing a large public dataset with lots of verbose human-readable strings that were clearly generated by some regular (in the formal language theory sense) grammar. It's not too hard to look at sets of these strings one by one to see the patterns; unfortunately, there's about 24,000 of these unique strings broken up into 33 categories and 1714 subcategories, so it's somewhat painful to do this manually. Basically, I'm looking for an existing algorithm (preferably with an

What does this JavaScript snippet mean? [duplicate]

不羁岁月 提交于 2019-11-26 14:39:23
问题 This question already has an answer here: What is the (function() { } )() construct in JavaScript? 23 answers I have not met this type of grammar before. What does it mean? To what technique is it related? (function(fun) { })(myFunkyAlert); 回答1: This is an anonymous function that will run as soon as it is declared. Its parameter is myFunkyAlert and inside the function it will be referenced as the fun variable. The reason we usually write a function like that is to avoid conflicts, due to