The difference between Chomsky type 3 and Chomsky type 2 grammar

三世轮回 提交于 2019-11-27 01:23:14

问题


I'm having trouble articulating the difference between Chomsky type 2 (context free languages) and Chomsky type 3 (Regular languages).

Can someone out there give me an answer in plain English? I'm having trouble understanding the whole hierarchy thing.


回答1:


A Type II grammar is a Type III grammar with a stack

A Type II grammar is basically a Type III grammar with nesting.

Type III grammar (Regular):

Use Case - CSV (Comma Separated Values)

Characteristics:

  • can be read with a using a FSM (Finite State Machine)
  • requires no intermediate storage
  • can be read with Regular Expressions
  • usually expressed using a 1D or 2D data structure
  • is flat, meaning no nesting or recursive properties

Ex:

this,is,,"an "" example",\r\n
"of, a",type,"III\n",grammar\r\n

As long as you can figure out all of the rules and edge cases for the above text you can parse CSV.

Type II grammar (Context Free):

Use Case - HTML (Hyper Text Markup Language) or SGML in general

Characteristics:

  • can be read using a DPDA (Deterministic Pushdown Automata)
  • will require a stack for intermediate storage
  • may be expressed as an AST (Abstract Syntax Tree)
  • may contain nesting and/or recursive properties

HTML could be expressed as a regular grammar:

<h1>Useless Example</h1>
<p>Some stuff written here</p>
<p>Isn't this fun</p>

But it's try parsing this using a FSM:

<body>
  <div id=titlebar>
    <h1>XHTML 1.0</h1>
    <h2>W3C's failed attempt to enforce HTML as a context-free language</h2>
  </div>
  <p>Back when the web was still pretty boring, the W3C attempted to standardize away the quirkiness of HTML by introducing a strict specification</p
  <p>Unfortunately, everybody ignored it.</p>
</body>

See the difference? Imagine you were writing a parser, you could start on an open tag and finish on a closing tag but what happens when you encounter a second opening tag before reaching the closing tag?

It's simple, you push the first opening tag onto a stack and start parsing the second tag. Repeat this process for as many levels of nesting that exist and if the syntax is well-structured, the stack can be un-rolled one layer at a time in the opposite level that it was built

Due to the strict nature of 'pure' context-free languages, they're relatively rare unless they're generated by a program. JSON, is a prime example.

The benefit of context-free languages is that, while very expressive, they're still relatively simple to parse.


But wait, didn't I just say HTML is context-free. Yep, if it is well-formed (ie XHTML).

While XHTML may be considered context-free, the looser-defined HTML would actually considered Type I (Ie Context Sensitive). The reason being, when the parser reaches poorly structured code it actually makes decisions about how to interpret the code based on the surrounding context. For example if an element is missing its closing tags, it would need to determine where that element exists in the hierarchy before it can decide where the closing tag should be placed.

Other features that could make a context-free language context-sensitive include, templates, imports, preprocessors, macros, etc.

In short, context-sensitive languages look a lot like context-free languages but the elements of a context-sensitive languages may be interpreted in different ways depending on the program state.

Disclaimer: I am not formally trained in CompSci so this answer may contain errors or assumptions. If you asked me the difference between a terminal and a non-terminal you'll earn yourself a blank stare. I learned this much by actually building a Type III (Regular) parser and by reading extensively about the rest.




回答2:


The wikipedia page has a good picture and bullet points.

Roughly, the underlying machine that can describe a regular language does not need memory. It runs as a statemachine (DFA/NFA) on the input. Regular languages can also be expressed with regular expressions.

A language with the "next" level of complexity added to it is a context free language. The underlying machine describing this kind of language will need some memory to be able to represent the languages that are context free and not regular. Note that adding memory to your machine makes it a little more powerful, so it can still express languages (e.g. regular languages) that didn't need the memory to begin with. The underlying machine is typically a push-down automaton.




回答3:


Type 3 grammars consist of a series of states. They cannot express embedding. For example, a Type 3 grammar cannot require matching parentheses because it has no way to show that the parentheses should be "wrapped around" their contents. This is because, as Derek points out, a Type 3 grammar does not "remember" anything about the previous states that it passed through to get to the current state.

Type 2 grammars consist of a set of "productions" (you can think of them as patterns) that can have other productions embedded within them. Thus, they are recursively defined. A production can only be defined in terms of what it contains, and cannot "see" outside of itself; this is what makes the grammar context-free.



来源:https://stackoverflow.com/questions/11549271/the-difference-between-chomsky-type-3-and-chomsky-type-2-grammar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!