常规语法与上下文无关语法

发布于 2024-07-13 23:49:14 字数 269 浏览 6 评论 0原文

我正在学习计算语言测试,有一个想法我无法理解。

我知道常规语法更简单,不能包含歧义,但无法完成编程语言所需的许多任务。 我还了解到,上下文无关语法允许歧义,但允许编程语言必需的一些东西(例如回文)。

我遇到的困难是理解如何通过知道常规语法非终结符可以映射到一个终结符或一个非终结符后跟一个终结符或一个上下文无关的非终结符映射来导出上述所有内容终结符和非终结符的任意组合。

有人可以帮我把所有这些放在一起吗?

I'm studying for my computing languages test, and there's one idea I'm having problems wrapping my head around.

I understood that regular grammars are simpler and cannot contain ambiguity, but can't do a lot of tasks that are required for programming languages. I also understood that context-free grammars allow ambiguity, but allow for some things necessary for programming languages (like palindromes).

What I'm having trouble with is understanding how I can derive all of the above by knowing that regular grammar nonterminals can map to a terminal or a nonterminal followed by a terminal or that a context-free nonterminal maps to any combination of terminals and nonterminals.

Can someone help me put all of this together?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

最佳男配角 2024-07-20 23:49:14

常规语法是右线性或左线性的,而上下文无关语法基本上是终结符和非终结符的任意组合。 因此,您可以看到常规语法是上下文无关语法的子集。

例如,对于回文,其形式为,

S->ABA
A->something
B->something

您可以清楚地看到回文不能用常规语法表达,因为它需要是右线性或左线性的,因此两侧不能有非终结符。

由于正则语法是非二义性的,因此对于给定的非终结符只有一个产生规则,而在上下文无关语法的情况下可以有多个规则。

Regular grammar is either right or left linear, whereas context free grammar is basically any combination of terminals and non-terminals. Hence you can see that regular grammar is a subset of context-free grammar.

So for a palindrome for instance, is of the form,

S->ABA
A->something
B->something

You can clearly see that palindromes cannot be expressed in regular grammar since it needs to be either right or left linear and as such cannot have a non-terminal on both side.

Since regular grammars are non-ambiguous, there is only one production rule for a given non-terminal, whereas there can be more than one in the case of a context-free grammar.

隐诗 2024-07-20 23:49:14

我认为你要考虑的是各种泵引理。 常规语言可以被有限自动机识别。 上下文无关语言需要一个堆栈,而上下文相关语言需要两个堆栈(这相当于说它需要一个完整的图灵机。)

因此,如果我们考虑 正则语言的泵引理,它本质上说的是任何正则语言都可以分解为三部分,xyz,其中语言的所有实例都在 xy*z 中(其中 * 是 Kleene 重复,即 0 或更多y 的副本。)您基本上有一个可以扩展的“非终结符”。

现在,上下文无关语言怎么样? 有一个类似的上下文无关语言的泵引理,它将语言中的字符串分成五个部分,uvxyz,并且该语言的所有实例都在 uvixyiz 中,其中 i ≥ 0. 现在,您有两个可以复制或泵送的“非终结符”,只要您具有相同的数字

I think what you want to think about are the various pumping lemmata. A regular language can be recognized by a finite automaton. A context-free language requires a stack, and a context sensitive language requires two stacks (which is equivalent to saying it requires a full Turing machine.)

So, if we think about the pumping lemma for regular languages, what it says, essentially, is that any regular language can be broken down into three pieces, x, y, and z, where all instances of the language are in xy*z (where * is Kleene repetition, ie, 0 or more copies of y.) You basically have one "nonterminal" that can be expanded.

Now, what about context-free languages? There's an analogous pumping lemma for context-free languages that breaks the strings in the language into five parts, uvxyz, and where all instances of the language are in uvixyiz, for i ≥ 0. Now, you have two "nonterminals" that can be replicated, or pumped, as long as you have the same number.

少跟Wǒ拽 2024-07-20 23:49:14

常规语法和上下文无关语法之间的区别:
(N, Σ, P, S) :终结符、非终结符、产生式、起始状态 终结符

● 由形式语法定义的语言的基本符号

● abc

非终结符(或句法变量)

● 根据以下规则被终结符组替换产生式规则

● ABC

正则语法:右正则或左正则语法
正确的正则语法,所有规则都遵循以下形式

  1. B → a,其中 B 是 N 中的非终结符,a 是 Σ B → aC 中的终结符,
  2. 中,
  3. 其中 B 和 C 位于 N 中,a 位于 Σ B → ε 其中 B位于 N 中,ε 表示空字符串,即长度为 0 的字符串

左正则语法,所有规则遵循形式

  1. 中的终结符
  2. A → a 其中 A 是 N 中的非终结符,a 是 Σ A → Ba 其中 A 和 B 在 N 中,a 在 Σ
  3. A → ε 其中 A 在 N 中,ε 是空字符串

上下文无关语法 (CFG)

○ 正式语法,其中每个产生式规则都是形式为 V → w

○ V 是单个非终结符

○ w 是终结符和/或非终结符字符串(w 可以为空)

The difference between regular and context free grammar:
(N, Σ, P, S) : terminals, nonterminals, productions, starting state Terminal symbols

● elementary symbols of the language defined by a formal grammar

● abc

Nonterminal symbols (or syntactic variables)

● replaced by groups of terminal symbols according to the production rules

● ABC

