简体   繁体   English

CFG S识别的单词是什么-> 1 | 0

[英]What are the words recognized by the CFG S - > 1 | S 0

I am dealing with this exercise: What are the words of我正在处理这个练习:什么是单词

S - > 1 | S 0

I find the problem particularly challenging because there are so many combinations and I have no clue these how to exhaustively enumerate all possibilities.我发现这个问题特别具有挑战性,因为有太多的组合,我不知道如何详尽地列举所有的可能性。 As a starter, the words 1, 10, 100, 10100, 100100 are some of the recognized words.首先,单词 1, 10, 100, 10100, 100100 是一些已识别的单词。

I find the problem particularly challenging because there are so many combinations and I have no clue these how to exhaustively enumerate all possibilities.我发现这个问题特别具有挑战性,因为有太多的组合,我不知道如何详尽地列举所有的可能性。

As with most grammars, an infinite set of sentences can be produced.与大多数语法一样,可以产生无限组的句子。 But there really aren't very many combinations of any given size, because:但实际上没有任何给定大小的组合,因为:

  • The grammar is linear (no production has more than one non-terminal)语法是线性的(没有产生式有多个非终结符)
  • There are only two productions.只有两部作品。

The simple way to enumerate all possible sentences is to a breadth-first search over the possible leftmost derivations, starting with a sentential form consisting only of the start symbol.枚举所有可能的句子的简单方法是对可能的最左派生进行广度优先搜索,从仅包含开始符号的句子形式开始。 (You could use rightmost derivations, but leftmost seems more natural.) (您可以使用最右边的推导,但最左边的似乎更自然。)

That's a very simple operation, particularly for a computer, although in a grammar this simple, it's easy to do by hand:这是一个非常简单的操作,特别是对于计算机来说,虽然在语法上如此简单,但手工操作很容易:

  0   S            start symbol
  1   1            in (0) replace S->1
  2   S 0          in (0) replace S->S 0
  3   1 0          in (2) replace S->1
  4   S 0 0        in (2) replace S->S 0
  ...

Here's a bit of Javascript which can generate sentences (or sentential forms if the all argument is true ) for arbitrary context-free grammars.这里有一些 Javascript,它可以为任意上下文无关语法生成句子(或句子形式,如果all参数为true )。 (The grammar parser is primitive, and still lacks error messages. But it works on correctly entered grammars. I might work on it some more tomorrow.) (语法解析器是原始的,仍然缺少错误消息。但它可以处理正确输入的语法。我明天可能会做更多的工作。)

 function Grammar(s) { let tokens = /(\\w+|"[^"]+"|'[^']*')|(:)|([|;])|(\\S)/g let which = m => m.findIndex((v, i)=>v&&i) let grammar = new Map() grammar.start = undefined let put = (lhs, rhs) => { if (grammar.has(lhs)) grammar.get(lhs).push(rhs) else grammar.set(lhs, [rhs]) } let lhs = undefined let rhs = [] for (let m of s.matchAll(tokens)) { switch (which(m)) { case 1: rhs.push(m[1]); break; /* Symbol */ case 2: if (lhs && rhs.length) put(lhs, rhs) else if (!lhs && rhs.length == 1) { if (!grammar.start) grammar.start = rhs[0] } else break; lhs = rhs.pop() rhs = [] break; case 3: if (lhs) { put(lhs, rhs) rhs = [] if (m[3] == ';') lhs = undefined } break case 4: break } } if (lhs) put(lhs, rhs); return grammar } let grammar = Grammar(` expr: atom | expr '+' expr | expr '*' expr | '-' expr atom: NUM | '(' expr ')' `) function* derivationGenerator(grammar, all) { level = [ [grammar.start] ] while (level.length) { nextLevel = [] for (const form of level) { let idx = form.findIndex(sym => grammar.has(sym)) if (idx < 0 || all) yield form if (idx >= 0) { let before = form.slice(0, idx) let after = form.slice(idx+1) grammar.get(form[idx]).forEach( rhs => nextLevel.push(before.concat(rhs, after))) } } level = nextLevel } } function take(gen, n, handle) { for (let v of gen) { handle(v) if (--n <= 0) return gen } } take(derivationGenerator(grammar), 10, form => console.log(...form))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM