简体   繁体   English

C语言中的上下文无关文法

[英]Context-free grammar in C

I have an assignment to make a program in C that displays a number (n < 50) of valid, context-free grammar strings using the following context-free grammar: 我分配了一个程序,用C编写一个程序,该程序使用以下上下文无关的语法显示许多(n <50)个有效的上下文无关的语法字符串:

S -> AA|0
A -> SS|1

I had few concepts of how to do it, but after analyzing them more and more, none of them were right. 我几乎没有关于如何执行此操作的概念,但是在对它们进行越来越多的分析之后,它们都不对。

For now, I'm planning to make an array and randomly change [..., A, ...] for [..., S, S, ...] or [..., 1, ...] until there are only 0s and 1s and then check whether the same thing was already randomly generated. 目前,我正计划制作一个数组,并随机更改[..., S, S, ...][..., 1, ...] [..., A, ...] [..., 1, ...]直到只有0和1,然后检查是否已经随机生成了相同的事物。

I'm still not convinced if that is the right approach, and I still don't know exactly how to do that or where to keep the final words because the basic form will be an array of chars of different length. 我仍然不确定这是否是正确的方法,而且我仍然不确切地知道该怎么做或在哪里保留最终用语,因为基本形式将是不同长度的字符数组。 Also, in C, is a two dimensional array of chars equal to an array of strings? 另外,在C中,二维字符数组等于字符串数组吗?

Does this make any sense, and is it a proper way to do it? 这有任何意义吗,这是正确的方法吗? Or am I missing something? 还是我错过了什么?

You can simply make a random decision every time you need to decide on something. 您可以在每次需要决定某件事时简单地做出随机决定。 For example: 例如:

function A():
  if (50% random chance)
    return "1"
  else
    return concat(S(), S())

function S():
  if (50% random chance)
    return "0"
  else
    return concat(A(), A())

Calling S() multiple times give me these outputs: 多次调用S()给我这些输出:

"0"
"00110110100100101111010111111111001111101011100100011000000110101110000110101110
 10001000110001111100011000101011000001101111000110110011101010111111111011010011
 10000000101111100100011011010000000101000111110010001000101001100110100111111111
 1001010011"
"11"
"10010010101111010111101"

All valid strings for your grammar. 语法的所有有效字符串。 Note that you may need to tweak a little the random chances. 请注意,您可能需要调整一些随机机会。 This sample has a high probability to generate very small strings like "11" . 该样本极有可能生成非常小的字符串,如"11"

Try to think of the context-free grammar as a set of rules that allow you to generate new strings in a language. 尝试将无上下文语法视为一组规则,使您可以使用某种语言生成新的字符串。 For example, the first rule: 例如,第一个规则:

S -> AA | 0

How could you generate a word S in this language? 您如何用这种语言生成单词S? One way is with a function that generates, at random, either the string "0" or two A words, concatenated. 一种方法是使用随机生成字符串“ 0”或两个串联的A字的函数。

Similarly, to implement the second rule: 同样,要实施第二条规则:

A -> SS | 1

write a function that generates, at random, either "1" or two S words concatenated. 编写一个函数,该函数随机生成“ 1”或两个串联的S字。

You asked several questions... 你问了几个问题...
Regarding The question: BTW in C, is two dimensional array of chars equal to array of strings? 关于问题: C中的BTW,二维字符数组等于字符串数组吗?

Yes. 是。

Here are ways to declare arrays of strings, each example shows varying flexibility in terms of usage: 以下是声明字符串数组的方法,每个示例在用法方面都显示出不同的灵活性:

char **ArrayOfStrings;  //most flexible declaration - 
                        //pointer to pointer, can use `calloc()` or `malloc()` to create memory for
                        //any number of strings of any length (all strings will have same length) 

or 要么

char *ArrayOfStrings[10]; //somewhat flexible - 
                          //pointer to array of 10 strings, again can use  `c(m)alloc()` to allocate memory for 
                          //each string to have any lenth (all strings will have same length)

or 要么

ArrayOfStrings[5][10]; //Not flexible - (but still very useful)
                       //2 dimensional array of 5 strings, each with space for up to 9 chars + '\0' 
                       //Note:  In C, by definition, strings must always be NULL terminated.

Note: Although each of these forms are valid, and very useful when used correctly, It is good to be aware there are differences in the way each will behave in practice. 注意:尽管这些形式中的每一个都是有效的,并且在正确使用时非常有用,但最好注意每种形式在实践中的行为会有差异 (read the link for a good discussion on that) (请阅读链接以获取有关此内容的精彩讨论)

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

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