regular grammar: right or left regular grammar
right regular grammar, all rules obey the forms

  1. B → a where B is a nonterminal in N and a is a terminal in Σ
  2. B → aC where B and C are in N and a is in Σ
  3. B → ε where B is in N and ε denotes the empty string, i.e. the string of length 0

left regular grammar, all rules obey the forms

  1. A → a where A is a nonterminal in N and a is a terminal in Σ
  2. A → Ba where A and B are in N and a is in Σ
  3. A → ε where A is in N and ε is the empty string

context free grammar (CFG)

○ formal grammar in which every production rule is of the form V → w

○ V is a single nonterminal symbol

○ w is a string of terminals and/or nonterminals (w can be empty)

对岸观火 2024-07-20 23:49:14

正则语法:- 包含如下产生式的语法是 RG:

V->TV or VT
V->T

其中 V=变量且 T=终端

RG 可以是左线性语法或右线性语法,但不是中线性语法。

我们知道所有 RG 都是线性文法,但只有左线性或右线性文法是 RG。

常规语法可能是二义性的。

S->aA|aB
A->a
B->a

歧义语法:- 对于字符串 x,它们存在多个 LMD 或多个 RMD 或多个解析树或一个 LMD 和一个 RMD,但都生成不同的解析树。

                S                   S

              /   \               /   \
             a     A             a     B
                    \                   \
                     a                   a

这个语法是二义性语法,因为有两个解析树。

CFG:-如果其产生式的形式为:DCFL:,则称为 CFG 的语法:DCFL

   V->@   where @ belongs to (V+T)*

:- 正如我们所知,所有 DCFL 都是 LL(1) 语法并且所有 LL(1)是 LR(1) 所以它永远不会有歧义。 所以DCFG是Never be ambigful。

我们还知道所有 RL 都是 DCFL,因此 RL 永远不会含糊不清。 请注意,RG 可能不明确,但 RL 则不然。

CFL: CFL 可能含糊,也可能不含糊。

注意: RL 本质上永远不会含糊不清。

Regular grammar:- grammar containing production as follows is RG:

V->TV or VT
V->T

where V=variable and T=terminal

RG may be Left Linear Grammar or Right Liner Grammar, but not Middle linear Grammar.

As we know all RG are Linear Grammar but only Left Linear or Right Linear Grammar are RG.

A regular grammar can be ambiguous.

S->aA|aB
A->a
B->a

Ambiguous Grammar:- for a string x their exist more than one LMD or More than RMD or More than one Parse tree or One LMD and One RMD but both Produce different Parse tree.

                S                   S

              /   \               /   \
             a     A             a     B
                    \                   \
                     a                   a

this Grammar is ambiguous Grammar because two parse tree.

CFG:- A grammar said to be CFG if its Production is in form:

   V->@   where @ belongs to (V+T)*

DCFL:- as we know all DCFL are LL(1) Grammar and all LL(1) is LR(1) so it is Never be ambiguous. so DCFG is Never be ambiguous.

We also know all RL are DCFL so RL never be ambiguous. Note that RG may be ambiguous but RL not.

CFL: CFl May or may not ambiguous.

Note: RL never be Inherently ambiguous.

美人迟暮 2024-07-20 23:49:14

正则表达式

  • 词法分析基础
  • 表示常规语言

上下文无关语法 解析

  • 基础
  • 表示语言结构

在此处输入图像描述

Regular Expressions

  • Basis of lexical analysis
  • Represent regular languages

Context Free Grammars

  • Basis of parsing
  • Represent language constructs

enter image description here

温折酒 2024-07-20 23:49:14

如果所有产生式规则都具有以下形式,则语法是上下文无关的:A(即规则的左侧只能是单个变量;右侧不受限制,可以是任何终结符和变量序列)。
我们可以将语法定义为 4 元组,其中 V 是有限集(变量),_ 是有限集(终结点),S 是起始变量,R 是有限规则集,其中每个规则都是一个映射V
常规语法是右线性或左线性的,而上下文无关语法基本上是终结符和非终结符的任意组合。 因此我们可以说正则语法是上下文无关语法的子集。
在这些属性之后,我们可以说上下文无关语言集还包含常规语言集

A grammar is context-free if all production rules have the form: A (that is, the left side of a rule can only be a single variable; the right side is unrestricted and can be any sequence of terminals and variables).
We can define a grammar as a 4-tuple where V is a finite set (variables), _ is a finite set (terminals), S is the start variable, and R is a finite set of rules, each of which is a mapping V
regular grammar is either right or left linear, whereas context free grammar is basically any combination of terminals and non-terminals. hence we can say that regular grammar is a subset of context-free grammar.
After these properties we can say that Context Free Languages set also contains Regular Languages set

念﹏祤嫣 2024-07-20 23:49:14

基本上,正则语法是上下文无关语法的子集,但我们不能说每个上下文无关语法都是正则语法。 主要是上下文无关语法是二义性的,而常规语法也可能是二义性的。

Basically regular grammar is a subset of context free grammar,but we can not say Every Context free grammar is a regular grammar. Mainly context free grammar is ambiguous and regular grammar may be ambiguous.

作妖 2024-07-20 23:49:14

常规语法永远不会含糊,因为它要么是左线性的,要么是右线性的,所以我们不能为常规语法制作两个决策树,所以它总是明确的。但除了常规语法之外,所有语法都可能是或可能不是常规的

a regular grammer is never ambiguous because it is either left linear or right linear so we cant make two decision tree for regular grammer so it is always unambiguous.but othert than regular grammar all are may or may not be regular

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文