?? sentencecompile.h
字號:
//SentenceCompile.h
//inputfile: words.txt,wordstable1.txt
//outputfile: sentences.txt,wordstable2.txt
#ifndef SENTENCECOMPILE_H_
#define SENTENCECOMPILE_H_
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
using namespace std;
/*****************************************************************************************
語法分析器
實(shí)現(xiàn)功能:
1。按照正則表達(dá)式,判斷語句是否有語法錯誤。
2。如果有錯誤則需要輸入錯誤類型及行號,沒錯誤則輸出一個正確信號。
3。讀取由詞法分析器所生成的字符表,并回填相應(yīng)的類型信息。
4。要求將過程及函數(shù)所用到的變量數(shù)量、類型記錄下來。
5。將程序中的語句分離開,并記錄到目標(biāo)文件以便類型檢查使用。
6。可以實(shí)現(xiàn)的語句類型有以下幾種:
//statement
a ; //過程
a (b) ; //帶有參數(shù)的過程
a := b ; //賦值
if a=b then a:=b else a:=b ; //條件
while a=b do a:= b ; //循環(huán)
read (a,b) ; //輸入
write (a,b) ; //輸出
回填的信息如下 :
序號(Index) 類型(Kind) 基本類型(Type) 名稱(Name) 參數(shù)個數(shù)(Have) 所屬函數(shù)(Belong)
類型:
程序名 900
過程名 800
函數(shù)名 700
變量 100
程序參數(shù) 990
過程參數(shù) 810
函數(shù)參數(shù) 710
基本類型:
整數(shù) 101
浮點(diǎn)數(shù) 102
布爾型 103
整數(shù)數(shù)組 201
浮點(diǎn)數(shù)數(shù)組 202
布爾型數(shù)組 203
等待輸入類型信息 100
等待輸入數(shù)組類型信息 200
等待輸入函數(shù)類型信息 700
舉例如下:
program example(input,output);
var x,y:integer;
var abc:real;
procedure gcd(a,b:integer);
讀入信息:
900 example
910 input
910 output
100 x
100 y
800 gcd
810 a
810 b
輸出信息:
Index Kind Type Name Have Belong
1 900 900 example 2 0
2 990 101 input 0 101(第一個的第一位)
3 990 101 output 0 102
4 100 101 x 0 1
5 100 101 y 0 2
6 700 800 gcd 2 0
7 710 101 a 0 601
8 710 101 b 0 602
正則表達(dá)式:
programs -> program id ( id_list ) ; (1)
(declaration) *
[ functions ] *
[ procedures ] *
body .
declaration-> var id_list : type ; (2)
type -> def (3)
|
array [ number .. number ] of def (4)
|
record declaration end (5)
functions -> function id ( [ id_list : type ] ) : type ; (6)
[ declaration ] *
body ;
procedures -> procedure id ( [ id_list : type ] ) ; (7)
[ declaration ] *
body ;
body -> begin (8)
[ statement ; ] *
end
|
statement
statement -> ids (9)
|
ids ( simple_list ) (10)
|
ids assignop simple (11)
|
while expression do body (12)
|
if expression then body else body (13)
|
read ( ids_list ) (14)
|
write ( simple_list ) (15)
id_list -> id , id_list | id (16)
ids_list -> ids , ids_list | ids (17)
expression -> simple relop simple (18)
simple_list -> simple | simple , simple_list (19)
simple -> factor (20)
|
factor op simple (21)
factor -> ids (22)
|
ids ( simple_list ) (23)
|
number (24)
|
not factor (25)
|
true (26)
|
false (27)
|
( simple ) (28)
其中的非終結(jié)符:
1 programs
2 declaration
3 type
4 functions
5 procedures
6 body
7 statement
8 id_list
9 ids_list
10 expression
11 simple_list
12 simple
13 factor
終結(jié)符:
101 program,102 begin,103 end,104 def,105 function,106 procedure
107 if,108 then,109 else,110 while,111 do
112 read,113 write,114 array,115 record,116 of
117 or,118 and,119 not,120 true,121 false
122 number,123 id,124 ids
125 op,126 relop,127 :,128 ;,129 (,130 ),131 [,132 ],133 .,134 ,
135 var
136 assignop
寫出狀態(tài)機(jī)如下:————————函數(shù):Check(int state,int next)
<凡是下面列出的特殊移進(jìn)及歸約,均在程序塊后面加一句 Check(State[StateIndex],next) >
1 programs -> 1000 program 1001 id 1002( 1003 id_list 1004 ) 1005 ; 1006
(declaration) *
[ functions ] *
[ procedures ] *
body 1007 . 9999(結(jié)束)
1003 -> 123 -> StateIndex++, State[StateIndex]= 8000
1006 -> 135 -> StateIndex++, State[StateIndex]= 2000
1006 -> 105 -> StateIndex++, State[StateIndex]= 4000
1006 -> 106 -> StateIndex++, State[StateIndex]= 5000
1006 -> 102 -> StateIndex++, State[StateIndex]= 6000
1007 -> 133 -> State = 9999
9999 -> -> 輸出成功信息
9999 -> 任意 -> 輸出錯誤信息
1001->1002: VariantIndex++;
Parament=0;
VariantType[]=900;
1005->1006: Parament=0;HaveCount=0;
1006->6000: Parament=0;HaveCount=0;
2 declaration-> 2000 var 2001 id_list 2002 : 2003 type 2004 ; 2005
2001 -> 123 -> StateIndex++, State[StateIndex]= 8000
2003 -> 104 -> StateIndex++, State[StateIndex]= 3000
114
115
2005 -> 任意 -> StateIndex--, Check(State[StateIndex],2)
2003->3000: TypeNeed=100;
3 type -> 3000 def 3001
|
3000 array 3002 [ 3003 number 3004 . 3005 . 3006 number 3007 ] 3008 of 3009 def 3010
|
3000 record 3011 declaration 3012 end 3013
3001 -> 任意 -> StateIndex--, Check(State[StateIndex],3)
3010 -> 任意 -> StateIndex--, Check(State[StateIndex],3)
3011 -> 135 -> StateIndex++, State[StateIndex]= 2000
3013 -> 任意 -> StateIndex--, Check(State[StateIndex],3)
3000->3001: 將Type=100+temp信息回填到VariantType==TypeNeed的VariantType中
3009->3010: 將Type=200+temp信息回填到VariantType==TypeNeed的VariantType中
* 3000->3011: 將Type=300信息回填到VariantType==TypeNeed的VariantType中
4 functions -> 4000 function 4001 id 4002 ( 4004 [ id_list 4005 : 4006 type 4007 ] ) 4009 : 4010 type 4011 ; 4012
[ declaration ] *
body 4013 ; 4014
4004 -> 123 -> StateIndex++, State[StateIndex]= 8000
4006 -> 104 -> StateIndex++, State[StateIndex]= 3000
114
115
4012 -> 135 -> StateIndex++, State[StateIndex]= 2000
4012 -> 102 -> StateIndex++, State[StateIndex]= 6000
4014 -> 任意 -> StateIndex--, Check(State[StateIndex],4)
4001->4002: VariantIndex++;SubCount++;
Temp=VariantIndex;
VariantBelong[]=0;
Parament=VariantIndex;
VariantType=700;等待輸入類型
4004->8000: HaveCount=0;
4009->4010: VariantHave=HaveCount;參數(shù)個數(shù)
4006->3000: TypeNeed=100;
4010->3000: TypeNeed=700;
4013->4014: Parament=0;
5 procedures -> 5000 procedure 5001 id 5002 ( 5003 [ id_list 5004 : 5005 type 5006 ] ) 5007 ; 5008
[ declaration ] *
body 5009 ; 5010
5003 -> 123 -> StateIndex++, State[StateIndex]= 8000
5005 -> 104 114 115 -> StateIndex++, State[StateIndex]= 3000
5008 -> 135 -> StateIndex++, State[StateIndex]= 2000
5008 -> 102 -> StateIndex++, State[StateIndex]= 6000
5010 -> 任意 -> StateIndex--, Check(State[StateIndex],5)
5001->5002: VariantIndex++;SubCount++;
Temp=VariantIndex;
VariantBelong[]=0;
Parament=VariantIndex;
VariantType=800;
5003->8000: HaveCount=0;
5005->3000: TypeNeed=100;
5007->5008: VariantHave=HaveCount;
6 body -> 6000 begin 6001
[ [ statement 6002 ; 6003 ] statement 6002 ]
end 6004
|
6000 statement 6005
6001 -> 124 -> StateIndex++, State[StateIndex]= 7000
107
110
112
113
6000 -> 124 -> StateIndex++, State[StateIndex]= 7000
107
110
112
113
6004 -> 任意 -> StateIndex--, Check(State[StateIndex],6)
6005 -> 任意 -> StateIndex--, Check(State[StateIndex],6)
7 statement -> 7000 ids 7001
|
7000 ids 7001 ( 7002 simple_list 7003 ) 7004
|
7000 ids 7001 assignop 7005 simple 7006
|
7000 while 7007 expression 7008 do 7009 body 7010
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -