SLR分析器的设计
Posted 红心火柴
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SLR分析器的设计相关的知识,希望对你有一定的参考价值。
一、实验目的
根据文法编制SLR语法分析程序,以便对输入的符号串进行语法分析。通过编写SLR语法分析程序掌握移进归约方法的基本原理、SLR分析表的构造方法以及移进归约分析法主控程序的设计。
二、实验内容
对下列算术表达式的文法编写SLR语法分析程序,要求对输入的符号串进行语法分析:
(1)E->E+T
(2)E->E-T
(3)E->T
(4)T->T*F
(5)T->T/F
(6)T->F
(7)F->(E)
(8)F->id
(9)F->num
注:文法中id表示标识符(此处标识符的定义与实验一中标识符的定义相同),num表示数字(简单处理可以认为是整数)
三、实验设计方案
五、测试方案及测试结果
结语
SLR分析器设计的介绍就到这里啦,希望这篇文章能给予你一些帮助,感谢各位人才的:点赞、收藏和评论,我们下次见。
附录
以下提供测试代码
SLR语法分析器
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
char Keywords[22][10]=
//关键字表
"double", "int", "struct", "break", "static",
"long", "switch", "case", "char", "return",
"const", "float", "short", "continue", "for",
"signed", "void","default", "do", "while",
"if", "else";
char Symbol[30][10] =
//符号表
"+", "-", "*", "/", "=", "==", "!=", ">", "<", ">=",
"<=", ",", ";", "", "", "(", ")", "!","&", "&&",
"|", "||", "^", "#", "%", "[", "]", ".", ":", "$";
static char word[20];//每次扫描提取到的词
bool IsLetter(char ch)
//判断是否为字母,包括下划线
if (ch >= 'a'&&ch <= 'z' || ch >= 'A'&&ch <= 'Z'|| ch=='_')
return true;
else
return false;
bool IsDigit(char ch)
//判断是否为数字
if (ch >= '0' && ch <= '9')
return true;
else
return false;
bool IsSymbol(char ch)
//判断是否为单字符号
if (ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == ';'
|| ch == '(' || ch == ')' || ch == '^' || ch == ',' || ch == '#'
|| ch == '%' || ch == '[' || ch == ']' || ch == '' || ch == ''
|| ch == '.' || ch == ':'|| ch == '$')
return true;
else
return false;
int IsKeyword(char word[])
//判断是否为关键字
for(int i=0;i<32;i++)
if (strcmp(word,Keywords[i])==0)
return i + 1;
return -1;
int LexicalAnalyzer(char CurrentCode[], int &cc)
//词法分析
int i, w = 0, cn;
while (CurrentCode[cc] == ' ')
cc++;
for (i = 0; i<20; i++)
word[i] = '\\0';
if (IsLetter(CurrentCode[cc]))
word[w] = CurrentCode[cc];
w++;
cc++;
while (IsLetter(CurrentCode[cc]) || IsDigit(CurrentCode[cc]))
word[w] = CurrentCode[cc];
w++;
cc++;
word[w] = '\\0';
cn = IsKeyword(word);
if(cn == -1)
return 33;
else
return cn;
else if (IsDigit(CurrentCode[cc]))
while (IsDigit(CurrentCode[cc]))
word[w] = CurrentCode[cc];
w++;
cc++;
word[w] = '\\0';
return 34;
else if (IsSymbol(CurrentCode[cc]))
word[0] = CurrentCode[cc];
word[1] = '\\0';
cc++;
for (i = 0; i<30; i++)
if (strcmp(word, Symbol[i]) == 0)
return 35 + i;
else if (CurrentCode[cc] == '<')
cc++;
if (CurrentCode[cc] == '=')
cc++;
return 45;
else
return 43;
else if (CurrentCode[cc] == '>')
cc++;
if (CurrentCode[cc] == '=')
cc++;
return 44;
else
return 42;
else if (CurrentCode[cc] == '=')
cc++;
if (CurrentCode[cc] == '=')
cc++;
return 40;
else
return 39;
else if (CurrentCode[cc] == '!')
cc++;
if (CurrentCode[cc] == '=')
cc++;
return 41;
else
return 52;
else if (CurrentCode[cc] == '&')
cc++;
if (CurrentCode[cc] == '&')
cc++;
return 54;
else
return 53;
else if (CurrentCode[cc] == '|')
cc++;
if (CurrentCode[cc] == '|')
cc++;
return 56;
else
return 55;
else if (CurrentCode[cc] == '\\0')
cc++;
return 0;
else
cc++;
return -2;
typedef struct
//栈的结构体
char *base;
char *top;
int stacksize;
Stack;
int InitStack(Stack &S)
//创造一个空栈
S.base=(char *)malloc(100*sizeof(char));
if(!S.base)
exit(-2);
S.top=S.base;
S.stacksize=100;
return 1;
;
int DestroyStack(Stack &S)
//销毁栈S
while(S.top!=S.base)
free(--S.top);
return 1;
int StackEmpty(Stack S)
//判断是否为空栈
if(S.top==S.base)
return 1;
else
return 0;
int GetTop(Stack S,char &e)
//若栈不为空,取栈顶元素e
if(S.base==S.top)
return 0;
e=*(S.top-1);
return 1;
int Push(Stack &S,char e)
//将e压入栈顶
*S.top=e;
S.top++;
return 1;
int Pop(Stack &S,char &e)
//若栈不为空,将栈顶元素e出栈
if(S.top==S.base)
return 0;
e=*--S.top;
return 1;
int StackTraverse(Stack S)
//遍历栈中的元素
if(S.base==NULL)
return -1;
if(S.base==S.top)
printf("栈中没有元素\\n");
char *p;
p=S.top;
while(p-S.base>0)
p--;
printf("%c ",*p);
printf("\\n");
return 1;
int ReverseTraverse(Stack S)
//遍历输出从栈底至栈顶的元素
if(S.base==NULL)
return -1;
if(S.base==S.top)
printf("\\t栈中没有元素\\n");
char *p,a[30];
int i=0;
p=S.base;
while(p<S.top)
a[i]=*p;
i++;
p++;
a[i]='\\0';
printf("\\t%-15s",a);
return 1;
void SetFontRed()
//字体变红
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED);
void SetFontWhite()
//字体变白
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
char row[17]=
//SLR分析表中的行头
'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g'
;
char column[12]=
//SLR分析表中的列头
'i','n','+','-','*','/','(',')','$','E','T','F'
;
char TableValue[10];//读取到的SLR分析表中的内容
char SLR1Table[17][12][10]=
//将SLR分析表输入进行存储
"s5","s6","e1","e1","e1","e1","s4","e2","e1","1","2","3",
"e3","e3","s7","s8","0","0","e3","e2","acc","0","0","0",
"e3","e3","r3","r3","s9","sa","e3","r3","r3","0","0","0",
"e3","e3","r6","r6","r6","r6","e3","r6","r6","0","0","0",
"s5","s6","e1","e1","e1","e1","s4","e1","e1","b","2","3",
"0","0","r8","r8","r8","r8","e3","r8","r8","0","0","0",
"e3","0","r9","r9","r9","r9","e3","r9","r9","0","0","0",
"s5","s6","e1","e1","e1","e1","s4","e1","e1","0","c","3",
"s5","s6","e1","e1","e1","e1","s4","e1","e1","0","d","3",
"s5","s6","e1","e1","e1","e1","s4","e1","e1","0","0","e",
"s5","s6","e1","e1","e1","e1","s4","e1","e1","0","0","f",
"e3","e3","s7","s8","0","0","e3","sg","e4","0","0","0",
"e3","e3",语法分析 作业4 LR SLR编译原理