• 基于Java实现的语法分析器


    语法分析器的简单实现

    一、实验目的

    通过构建一个简单的语法分析程序,进一步熟悉语法分析的过程以及加深对 First 和Follow 的认识。

    二、实验描述

    本次实验使用 Java 语言编写,简单实现了对 C 语言程序的语法分析。程序的输入是 C 语言程序代码 program.c 文件以及已经定义好的文法 CFG.txt 文件。程序的输出是控制台和 output.txt 文件,内容是语法分析的过程。本实验中用到的词法分析部分是实验一的词法分析器。

    三、实验方法

    本次实验选用的方法是 PPT 中给出的第一种方法中的 a 方法,根据文法编程构建 LL(1) 预测分析表来进行语法分析。

    实验设想

    无。

    相关的自动机描述

    大部分同词法分析。

    四、数据结构的定义

    Lex 类:词法分析程序

    Token 类:Token 序列中的元素

    Production 类:产生式类,包括两个属性,一个是 left(String 类型),表示产生式的左部;另一个是 right(List类型),表示产生式的右部。
    FirstFollow 类:表示 First 或 Follow 的类,包括两个属性,一个是 left(String 类型),另一个是 right(List类型)。
    Syntax 类:语法分析程序核心 终结符和非终结符分别用 List类型来表示。

    产生式用 List类型来表示。

    符号的 First 和 Follow 分别用 List类型来表示。

    预测分析表用 Production[][]类型表示,还需要借助终结符和非终结符的 List作为表头。

    语法分析过程中的栈用 Java 自带的 Stack来表示。

    Token 序列(简化版)用 List类型来表示。

    五、核心算法描述

    程序的核心算法主要有四个:

    getFirst():获得符号的 First 集合,算法在龙书的 P140.

    getFollow():获得符号的 Follow 集合,算法在龙书的 P140-141.

    getParsingTable():构建预测分析表,算法在龙书的 P142-143,算法 4.31

    syntaxParsing():表驱动的预测语法分析,算法在龙书的 P144,算法 4.34

    语法分析的过程输出 output.txt:

    Match:  	 
    Stack: 	 	$ Function  
    Input: 	 	int ID ( float ID ) { while ( ID <= NUM ) { if ( ID == NUM ) { ID = NUM ; } else 
    { int ID = NUM ; ID = ID + NUM ; } } return NUM ; } $  
    Action:  	输出: Program -> Function  
    Match:  	 
    Stack: 	 	$ } Return Statement { ) Para ( FuncName DataType  
    Input: 	 	int ID ( float ID ) { while ( ID <= NUM ) { if ( ID == NUM ) { ID = NUM ; } else 
    { int ID = NUM ; ID = ID + NUM ; } } return NUM ; } $  
    Action:  	输出: Function -> DataType FuncName ( Para ) { Statement Return }  
    Match:  	 
    Stack: 	 	$ } Return Statement { ) Para ( FuncName int  
    Input: 	 	int ID ( float ID ) { while ( ID <= NUM ) { if ( ID == NUM ) { ID = NUM ; } else 
    { int ID = NUM ; ID = ID + NUM ; } } return NUM ; } $  
    Action:  	输出: DataType -> int  
    Match:  	int  
    Stack: 	 	$ } Return Statement { ) Para ( FuncName  
    Input: 	 	ID ( float ID ) { while ( ID <= NUM ) { if ( ID == NUM ) { ID = NUM ; } else { int 
    ID = NUM ; ID = ID + NUM ; } } return NUM ; } $  
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    Action: 匹配 int

    Match:  	int  
    Stack: 	 	$ } Return Statement { ) Para ( ID  
    Input: 	 	ID ( float ID ) { while ( ID <= NUM ) { if ( ID == NUM ) { ID = NUM ; } else { int 
    ID = NUM ; ID = ID + NUM ; } } return NUM ; } $  
    Action:  	输出: FuncName -> ID  
    Match:  	int ID  
    Stack: 	 	$ } Return Statement { ) Para (  
    Input: 	 	( float ID ) { while ( ID <= NUM ) { if ( ID == NUM ) { ID = NUM ; } else { int ID 
    = NUM ; ID = ID + NUM ; } } return NUM ; } $  
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    Action: 匹配 ID

    Match: 	int ID (  
    Stack: 	 	$ } Return Statement { ) Para  
    Input: 	 	float ID ) { while ( ID <= NUM ) { if ( ID == NUM ) { ID = NUM ; } else { int ID = 
    NUM ; ID = ID + NUM ; } } return NUM ; } $  
    
    • 1
    • 2
    • 3
    • 4

    Action: 匹配 (

    Match:  	int ID (  
    Stack: 	 	$ } Return Statement { ) ID DataType  
    Input: 	 	float ID ) { while ( ID <= NUM ) { if ( ID == NUM ) { ID = NUM ; } else { int ID = 
    NUM ; ID = ID + NUM ; } } return NUM ; } $  
    Action:  	输出: Para -> DataType ID  
    Match:  	int ID (  
    Stack: 	 	$ } Return Statement { ) ID float  
    Input: 	 	float ID ) { while ( ID <= NUM ) { if ( ID == NUM ) { ID = NUM ; } else { int ID = 
    NUM ; ID = ID + NUM ; } } return NUM ; } $  
    Action:  	输出: DataType -> float  
    Match:  	int ID ( float  
    Stack: 	 	$ } Return Statement { ) ID  
    Input: 	 	ID ) { while ( ID <= NUM ) { if ( ID == NUM ) { ID = NUM ; } else { int ID = NUM ; 
    ID = ID + NUM ; } } return NUM ; } $  
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    Action: 匹配 float

    Match:  	int ID ( float ID  
    Stack: 	 	$ } Return Statement { )  
    Input: 	 	) { while ( ID <= NUM ) { if ( ID == NUM ) { ID = NUM ; } else { int ID = NUM ; 
    ID = ID + NUM ; } } return NUM ; } $  
    
    • 1
    • 2
    • 3
    • 4

    Action: 匹配 ID

    Match:  	int ID ( float ID )  
    Stack: 	 	$ } Return Statement {  
    Input: 	 	{ while ( ID <= NUM ) { if ( ID == NUM ) { ID = NUM ; } else { int ID = NUM ; ID 
    = ID + NUM ; } } return NUM ; } $  
    
    • 1
    • 2
    • 3
    • 4

    Action: 匹配 )

    Match:  	int ID ( float ID ) {  
    Stack: 	 	$ } Return Statement  
    Input: 	 	while ( ID <= NUM ) { if ( ID == NUM ) { ID = NUM ; } else { int ID = NUM ; ID 
    = ID + NUM ; } } return NUM ; } $  
    
    • 1
    • 2
    • 3
    • 4

    Action: 匹配 {

    Match:  	int ID ( float ID ) {  
    Stack: 	 	$ } Return Statement S  
    Input: 	while ( ID <= NUM ) { if ( ID == NUM ) { ID = NUM ; } else { int ID = NUM ; ID 
    = ID + NUM ; } } return NUM ; } $  
    Action:  	输出: Statement -> S Statement  
    Match:  	int ID ( float ID ) {  
    Stack: 	 	$ } Return Statement B ) C ( while  
    Input: 	 	while ( ID <= NUM ) { if ( ID == NUM ) { ID = NUM ; } else { int ID = NUM ; ID 
    = ID + NUM ; } } return NUM ; } $  
    Action:  	输出: S -> while ( C ) B  
    Match:  	int ID ( float ID ) { while  
    Stack: 	 	$ } Return Statement B ) C (  
    Input: 	 	( ID <= NUM ) { if ( ID == NUM ) { ID = NUM ; } else { int ID = NUM ; ID = ID + 
    NUM ; } } return NUM ; } $  
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    Action: 匹配 while

    Match:  	int ID ( float ID ) { while (  
    Stack: 	 	$ } Return Statement B ) C  
    Input: 	 	ID <= NUM ) { if ( ID == NUM ) { ID = NUM ; } else { int ID = NUM ; ID = ID + 
    NUM ; } } return NUM ; } $  
    
    • 1
    • 2
    • 3
    • 4

    Action: 匹配 (

    Match:  	int ID ( float ID ) { while (  
    Stack: 	 	$ } Return Statement B ) Keyword CompareOP Keyword  
    Input: 	 	ID <= NUM ) { if ( ID == NUM ) { ID = NUM ; } else { int ID = NUM ; ID = ID + 
    NUM ; } } return NUM ; } $  
    Action:  	输出: C -> Keyword CompareOP Keyword  
    Match:  	int ID ( float ID ) { while (  
    Stack: 	 	$ } Return Statement B ) Keyword CompareOP ID  
    Input: 	 	ID <= NUM ) { if ( ID == NUM ) { ID = NUM ; } else { int ID = NUM ; ID = ID + 
    NUM ; } } return NUM ; } $  
    Action:  	输出: Keyword -> ID  
    Match:  	int ID ( float ID ) { while ( ID  
    Stack: 	 	$ } Return Statement B ) Keyword CompareOP  
    Input: 	 	<= NUM ) { if ( ID == NUM ) { ID = NUM ; } else { int ID = NUM ; ID = ID + 
    NUM ; } } return NUM ; } $  
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    Action: 匹配 ID

    Match:int ID ( float ID ) { while ( ID
    Stack:$ } Return Statement B ) Keyword <=
    Input:<= NUM ) { if ( ID == NUM ) { ID = NUM ; } else { int ID = NUM ; ID = ID +
    NUM ; } } return NUM ; } $  
    Action: 	输出: CompareOP -> <=  
    Match:  	int ID ( float ID ) { while ( ID <=  
    Stack: 	 	$ } Return Statement B ) Keyword  
    Input:  NUM ) { if ( ID == NUM ) { ID = NUM ; } else { int ID = NUM ; ID = ID + NUM ; } } return NUM ; } $  
    
    • 1
    • 2
    • 3
    • 4
    • 5

    Action: 匹配 <=

    Match:  	int ID ( float ID ) { while ( ID <=  
    Stack: 	 	$ } Return Statement B ) NUM  
    Input:  NUM ) { if ( ID == NUM ) { ID = NUM ; } else { int ID = NUM ; ID = ID + NUM ; } } return NUM ; } $  
    Action:  	输出: Keyword -> NUM  
    Match:  	int ID ( float ID ) { while ( ID <= NUM  
    Stack: 	 	$ } Return Statement B )  
    Input:  ) { if ( ID == NUM ) { ID = NUM ; } else { int ID = NUM ; ID = ID + NUM ; } } return NUM ; } $  
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    Action: 匹配 NUM

    Match:  	int ID ( float ID ) { while ( ID <= NUM )  
    Stack: 	 	$ } Return Statement B  
    Input:  { if ( ID == NUM ) { ID = NUM ; } else { int ID = NUM ; ID = ID + NUM ; } } return NUM ; } $  
    
    • 1
    • 2
    • 3

    Action: 匹配 )

    Match:  	int ID ( float ID ) { while ( ID <= NUM )  
    Stack: 	 	$ } Return Statement } Statement {  
    Input:  { if ( ID == NUM ) { ID = NUM ; } else { int ID = NUM ; ID = ID + NUM ; } } return NUM ; } $  
    Action:  	输出: B -> { Statement }  
    Match:  	int ID ( float ID ) { while ( ID <= NUM ) {  
    Stack: 	 	$ } Return Statement } Statement  
    Input: 	 	if ( ID == NUM ) { ID = NUM ; } else { int ID = NUM ; ID = ID + NUM ; } } return NUM ; } $  
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    Action: 匹配 {

    Match:  	int ID ( float ID ) { while ( ID <= NUM ) {  
    
    • 1
    Stack:$ } Return Statement } Statement S
    Input: ;NUM ; } $if ( ID == NUM ) { ID = NUM ; } else { int ID = NUM ; ID = ID + NUM ; } } return
    Action:输出: Statement -> S Statement
    Match:int ID ( float ID ) { while ( ID <= NUM ) {
    Stack:$ } Return Statement } Statement D B ) C ( if
    Input:if ( ID == NUM ) { ID = NUM ; } else { int ID = NUM ; ID = ID + NUM ; } } return
    NUM ; } $ Action: ;输出: S -> if ( C ) B D
    Match:int ID ( float ID ) { while ( ID <= NUM ) { if
    Stack:$ } Return Statement } Statement D B ) C (
    Input:( ID == NUM ) { ID = NUM ; } else { int ID = NUM ; ID = ID + NUM ; } } return
    NUM ; } $ Action: ;匹配 if
    Match:int ID ( float ID ) { while ( ID <= NUM ) { if (
    Stack:$ } Return Statement } Statement D B ) C
    Input:ID == NUM ) { ID = NUM ; } else { int ID = NUM ; ID = ID + NUM ; } } return
    NUM ; } $ Action: ;匹配 (
    Match:int ID ( float ID ) { while ( ID <= NUM ) { if (
    Stack:$ } Return Statement } Statement D B ) Keyword CompareOP Keyword
    Input:ID == NUM ) { ID = NUM ; } else { int ID = NUM ; ID = ID + NUM ; } } return
    NUM ; } $ Action: ;输出: C -> Keyword CompareOP Keyword
    Match:int ID ( float ID ) { while ( ID <= NUM ) { if (
    Stack:$ } Return Statement } Statement D B ) Keyword CompareOP ID
    Input:ID == NUM ) { ID = NUM ; } else { int ID = NUM ; ID = ID + NUM ; } } return
    NUM ; } $ Action: ;输出: Keyword -> ID
    Match:int ID ( float ID ) { while ( ID <= NUM ) { if ( ID
    Stack:$ } Return Statement } Statement D B ) Keyword CompareOP
    Input:== NUM ) { ID = NUM ; } else { int ID = NUM ; ID = ID + NUM ; } } return NUM ; }
    $ ;Action: ;匹配 ID
    Match:int ID ( float ID ) { while ( ID <= NUM ) { if ( ID
    Stack:$ } Return Statement } Statement D B ) Keyword ==
    Input:== NUM ) { ID = NUM ; } else { int ID = NUM ; ID = ID + NUM ; } } return NUM ; }
    $ ;Action: ;输出: CompareOP -> ==
    Match:int ID ( float ID ) { while ( ID <= NUM ) { if ( ID ==
    Stack:$ } Return Statement } Statement D B ) Keyword
    Input:NUM ) { ID = NUM ; } else { int ID = NUM ; ID = ID + NUM ; } } return NUM ; }
    $ Action: ;匹配 ==
    Match:int ID ( float ID ) { while ( ID <= NUM ) { if ( ID ==
    Stack:$ } Return Statement } Statement D B ) NUM
    Input:NUM ) { ID = NUM ; } else { int ID = NUM ; ID = ID + NUM ; } } return NUM ; }
    $ ;Action:输出: Keyword -> NUM
    ;Match:int ID ( float ID ) { while ( ID <= NUM ) { if ( ID == NUM
    Stack:$ } Return Statement } Statement D B )
    Input:) { ID = NUM ; } else { int ID = NUM ; ID = ID + NUM ; } } return NUM ; } $
    Action: ;匹配 NUM
    Match:int ID ( float ID ) { while ( ID <= NUM ) { if ( ID == NUM )
    Stack:$ } Return Statement } Statement D B
    Input:{ ID = NUM ; } else { int ID = NUM ; ID = ID + NUM ; } } return NUM ; } $
    Action: ;匹配 )
    Match:int ID ( float ID ) { while ( ID <= NUM ) { if ( ID == NUM )
    Stack:$ } Return Statement } Statement D } Statement {
    Input:{ ID = NUM ; } else { int ID = NUM ; ID = ID + NUM ; } } return NUM ; } $
    Action:输出: B -> { Statement }
    ;Match:int ID ( float ID ) { while ( ID <= NUM ) { if ( ID == NUM ) {
    Stack:$ } Return Statement } Statement D } Statement
    Input:ID = NUM ; } else { int ID = NUM ; ID = ID + NUM ; } } return NUM ; } $
    Action: ;匹配 {
    Match:int ID ( float ID ) { while ( ID <= NUM ) { if ( ID == NUM ) {
    Stack:$ } Return Statement } Statement D } Statement S
    Input:ID = NUM ; } else { int ID = NUM ; ID = ID + NUM ; } } return NUM ; } $
    Action:输出: Statement -> S Statement
    ;Match:int ID ( float ID ) { while ( ID <= NUM ) { if ( ID == NUM ) {
    Stack:$ } Return Statement } Statement D } Statement N Keyword = ID
    Input:ID = NUM ; } else { int ID = NUM ; ID = ID + NUM ; } } return NUM ; } $
    Action: ;输出: S -> ID = Keyword N
    Match:int ID ( float ID ) { while ( ID <= NUM ) { if ( ID == NUM ) { ID
    Stack:$ } Return Statement } Statement D } Statement N Keyword =
    Input:= NUM ; } else { int ID = NUM ; ID = ID + NUM ; } } return NUM ; } $
    Action:匹配 ID
    ;Match:int ID ( float ID ) { while ( ID <= NUM ) { if ( ID == NUM ) { ID =
    Stack:$ } Return Statement } Statement D } Statement N Keyword
    Input:NUM ; } else { int ID = NUM ; ID = ID + NUM ; } } return NUM ; } $
    Action: ;匹配 =
    Match:int ID ( float ID ) { while ( ID <= NUM ) { if ( ID == NUM ) { ID =
    Stack:$ } Return Statement } Statement D } Statement N NUM
    Input:NUM ; } else { int ID = NUM ; ID = ID + NUM ; } } return NUM ; } $
    Action:输出: Keyword -> NUM
    ;Match:int ID ( float ID ) { while ( ID <= NUM ) { if ( ID == NUM ) { ID = NUM
    Stack:$ } Return Statement } Statement D } Statement N
    Input:; } else { int ID = NUM ; ID = ID + NUM ; } } return NUM ; } $
    Action:匹配 NUM
    ;Match:int ID ( float ID ) { while ( ID <= NUM ) { if ( ID == NUM ) { ID = NUM
    Stack:$ } Return Statement } Statement D } Statement ;
    Input:; } else { int ID = NUM ; ID = ID + NUM ; } } return NUM ; } $
    Action: ;输出: N -> ;
    Match:int ID ( float ID ) { while ( ID <= NUM ) { if ( ID == NUM ) { ID = NUM ;
    Stack:$ } Return Statement } Statement D } Statement
    Input:} else { int ID = NUM ; ID = ID + NUM ; } } return NUM ; } $
    Action: ;匹配 ;
    Match:int ID ( float ID ) { while ( ID <= NUM ) { if ( ID == NUM ) { ID = NUM ;
    Stack:$ } Return Statement } Statement D }
    Input:} else { int ID = NUM ; ID = ID + NUM ; } } return NUM ; } $
    Action: ;输出: Statement -> ε
    Match:int ID ( float ID ) { while ( ID <= NUM ) { if ( ID == NUM ) { ID = NUM ; }
    Stack:$ } Return Statement } Statement D
    Input:else { int ID = NUM ; ID = ID + NUM ; } } return NUM ; } $
    Action: ;匹配 }
    Match:int ID ( float ID ) { while ( ID <= NUM ) { if ( ID == NUM ) { ID = NUM ; }
    Stack:$ } Return Statement } Statement B else
    Input:else { int ID = NUM ; ID = ID + NUM ; } } return NUM ; } $
    Action:输出: D -> else B
    ;Match:int ID ( float ID ) { while ( ID <= NUM ) { if ( ID == NUM ) { ID = NUM ; } else
    Stack:$ } Return Statement } Statement B
    Input:{ int ID = NUM ; ID = ID + NUM ; } } return NUM ; } $
    Action: ;匹配 else
    Match:int ID ( float ID ) { while ( ID <= NUM ) { if ( ID == NUM ) { ID = NUM ; } else
    Stack:$ } Return Statement } Statement } Statement {
    Input:{ int ID = NUM ; ID = ID + NUM ; } } return NUM ; } $
    Action: ;输出: B -> { Statement }
    Match:int ID ( float ID ) { while ( ID <= NUM ) { if ( ID == NUM ) { ID = NUM ; } else {
    Stack:$ } Return Statement } Statement } Statement
    Input:int ID = NUM ; ID = ID + NUM ; } } return NUM ; } $
    Action: ;匹配 {
    Match:int ID ( float ID ) { while ( ID <= NUM ) { if ( ID == NUM ) { ID = NUM ; } else {
    Stack:$ } Return Statement } Statement } Statement S
    Input:int ID = NUM ; ID = ID + NUM ; } } return NUM ; } $
    Action:输出: Statement -> S Statement
    ;Match:int ID ( float ID ) { while ( ID <= NUM ) { if ( ID == NUM ) { ID = NUM ; } else {
    Stack:$ } Return Statement } Statement } Statement M ID DataType
    Input:int ID = NUM ; ID = ID + NUM ; } } return NUM ; } $
    Action:输出: S -> DataType ID M
    ;Match:int ID ( float ID ) { while ( ID <= NUM ) { if ( ID == NUM ) { ID = NUM ; } else {
    Stack:$ } Return Statement } Statement } Statement M ID int
    Input:int ID = NUM ; ID = ID + NUM ; } } return NUM ; } $
    Action: ;输出: DataType -> int
    Match:int ID ( float ID ) { while ( ID <= NUM ) { if ( ID == NUM ) { ID = NUM ; } else
    { int ; Stack:$ } Return Statement } Statement } Statement M ID
    Input:ID = NUM ; ID = ID + NUM ; } } return NUM ; } $
    Action:匹配 int
    ;Match:int ID ( float ID ) { while ( ID <= NUM ) { if ( ID == NUM ) { ID = NUM ; } else
    { int ID ; Stack:$ } Return Statement } Statement } Statement M
    Input:= NUM ; ID = ID + NUM ; } } return NUM ; } $
    Action:匹配 ID
    ;Match:int ID ( float ID ) { while ( ID <= NUM ) { if ( ID == NUM ) { ID = NUM ; } else
    { int ID ; Stack:$ } Return Statement } Statement } Statement ; NUM =
    Input:= NUM ; ID = ID + NUM ; } } return NUM ; } $
    Action:输出: M -> = NUM ;
    Match:  int ID ( float ID ) { while ( ID <= NUM ) { if ( ID == NUM ) { ID = NUM ; } else { int ID =  
    	Stack: 	 	$ } Return Statement } Statement } Statement ; NUM  
    	Input: 	 	NUM ; ID = ID + NUM ; } } return NUM ; } $  
    	Action:  	匹配 = 
     
    Match:  int ID ( float ID ) { while ( ID <= NUM ) { if ( ID == NUM ) { ID = NUM ; } else { int ID = NUM  
    	Stack: 	 	$ } Return Statement } Statement } Statement ;  
    	Input: 	 	; ID = ID + NUM ; } } return NUM ; } $  
    	Action:  	匹配 NUM 
     
    Match:  int ID ( float ID ) { while ( ID <= NUM ) { if ( ID == NUM ) { ID = NUM ; } else { int ID = NUM ;  
    	Stack: 	 	$ } Return Statement } Statement } Statement  
    	Input: 	 	ID = ID + NUM ; } } return NUM ; } $  
    	Action:  	匹配 ; 
     
    Match:  int ID ( float ID ) { while ( ID <= NUM ) { if ( ID == NUM ) { ID = NUM ; } else { int ID = NUM ;  
    	Stack: 	 	$ } Return Statement } Statement } Statement S  
    	Input: 	 	ID = ID + NUM ; } } return NUM ; } $  
    	Action:  	输出: Statement -> S Statement  
     
    Match:  int ID ( float ID ) { while ( ID <= NUM ) { if ( ID == NUM ) { ID = NUM ; } else { int ID = NUM ;  
    	Stack: 	 	$ } Return Statement } Statement } Statement N Keyword = ID  
    	Input: 	 	ID = ID + NUM ; } } return NUM ; } $  
    	Action:  	输出: S -> ID = Keyword N  
     
    Match:  int ID ( float ID ) { while ( ID <= NUM ) { if ( ID == NUM ) { ID = NUM ; } else { int ID = NUM ; ID  
    	Stack: 	 	$ } Return Statement } Statement } Statement N Keyword =  
    	Input: 	 	= ID + NUM ; } } return NUM ; } $  
    	Action:  	匹配 ID 
     
    Match:  int ID ( float ID ) { while ( ID <= NUM ) { if ( ID == NUM ) { ID = NUM ; } else { int ID = NUM ; ID =  
    	Stack: 	 	$ } Return Statement } Statement } Statement N Keyword  
    	Input: 	 	ID + NUM ; } } return NUM ; } $  
    	Action:  	匹配 = 
     
    	Match:  	int ID ( float ID ) { while ( ID <= NUM ) { if ( ID == NUM ) { ID = NUM ; } else 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    { int ID = NUM ; ID =  
    	Stack: 	 	$ } Return Statement } Statement } Statement N ID  
    	Input: 	 	ID + NUM ; } } return NUM ; } $  
    	Action:  	输出: Keyword -> ID  
     
    Match:  int ID ( float ID ) { while ( ID <= NUM ) { if ( ID == NUM ) { ID = NUM ; } else { int ID = NUM ; ID = ID  
    	Stack: 	 	$ } Return Statement } Statement } Statement N  
    	Input: 	 	+ NUM ; } } return NUM ; } $  
    	Action:  	匹配 ID 
     
    Match:  int ID ( float ID ) { while ( ID <= NUM ) { if ( ID == NUM ) { ID = NUM ; } else { int ID = NUM ; ID = ID  
    	Stack: 	 	$ } Return Statement } Statement } Statement ; Keyword OP  
    	Input: 	 	+ NUM ; } } return NUM ; } $  
    	Action:  	输出: N -> OP Keyword ;  
     
    Match:  int ID ( float ID ) { while ( ID <= NUM ) { if ( ID == NUM ) { ID = NUM ; } else { int ID = NUM ; ID = ID  
    	Stack: 	 	$ } Return Statement } Statement } Statement ; Keyword +  
    	Input: 	 	+ NUM ; } } return NUM ; } $  
    	Action:  	输出: OP -> +  
     
    Match:  int ID ( float ID ) { while ( ID <= NUM ) { if ( ID == NUM ) { ID = NUM ; } else { int ID = NUM ; ID = ID +  
    	Stack: 	 	$ } Return Statement } Statement } Statement ; Keyword  
    	Input: 	 	NUM ; } } return NUM ; } $  
    	Action:  	匹配 + 
     
    Match:  int ID ( float ID ) { while ( ID <= NUM ) { if ( ID == NUM ) { ID = NUM ; } else { int ID = NUM ; ID = ID +  
    	Stack: 	 	$ } Return Statement } Statement } Statement ; NUM  
    	Input: 	 	NUM ; } } return NUM ; } $  
    	Action:  	输出: Keyword -> NUM  
     
    Match:  int ID ( float ID ) { while ( ID <= NUM ) { if ( ID == NUM ) { ID = NUM ; } else { int ID = NUM ; ID = ID + NUM  
    	Stack: 	 	$ } Return Statement } Statement } Statement ;  
    	Input: 	 	; } } return NUM ; } $  
    	Action:  	匹配 NUM 
     
    Match:  int ID ( float ID ) { while ( ID <= NUM ) { if ( ID == NUM ) { ID = NUM ; } else { int ID = NUM ; ID = ID + NUM ;  
    	Stack: 	 	$ } Return Statement } Statement } Statement  
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    Input: 	 	} } return NUM ; } $  
    	Action:  	匹配 ; 
     
    Match:  int ID ( float ID ) { while ( ID <= NUM ) { if ( ID == NUM ) { ID = NUM ; } else { int ID = NUM ; ID = ID + NUM ;  
    	Stack: 	 	$ } Return Statement } Statement }  
    	Input: 	 	} } return NUM ; } $  
    	Action:  	输出: Statement -> ε  
     
    Match:  int ID ( float ID ) { while ( ID <= NUM ) { if ( ID == NUM ) { ID = NUM ; } else { int ID = NUM ; ID = ID + NUM ; }  
    	Stack: 	 	$ } Return Statement } Statement  
    	Input: 	 	} return NUM ; } $  
    	Action:  	匹配 } 
     
    	Match:  	int ID ( float ID ) { while ( ID <= NUM ) { if ( ID == NUM ) { ID = NUM ; } else 
    { int ID = NUM ; ID = ID + NUM ; }  
    	Stack: 	 	$ } Return Statement }  
    	Input: 	 	} return NUM ; } $  
    	Action:  	输出: Statement -> ε  
     
    Match:  int ID ( float ID ) { while ( ID <= NUM ) { if ( ID == NUM ) { ID = NUM ; } else { int ID = NUM ; ID = ID + NUM ; } }  
    	Stack: 	 	$ } Return Statement  
    	Input: 	 	return NUM ; } $  
    	Action:  	匹配 } 
     
    Match:  int ID ( float ID ) { while ( ID <= NUM ) { if ( ID == NUM ) { ID = NUM ; } else { int ID = NUM ; ID = ID + NUM ; } }  
    	Stack: 	 	$ } Return  
    	Input: 	 	return NUM ; } $  
    	Action:  	输出: Statement -> ε  
     
    	Match:  	int ID ( float ID ) { while ( ID <= NUM ) { if ( ID == NUM ) { ID = NUM ; } else 
    { int ID = NUM ; ID = ID + NUM ; } }  
    	Stack: 	 	$ } ; Keyword return  
    	Input: 	 	return NUM ; } $  
    	Action:  	输出: Return -> return Keyword ;  
     
    Match:  int ID ( float ID ) { while ( ID <= NUM ) { if ( ID == NUM ) { ID = NUM ; } else { int ID = NUM ; ID = ID + NUM ; } } return  
    	Stack: 	 	$ } ; Keyword  
    	Input: 	 	NUM ; } $  
    	Action:  	匹配 return 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    Match:  	int ID ( float ID ) { while ( ID <= NUM ) { if ( ID == NUM ) { ID = NUM ; } else 
    { int ID = NUM ; ID = ID + NUM ; } } return  
    Stack: 	 	$ } ; NUM  
    Input: 	 	NUM ; } $  
    Action:  	输出: Keyword -> NUM  
    Match:  	int ID ( float ID ) { while ( ID <= NUM ) { if ( ID == NUM ) { ID = NUM ; } else 
    { int ID = NUM ; ID = ID + NUM ; } } return NUM  
    Stack: 	 	$ } ;  
    Input: 	 	; } $  
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    Action: 匹配 NUM

    Match:  	int ID ( float ID ) { while ( ID <= NUM ) { if ( ID == NUM ) { ID = NUM ; } else 
    { int ID = NUM ; ID = ID + NUM ; } } return NUM ;  
    Stack: 	 	$ }  
    Input: 	 	} $  
    Action:  	匹配 ; 
    Match:  int ID ( float ID ) { while ( ID <= NUM ) { if ( ID == NUM ) { ID = NUM ; } else { int ID = NUM ; ID = ID + NUM ; } } return NUM ; }  
    Stack: 	 	$  
    Input: 	 	$  
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    Action: 匹配 }

    Match:  int ID ( float ID ) { while ( ID <= NUM ) { if ( ID == NUM ) { ID = NUM ; } else { int ID = NUM ; ID = ID + NUM ; } } return NUM ; } $  
    Stack: 	 	 
    Input: 	 	 
    
    • 1
    • 2
    • 3

    Action: 匹配 $

  • 相关阅读:
    Worthington细胞色素 C 消化研究丨羧肽酶 B方案
    【嵌入式Linux应用】初步移植MQTT到Ubuntu和Linux开发板
    Excel 数据透视表教程大全之 05 数据透视表绘制各种二维排列的数据,实现双向枢轴(教程含数据)
    使用C# RDLC环境搭建
    百度Java面试题前200页都在这里了
    vue2-org-tree 树型结构的使用
    SpringBoot - Identify and stop the process that‘s listening on port 8080解决方案
    开源IPTV源服务程序使用教程
    PAT 1139 First Contact
    架构整洁之道(二)
  • 原文地址:https://blog.csdn.net/newlw/article/details/126930509