About regular expressions, regular syntax, NFA, LR (1)

Source: Internet
Author: User

Yesterday andSumtecWhen talking about automation and syntax analysis, I suddenly had a bit of confusion in my mind and confused some concepts. After reading Tsinghua's compilation books for a long time, I didn't fully understand them....This morning, I read part of the automatic machine in discrete mathematics and Its Application to clarify the clues. It is also a clear explanation of books from foreigners.

 

YesterdayNFAAnd in syntax analysisLL (1) LR (1)Confused. In factLL (1)AnalysisLR (1)Analysis is good. It uses a computing model based on the push-down mechanism rather than the finite one. The computing capability of the push-down automatic machine is stronger than that of the finite automatic machine.

 

SecondNFAAndDFAIs indeed equivalent, that is, for anyNFAYou can find an equivalentDFA (you can use the subset method to construct such a DFA).

To describe the relationship between finite automaton andLL (1) LR (1)First, we will outline the classification of grammar.

 

There are four types of grammar:

(1)Phrase Grammar(0Grammar)

(2)Context Grammar(1Grammar)

(3)Context-independent Grammar(2Grammar)

(4)Regular(Then)Grammar(3Grammar)

 

The relationships between the above four types of grammar are as follows,1Grammar is0A subset of grammar,2Grammar is1A subset of grammar,, 3Grammar is2A subset of grammar.

 

Our main research2Type and3Type method.

 

Type 3 grammar (regular syntax) and regular expression(Regular expression)It is equivalent. Any regular expression can always be converted into an equivalent regular expression. At the same time, regular expressions are equivalent to finite automaton. A language that can be recognized by Finite Automation must be expressed by a regular expression, while a language that is represented by a regular expression must be recognized by a Finite Automation.

 

But the regular syntax is insufficient to describeProgramDesign Language ( For example, you cannot use regular expressions to define mathematical expressions with parentheses. ) Popular programming languages such C #, Java And so on. 2 It is defined by context-independent grammar. Therefore, finite automatic machines cannot identify programming languages. ( Finally, I will give an example. ) . Therefore, the model of the push-down mechanism is proposed. Push down all the parts of the limited automatic machine, such as the State and State transfer table. At the same time, it has one more stack than the finite automatic machine, which is often referred to as the computing stack. The push-down machine can push the terminator or non-terminator to the stack based on the situation, or pop up the stack.

 

WhileLL (1),LR (1)The equals analysis method is used to analyze context-independent grammar, based on the model of the push-down machine. This is why all the books are based onLR (1)The prediction Analyzer of analytics is composed of three parts: State transfer table, Controller, and computing stack. The so-called transfer and reduction are the problems of the inbound and outbound stacks.

 

Finally, let's give an example.(It seems that everyone is asleep.-_-B)

 

First, give a Syntax:

 

S-> 0s1 | 01

 

Where0,1Is the Terminator.

 

Such a grammar description language is actuallyNItems0Add equal numberNItems1, HereNIs an integer.

 

This syntax is a context-independent syntax, but not a regular syntax. Therefore, we cannot write a regular expression to describe such a language. Like an NFA that can recognize any sentence in this grammar, we can always find such a sentence, which is not defined by this grammar but accepted by this NFA. In other words, any NFA cannot be used to determine whether a sentence is defined by the above syntax. Actually, if you want to write a coloring program, the input file is a string 0 , 1 The sequence that consists N Items 0 Add N Items 1 The sequence is colored in red, and other black words, we cannot simply use regular expression matching to complete this task.

 

If the above example is still abstract, there is no way to use regular expressions to match the language "mathematical expressions with parentheses. Because the syntax used to describe "mathematical expressions with parentheses" is not a regular syntax, because it contains a syntax similar:

F-> (E)

Such a part.

The regular expression syntax requires that all the generative forms must be a-> AB or a-> A (where A and B are non-terminologies, and A is a terminator)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.