递归下降语法分析初稿
This commit is contained in:
parent
ca494463e3
commit
8afaba08c5
|
@ -141,6 +141,7 @@
|
|||
<ItemGroup>
|
||||
<ClCompile Include="LexicalAnalyzer.cpp" />
|
||||
<ClCompile Include="Parsing_RD.cpp" />
|
||||
<ClCompile Include="the_main.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="LexicalAnalyzer.h" />
|
||||
|
|
|
@ -21,6 +21,9 @@
|
|||
<ClCompile Include="Parsing_RD.cpp">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="the_main.cpp">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="LexicalAnalyzer.h">
|
||||
|
|
|
@ -17,10 +17,10 @@ LexicalAnalyzer::~LexicalAnalyzer(){}
|
|||
Word keyWords[21] =
|
||||
{
|
||||
{"program",PROGRAM},{"type",TYPE},{"var",VAR},
|
||||
{"procedure",PROCEDURE},{"begin",BEGIN},{"end",END1},{"array",ARRAY},
|
||||
{"procedure",PROCEDURE},{"begin",BEGIN},{"end",END},{"array",ARRAY},
|
||||
{"of",OF},{"record",RECORD},{"if",IF},{"then",THEN},{"else",ELSE},{"fi",FI},
|
||||
{"while",WHILE},{"do",DO},{"endwh",ENDWH},{"read",READ},{"write",WRITE},
|
||||
{"return",RETURN1},{"integer",INTEGER},{"char",CHAR1}
|
||||
{"return",RETURN},{"integer",INTEGER},{"char",CHAR1}
|
||||
};//保留字
|
||||
|
||||
//类型哈希表
|
||||
|
@ -29,9 +29,9 @@ unordered_map<int, string> ha =
|
|||
{0, "ENDFILE1"}, {1, "ERROR1"}, {2, "PROGRAM"}, {3,"PROCEDURE"},
|
||||
{4, "TYPE"}, {5, "VAR"}, {6, "IF"}, {7, "THEN"},
|
||||
{8, "ELSE"}, {9, "FI"}, {10, "WHILE"}, {11, "DO"},
|
||||
{12, "ENDWH"}, {13, "BEGIN"}, {14, "END1"}, {15, "READ"},
|
||||
{12, "ENDWH"}, {13, "BEGIN"}, {14, "END"}, {15, "READ"},
|
||||
{16, "WRITE"}, {11, "ARRAY"}, {12, "OF"}, {13, "RECORD"},
|
||||
{20, "RETURN1"}, {21, "INTEGER"}, {22, "CHAR1"}, {23, "ID"},
|
||||
{20, "RETURN"}, {21, "INTEGER"}, {22, "CHAR1"}, {23, "ID"},
|
||||
{24, "INTC"}, {25, "CHARC"}, {26, "ASSIGN"}, {27, "EQ"},
|
||||
{28, "LT"}, {29, "PLUS"}, {30, "MINUS"}, {31, "TIMES"},
|
||||
{32, "OVER"}, {33, "LPAREN"}, {34, "RPAREN"}, {35, "DOT"},
|
||||
|
@ -230,7 +230,7 @@ void LexicalAnalyzer::getTokenList()
|
|||
}
|
||||
else
|
||||
{
|
||||
tempToken = new Token(lineShow, Word(currentString, ERROR1));
|
||||
tempToken = new Token(lineShow, Word(currentString, DOT));
|
||||
TokenList.push_back(tempToken);
|
||||
}
|
||||
}
|
||||
|
@ -316,30 +316,4 @@ void LexicalAnalyzer::getTokenList()
|
|||
string temp(1, c);
|
||||
Token* tempToken = new Token(lineShow, Word(temp, ENDFILE1));
|
||||
TokenList.push_back(tempToken);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
LexicalAnalyzer lexicalanalyzer;
|
||||
lexicalanalyzer.getTokenList();
|
||||
int count = lexicalanalyzer.TokenList.size();
|
||||
//for (int i = 0; i < count; i++)
|
||||
//{
|
||||
// cout<< "第" << lexicalanalyzer.TokenList[i]->lineShow
|
||||
// << "行 <" << ha.at(lexicalanalyzer.TokenList[i]->word.Lex)
|
||||
// << "," << lexicalanalyzer.TokenList[i]->word.Sem
|
||||
// << ">" << endl;
|
||||
//}
|
||||
|
||||
//ofstream file;
|
||||
//file.open("tokenList.txt");
|
||||
//for (int i = 0; i < count; i++)
|
||||
//{
|
||||
// file << lexicalanalyzer.TokenList[i]->lineShow
|
||||
// << ' ' << lexicalanalyzer.TokenList[i]->word.Lex
|
||||
// << ' ' << lexicalanalyzer.TokenList[i]->word.Sem
|
||||
// << '\n';
|
||||
//}
|
||||
|
||||
cout << "运行成功" << endl;
|
||||
}
|
|
@ -20,8 +20,8 @@ typedef enum
|
|||
/* ±£Áô×Ö */
|
||||
PROGRAM, PROCEDURE, TYPE, VAR, IF,
|
||||
THEN, ELSE, FI, WHILE, DO, ENDWH,
|
||||
BEGIN, END1, READ, WRITE, ARRAY, OF,
|
||||
RECORD, RETURN1,
|
||||
BEGIN, END, READ, WRITE, ARRAY, OF,
|
||||
RECORD, RETURN,
|
||||
|
||||
INTEGER, CHAR1,
|
||||
|
||||
|
|
1301
Parsing_RD.cpp
1301
Parsing_RD.cpp
File diff suppressed because it is too large
Load Diff
118
Parsing_RD.h
118
Parsing_RD.h
|
@ -60,48 +60,55 @@ struct symbtable;
|
|||
typedef struct treeNode
|
||||
|
||||
{
|
||||
struct treeNode* child[3]; /* 子节点指针 */
|
||||
struct treeNode* sibling; /* 兄弟节点指针 */
|
||||
int lineno; /* 源代码行号 */
|
||||
NodeKind nodekind; /* 节点类型 */
|
||||
struct treeNode* child[3]; //子节点
|
||||
struct treeNode* sibling; //兄弟节点
|
||||
int lineno; //源程序行号
|
||||
|
||||
NodeKind nodekind; /*记录语法树节点类型,取值 ProK, PheadK, TypeK, VarK,
|
||||
ProcDecK, StmLK, DecK, StmtK, ExpK, 为语法树节点类型*/
|
||||
|
||||
union
|
||||
{
|
||||
DecKind dec;
|
||||
StmtKind stmt;
|
||||
ExpKind exp;
|
||||
} kind; /* 具体类型 */
|
||||
DecKind dec; /*记录语法树节点的声明类型,当 nodekind = DecK 时有效,取
|
||||
ArrayK,CharK,lntegerK,RecordK,IdK, 为语法树节点声明类型*/
|
||||
StmtKind stmt; /*记录语法树节点的语句类型,当 nodekind = StmtK 时有效,取值 IfK,
|
||||
WhileK,AssignK,ReadK, WriteK, CallK,RetumK, 为语法树节点语句类型*/
|
||||
ExpKind exp; /*记录语法树节点的表达式类型,当 nodekind=ExpK 时有效,取值 OpK,
|
||||
ConstK,IdK, 为语法树节点表达式类型*/
|
||||
|
||||
int idnum; /* 相同类型的变量个数 */
|
||||
} kind; /* 具体类型 */
|
||||
|
||||
string name[10]; /* 标识符的名称 */
|
||||
|
||||
struct symbtable* table[10]; /* 与标志符对应的符号表地址,在语义分析阶段填入*/
|
||||
int idnum; //记录一个节点中的标志符的个数
|
||||
string name[10]; //节点中标志符的名字
|
||||
struct symbtable* table[10]; /*记录类型名,当节点为声明类型,且类型是由类型标志符表示时有效。
|
||||
语义分析填入?*/
|
||||
|
||||
struct
|
||||
{
|
||||
struct
|
||||
{
|
||||
int low; /* 数组下界 */
|
||||
int up; /* 数组上界 */
|
||||
DecKind childtype; /* 数组的子类型 */
|
||||
}ArrayAttr; /* 数组属性 */
|
||||
int low; // 数组下界
|
||||
int up; // 数组上界
|
||||
DecKind childtype; // 数组的子类型
|
||||
}ArrayAttr; // 数组属性
|
||||
|
||||
struct
|
||||
{
|
||||
ParamType paramt; /* 过程的参数类型 */
|
||||
}ProcAttr; /* 过程属性 */
|
||||
ParamType paramt; /* 过程的参数类型值为枚举类型valparamtype 或者 varparamtype
|
||||
表示过程参数是值参还是变参*/
|
||||
}ProcAttr; // 过程属性
|
||||
|
||||
struct
|
||||
{
|
||||
LexType op; /* 表达式的操作符*/
|
||||
int val; /* 表达式的值 */
|
||||
VarKind varkind; /* 变量的类别 */
|
||||
ExpType type; /* 用于类型检查 */
|
||||
}ExpAttr; /* 表达式属性 */
|
||||
LexType op; // 表达式的操作符
|
||||
int val; // 表达式的值
|
||||
VarKind varkind; // 变量的类别
|
||||
ExpType type; // 用于类型检查
|
||||
}ExpAttr; // 表达式属性
|
||||
|
||||
string type_name; /* 类型名是标识符 */
|
||||
string type_name; //记录类型名,当节点为声明类型,且类型是由类型标志符表示时有效。
|
||||
|
||||
} attr; /* 属性 */
|
||||
} attr; //记录语法树节点其他属性
|
||||
}TreeNode;
|
||||
|
||||
class RecursiveDescentParsing
|
||||
|
@ -110,17 +117,74 @@ public:
|
|||
void initial(); //全局变量初始化
|
||||
void ReadNextToken(); //读文件
|
||||
void syntaxError(string errorMessage); //输出语法错误
|
||||
void match(LexType lt);
|
||||
void match(LexType lt); //匹配
|
||||
void fileClose();
|
||||
|
||||
//1
|
||||
TreeNode* parse(void);
|
||||
TreeNode* Program(void);
|
||||
TreeNode* ProgramHead(void);
|
||||
TreeNode* DeclarePart(void);
|
||||
TreeNode* TypeDec(void);
|
||||
TreeNode* TypeDeclaration(void);
|
||||
TreeNode* TypeDecList(void);
|
||||
TreeNode* TypeDecMore(void);
|
||||
void TypeId(TreeNode* t);
|
||||
void TypeName(TreeNode* t);
|
||||
void BaseType(TreeNode* t);
|
||||
|
||||
//11
|
||||
void BaseType(TreeNode* t);
|
||||
void StructureType(TreeNode* t);
|
||||
void ArrayType(TreeNode* t);
|
||||
void RecType(TreeNode* t);
|
||||
TreeNode* FieldDecList(void);
|
||||
TreeNode* FieldDecMore(void);
|
||||
void IdList(TreeNode* t);
|
||||
void IdMore(TreeNode* t);
|
||||
TreeNode* VarDec(void);
|
||||
TreeNode* VarDeclaration(void);
|
||||
|
||||
//21
|
||||
TreeNode* VarDecList(void);
|
||||
TreeNode* VarDecMore(void);
|
||||
void VarIdList(TreeNode* t);
|
||||
void VarIdMore(TreeNode* t);
|
||||
TreeNode* ProcDec(void);
|
||||
TreeNode* ProcDeclaration(void);
|
||||
void ParamList(TreeNode* t);
|
||||
TreeNode* ParamDecList(void);
|
||||
TreeNode* ParamMore(void);
|
||||
TreeNode* Param(void);
|
||||
|
||||
//31
|
||||
void FormList(TreeNode* t);
|
||||
void FidMore(TreeNode* t);
|
||||
TreeNode* ProcDecPart(void);
|
||||
TreeNode* ProcBody(void);
|
||||
TreeNode* ProgramBody(void);
|
||||
TreeNode* StmList(void);
|
||||
TreeNode* StmMore(void);
|
||||
TreeNode* Stm(void);
|
||||
TreeNode* AssCall(void);
|
||||
TreeNode* AssignmentRest(void);
|
||||
|
||||
//41
|
||||
TreeNode* ConditionalStm(void);
|
||||
TreeNode* LoopStm(void);
|
||||
TreeNode* InputStm(void);
|
||||
TreeNode* OutputStm(void);
|
||||
TreeNode* ReturnStm(void);
|
||||
TreeNode* CallStmRest(void);
|
||||
TreeNode* ActParamList(void);
|
||||
TreeNode* ActParamMore(void);
|
||||
TreeNode* Exp(void);
|
||||
TreeNode* Simple_exp(void);
|
||||
|
||||
//51
|
||||
TreeNode* Term(void);
|
||||
TreeNode* Factor(void);
|
||||
TreeNode* Variable(void);
|
||||
void VariMore(TreeNode* t);
|
||||
TreeNode* Fieldvar(void);
|
||||
void FieldvarMore(TreeNode* t);
|
||||
};
|
3
text.cpp
3
text.cpp
|
@ -1,7 +1,8 @@
|
|||
program p
|
||||
type t = integer;
|
||||
var t v1;
|
||||
var integer v1,v2;
|
||||
char v2;
|
||||
|
||||
begin
|
||||
read(v1);
|
||||
v1:= v1 + 10;
|
||||
|
|
|
@ -0,0 +1,44 @@
|
|||
#include <iostream>
|
||||
#include<fstream>
|
||||
#include<string>
|
||||
#include<unordered_map>
|
||||
#include<algorithm>
|
||||
#include<set>
|
||||
#include<vector>
|
||||
#include <iomanip>
|
||||
#include <sstream>
|
||||
#include"LexicalAnalyzer.h"
|
||||
#include"Parsing_RD.h"
|
||||
using namespace std;
|
||||
int main()
|
||||
{
|
||||
LexicalAnalyzer lexicalanalyzer;
|
||||
lexicalanalyzer.getTokenList();
|
||||
int count = lexicalanalyzer.TokenList.size();
|
||||
|
||||
//for (int i = 0; i < count; i++)
|
||||
//{
|
||||
// cout<< "µÚ" << lexicalanalyzer.TokenList[i]->lineShow
|
||||
// << "ÐÐ <" << ha.at(lexicalanalyzer.TokenList[i]->word.Lex)
|
||||
// << "," << lexicalanalyzer.TokenList[i]->word.Sem
|
||||
// << ">" << endl;
|
||||
//}
|
||||
|
||||
ofstream file;
|
||||
file.open("tokenList.txt");
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
file << lexicalanalyzer.TokenList[i]->lineShow
|
||||
<< ' ' << lexicalanalyzer.TokenList[i]->word.Lex
|
||||
<< ' ' << lexicalanalyzer.TokenList[i]->word.Sem
|
||||
<< endl;
|
||||
}
|
||||
file.close();
|
||||
|
||||
RecursiveDescentParsing rd;
|
||||
rd.initial();
|
||||
TreeNode* root;
|
||||
root = rd.parse();
|
||||
rd.fileClose();
|
||||
cout << "ÔËÐгɹ¦" << endl;
|
||||
}
|
|
@ -6,28 +6,30 @@
|
|||
2 21 integer
|
||||
2 37 ;
|
||||
3 5 var
|
||||
3 23 t
|
||||
3 21 integer
|
||||
3 23 v1
|
||||
3 38 ,
|
||||
3 23 v2
|
||||
3 37 ;
|
||||
4 22 char
|
||||
4 23 v2
|
||||
4 37 ;
|
||||
5 13 begin
|
||||
6 15 read
|
||||
6 33 (
|
||||
6 23 v1
|
||||
6 34 )
|
||||
6 37 ;
|
||||
6 13 begin
|
||||
7 15 read
|
||||
7 33 (
|
||||
7 23 v1
|
||||
7 26 :=
|
||||
7 23 v1
|
||||
7 29 +
|
||||
7 24 10
|
||||
7 34 )
|
||||
7 37 ;
|
||||
8 16 write
|
||||
8 33 (
|
||||
8 23 v1
|
||||
8 34 )
|
||||
9 14 end
|
||||
9 1 .
|
||||
9 0 ÿ
|
||||
8 26 :=
|
||||
8 23 v1
|
||||
8 29 +
|
||||
8 24 10
|
||||
8 37 ;
|
||||
9 16 write
|
||||
9 33 (
|
||||
9 23 v1
|
||||
9 34 )
|
||||
10 14 end
|
||||
10 35 .
|
||||
10 0 ÿ
|
||||
|
|
Loading…
Reference in New Issue