Compilation principle_P1003

1. Syntax analysis

1.1 Definition of Context Free Grammar

---- Regular expressions can define a simple language that can represent a fixed number of repetitions of a given structure or no specified number of repetitions

  Example: a (ba) 5, a (ba) *

---- Regular expressions cannot be used to describe paired or nested structures

  Example 1: A collection of paired bracket strings

  Example 2: {wcw|w is a string of a and b}

 

1.2 A context-free grammar is a quadruple (V T , V N , S, P)

  terminator set

  nonterminal set

  start symbol, one of the nonterminal symbols

  Production set, production form: A→α

parse tree

Ambiguity

There are two different views on the structure

 

2. Language and grammar

* The advantages of grammar

---- Grammar gives precise, easy-to-understand syntax descriptions

----- Automatically generate efficient analyzers

----- The language can be given to define the hierarchy

----- Grammar-based language implementation facilitates language modification

* Grammar issues

---- Grammar can only describe most of the grammar of programming languages, but cannot describe the context-dependent grammatical features in the language

 

2.1 Comparison of Regular Form and Context-Free Grammars

 

2.2 Reasons for separating lexers

* Why use regular form to define lexical

---- Lexical rules are very simple, no need to use context-free grammar

---- For lexical notation, the formal description is brief and easy to understand

From a software engineering point of view, the separation of lexical analysis and syntax analysis has the following benefits

---- Simplified design

----- The efficiency of the compiler will be improved

---- Compiler portability enhancement

---- Facilitates the module division of the front section of the compiler

* Whether to combine lexical analysis with grammatical analysis, and perform grammatical analysis directly from the character stream

---- If lexical analysis and grammatical analysis are combined, language comments and blank rules must be reflected in the grammar, and the grammar will be greatly complicated

---- An analyzer that handles comments and whitespace by itself is much more complex than an analyzer that has comments and whitespace removed by the lexer

 

 2.3 Verification of the language produced by the grammar

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325104476&siteId=291194637