当前位置:网站首页>Compilation principle - top-down analysis and recursive descent analysis construction (notes)

Compilation principle - top-down analysis and recursive descent analysis construction (notes)

2022-07-06 17:59:00 yjx23332

Purpose

Construct a top-down analysis program without backtracking

  • To eliminate the left recursion of grammar
  • Overcoming backtracking

Main idea

  • The parser consists of a set of recursive procedures , For each syntax variable ( non-terminal ) Construct a corresponding subroutine , Identify the corresponding grammatical unit
  • The recognition of input string is realized through the mutual call between subroutines
  • Such an analyzer is called a recursive descent analyzer ( Because the definition of grammar
    Usually recursive )

Process and variable

  • ADVANCE: Input string indicator IP Point to the next input symbol , That is, read a single word symbol ( Call lexical analyzer , Take down a symbol )

  • SYM:IP The currently indicated input symbol ( Obtained symbol )

  • ERROR : Error handling subroutine

example

 example
Each non terminator has the definition of the corresponding subroutine , First, in the analysis process , When you need to start from a non terminal ( deduction ) when , Call the subroutine corresponding to this non terminator .

That is to say , Each subroutine corresponds to a non terminator ( Grammatical units ), When you encounter this grammatical unit , Use the corresponding subroutine .

Such as , There is a subroutine , be responsible for E The identification of 、 deduction . This program is responsible for calling T and E’.T By the same token, such as .
Be careful , Grammar must be LL(1) Grammar can ( The action is unique ).

Templates

 Insert picture description here

1

 Insert picture description here

 Insert picture description here

2

 Insert picture description here
The first way to write it ,
 Insert picture description here
ELSE IF What we checked was , Whether it is a Terminator , Yes, it's over , Either it is an error .
another
 Insert picture description here
There is no problem with either .

The second kind , Although there is no inspection follow aggregate , But the next character will be checked first aggregate . In other words , That is, let the following program check whether the current result is correct .
This leads to , The difference between program error reporting positions .

The main program is the start character , Yes, start .
 Insert picture description here

Bakos normal form grammar

In the meta symbol “→” and “ |” On the basis of , Expand a few metalanguage symbols

Such as
 Insert picture description here

 Insert picture description here
 Insert picture description here

Drawing

 Insert picture description here

reference

[1] 《 Programming language compilation principle 》 The third edition Chen Huowang
[2] Compiling principle tutorial and courseware

原网站

版权声明
本文为[yjx23332]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060959132010.html

随机推荐