Noam Chomsky 将语法分为四种类型:
Type |
Name |
0 |
Unrestricted Grammar |
1 |
Context Sensitive Grammar |
2 |
Context Free Grammar |
3 |
Regular Grammar |
1. 上下文无关语法:
- Pushdown Automata 接受 Context Free Grammar 生成的语言
- 它是类型 0 和类型 1 语法的子集以及类型 3 语法的超集。
- 也称为阶段结构语法。
- 不同的上下文无关文法可以生成相同的上下文无关语言。
- Context Free Grammar 的分类是根据解析树的数量来完成的。
- 只有一棵解析树-> 明确。
- 不止一个解析树->二义性。
制作的形式是——
A->B;
A∈N i.e A is a non-terminal.
B∈V*(Any string).
例子 –
S –> AB
A –> a
B –> b
2. 常规语法:
- 它被有限状态自动机接受。
- 它是类型 0 、类型 1 和类型 2 语法的子集。
- 它生成的语言称为正则语言。
- 常规语言在 Union、Intersection、Complement 等操作下被关闭。
- 它们是最受限制的语法形式。
制作的形式是——
V –> VT / T (left-linear grammar)
(or)
V –> TV /T (right-linear grammar)
例子 –
1. S –> ab.
2. S -> aS | bS | ∊
上下文无关语法和常规语法的区别:
Parameter | Context Free Grammar | Regular Grammar |
Type | Type-2 | Type-3 |
Recognizer | Push-down automata. | Finite State Automata |
Rules | Productions are of the form: A->B; A∈N(Non-Terminal) B∈V*(Any string) |
Productions are of the form: V –> VT / T (left-linear grammar) (or) V –> TV /T (right-linear grammar) |
Restriction | Less than Regular Grammar | More than any other grammar |
Right-hand Side | The right-hand side of production has no restrictions. | The right-hand side of production should be either left linear or right linear. |
Set Property | Super Set of Regular Grammar | Subset of Context Free Grammar |
Intersection | Intersection of two CFL need not be a CFL | Intersection of two RG is a RG. |
Complement | They are not closed under complement | Closed under complement |
Range | The range of languages that come under CFG is wide. | The range of languages that come under RG is less than CFG. |
Examples | S –> AB;A –> a;B –> b | S -> aS | bS | ∊ |