惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
U
Unit 42
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
J
Java Code Geeks
D
DataBreaches.Net
B
Blog RSS Feed
D
Docker
L
LangChain Blog
aimingoo的专栏
aimingoo的专栏
F
Fortinet All Blogs
Y
Y Combinator Blog
A
About on SuperTechFans
V
V2EX
罗磊的独立博客
WordPress大学
WordPress大学
宝玉的分享
宝玉的分享
MongoDB | Blog
MongoDB | Blog
博客园 - 【当耐特】
Last Week in AI
Last Week in AI
S
SegmentFault 最新的问题
月光博客
月光博客
Vercel News
Vercel News
H
Hackread – Cybersecurity News, Data Breaches, AI and More
阮一峰的网络日志
阮一峰的网络日志

Victrid's Personal Site

斯普拉遁3打工模式联机网络过程分析 DHCP无类型静态路由──一个摆设 配置透明代理,实现无感上网 LaTeX插入其他PDF中的矢量图 光速不变原理和迈克尔孙──莫雷实验 交大葡萄演义 调试微信内置浏览器 欧悌甫戎篇 苏格拉底的申辩篇 LCA 最近公共祖先 弥罗斯人的辩论 埃斯库罗斯作品:波斯人、阿伽门农 电路理论资料 Coronavirus: A Humanitarian Crisis 雨夜 linux配置SSR 二叉堆的实现 懒政 Placement New Flipped: A Love Story 我宁可呆在这样的疯人院里 谈新发本地病例 勇敢与智慧 Powerful but Limited Drafted VLC Libva Error Troubleshooting Ring-Fit新上手 政治艺术化与艺术政治化 动态规划——从分割等和子集入手 被背叛的革命 (1)
Generating Unlimited Grammar: A Messenger Perspective
Victrid · 2021-11-05 · via Victrid's Personal Site

Chomsky hierarchy describes a containment hierarchy of classes of formal grammars. Lower level of languages, like regular and context-free, are well studied, and used in multiple practical fields, like regular expressions, and programming languages parsing.

Most textbooks introduce how to construct deterministic finite automaton from regular expressions, and some textbooks focusing on compilers will present LL, LR, LALR, etc. to show how a subset of context-free grammars are transferred to parsers. These topic are well researched, and if you use bison, it will even show you how reduce-reduce, shift-reduce conflicts are happening by examples.

When the problem comes to higher classes in the Chomsky hierarchy, the problem becomes difficult. C++ has a context-sensitive grammar, and GCC have to use a hand-written parser for it. Most textbooks will stop at context-free grammar, and teach you to use the pumping theorem to prove that some languages are not context-free.

For these languages, it’s even difficult to describe them with a correct grammar! Take $a^n b^n c^n, n \ge 0$ as an example (A popular and easy non-context-free language, commonly used to demonstrate pumping lemma). There are no definitive algorithm to guarantee a grammar for it.

Sentinel and Combination

A common solution for this can be sentinel and combination. In our example, we can first combine the last two part as $a^n (BC)^n$, which can be generated as context-free grammar, and with the sentinel $CB \rightarrow BC$, arrays like $BCBCBC\cdots$ can be derived as $BBB\cdots CCC$.

This can be obscure, if the part increases. We need to either combine recursively, or create larger sentinels to do the part. When the part is not definitive, the problem cannot be solved.

Take this problem as another example:

Let $\Sigma = \{1\}$ and $L_{\text{square}} := \{1^n \mid n \text{ is a perfect square}\}$. So $\epsilon, 1, 1111, 111111111$ are in $L$ but $11, 11111$ are not. Give an unrestricted grammar generating $L_{\text{square}}$.

Square numbers can be expanded to $1 + 2 + \cdots + (n-1) + n + (n-1) + \cdots + 2 + 1$. If we take this route, the problem could be difficult to solve by the combine method, as the items in the sequence increases with $n$.

Now we show a new perspective to view the problem. Taking the string as a queue, and messengers moving along them between head and tail. When certain situations are met, messengers will change the sequence, otherwise they’ll move through the queue.

This method will have longer reductions, but it will be easier and more intuitive to construct, and having less rules.

In the second problem, we separate the 1’s with 0, to specify a border for messengers to recognize. And we add symbols [ and ] to indicate the head and tail. A state in the reduction could be [01011010].

We can have 2 messengers to solve the problem.

  • messenger A, from head to tail:
    • insert new groups, i.e. 010, at the head and tail.
    • increase inner groups’ 1 number
  • messenger B, from head to tail:
    • remove all zeros, head and tail symbol, to reformat the string into the final look.

With the border of each group, the recognition field, i.e. the range it looks to derive uniquely, decides the size of its rule set. We here define these messengers with grammar:

$$\begin{align*} [ &\rightarrow [01A \\ A0 &\rightarrow 01A \\ A1 &\rightarrow 1A \\ A] &\rightarrow 0] \\ \end{align*}$$

and $B$:

$$\begin{align*} [ &\rightarrow B \\ B0 &\rightarrow B \\ B1 &\rightarrow 1B \\ B] &\rightarrow \epsilon \\ \end{align*}$$

and the start symbol: $S \rightarrow []$. Only 5 variables and 9 rules is needed.

Exercise

  1. Give the unlimited grammar generating language in the first problem.

    Hint: You may need to deal with situations when two messengers meet, causing a deadlock. (e.g. Emulate a lock by changing the head symbol to allow at most one messenger traveling in the sequence).

  2. Let $\Sigma = \{1\}$ and language $L := \{1^k \mid k \text{ is prime}\}$. Give an unlimited grammar generating $L$.

    Hint: Here is a not-so-clever path doing it:

    • list all numbers by increasing order
    • use Eratosthenes Sieve to filter primes
    • select one and remove others

    Not only head symbols can send messengers, and the direction are not limited from head to tail.

    You may change the head and tail symbol to indicate stages.

    You may need to deal with situations when messengers in different directions meet, causing a deadlock. (e.g. by explicitly swap them with $AB \rightarrow BA$).

Ads by Google

Read our privacy policy on how these personalized advertisements are delivered to you.

For your reading experience, we provide full-text RSS feeds. Although math formulas cannot be displayed well, the interface can be adjusted as you like and there are no ads.