導航:首頁 > 編程語言 > 遞歸下降識別程序

遞歸下降識別程序

發布時間:2023-03-23 01:49:55

❶ 循環語句的語法分析及語義分析程序設計

目 錄
1 課程任務書····································(2)
1問題描述·······································(3)
2文法及屬性文法的描述···························(3)
2.1 while-do循環語句的文法·····················(3)
2.2while-do循環語句的結構翻譯·················(3)
3語法分析及中間代碼形式的描述···················(4)
3.1 語法分析方法·······························(4)
3.2 中間代碼形式描述···························(4)
4簡要的分析與概要設計···························(5)
4.1詞法分析··································(5)
4.2遞歸下降翻譯器的設計·······················(5)
4.3語法制導翻譯·······························(5)
5 詳細的演算法描述································(6)
5.1 文法·······································(6)
5.2 查錯·······································(6)
6 測試方法和測試結果···························(9)
6.1測試方法··································(9)
6.2測試結果··································(10)
7 設計的特點、不足、收獲與體會·················(10)
7.1 設計的特點································(10)
7.2 不足、收獲與體會··························(11)
8 參考文獻·····································(11)

課程設計任務書
題 目: 循環語句的語法分析及語義分析程序設計(遞歸下降法)
1.目的
通過設計、編制、調試一個語法及語義分析程序,加深對語法及語義分析原理的理解。

2.設計內容及要求
WHILE〈布爾表達式〉DO〈賦值語句〉
其中
(1)學號29至32的同學按順序分別選擇遞歸下降法、LL(1)、算符優先分析法(或簡單優先法)、LR法完成以上任務,中間代碼選用四元式。
(2)如1題寫出符合分析方法要求的文法,給出分析方法的思想,完成分析程序設計。腔迅
(3)編制好分析程序後,設計若干用例,上機測試並通過所設計的分析程序。

3.課程設計報告書的內容應包括:
1.設計題目、班級、學號、姓名、完成日期;
2.給出語法分析方法及中間代碼形式的描述、文法和屬伍歲此性文法的設計;或者詞法分析方法
3.及符號表和TOKEN代碼的設計。
4.簡要的分析與概要設計;
5.詳細的演算法描述;
6.源程序清單;
7.給出軟體的測試方法和測試結果;
8.設計的評價、收獲與體會。

4.時間安排:
第17周,周1-周4上午,周五全天

指導教師簽名: 年 月 日
系主任(或責任教師)簽名: 年 月 日

1問題描述
設計一個WHILE〈布爾表達式〉DO〈賦值語句〉循環語句的詞法﹑語法及語義分析程序,語法分析選擇遞歸下降法,採用用語法制導翻譯輸出中間代碼四元式。
2文法及屬性文法的描述。
2.1 while-do循環語句的文法
產生式為S-> while E do A,為便於語法雀岩制導翻譯將其改寫如下:
文法G(s)如下:
S-->WEDG (意思是while E do G)
G-->c=R
R-->dTe|d
T-->+|-|*|/
E-->aFb
F--> >|==|<

2.2 whlie-do循環語句的結構翻譯:

3.語法分析方法及中間代碼形式的描述
3.1語法分析方法
遞歸下降法的實現思想是為文法的每個非終結符號設計一個相對應的遞歸子程序,識別程序由一組這樣的子程序組成。
它的優點是簡單直觀,易於構造,很多編譯系統所實現
缺點是對文法要求很高,由於遞歸調用多,影響分析器的效率
其文法可以表示為:
E→T│E+T
T→F│T*F
F→i│(E)
可以用語法圖來表示語言的文法,如圖:

E

T

F

3.2中間代碼形式描述
中間代碼採用四元式輸出,一個四元式是一個帶有四個域的記錄結構,這四個域分別稱為op﹑arg1﹑arg2及result。域op包含一個代表運算符的內部碼。語句while a<b do a=a+b的四元式輸出形式如下:
100 ( <, a , b , 102 )
101 ( j , _ , _ , 105 )
102 ( + , a , b , n )
103 ( = , n , _ , a )
104 ( j , _ , _ , 100)
105
4.簡要的分析與概要設計
4.1詞法分析
詞法分析程序的任務是:從左至右逐個字元地對源程序進行掃描,產生一個個的單詞符號,把作為字元串的源程序改造成為單詞符號的中間程序。詞法分析檢查的錯誤主要是挑出源程序中出現的非法符號。所謂非法符號是指不是程序設計語言中允許出現的符號,就像自然語句中的錯字。
4.2遞歸下降翻譯器的設計
1.:對每個非終結符A構造一個函數過程,對A的每個繼承屬性設置一個形式參數,函數的返回值為A的綜合屬性,A對應的函數過程中,為出現在A的產生式中的每一個文法符號的每一個屬性都設置一個局部變數。非終結符A對應的函數過程中,根據當前的輸入符號決定使用哪個產生式候選。
2:每個產生式對應的程序代碼中,按照從左到右的次序,對於單詞符號,非3:終結符和語義動作分別做以下工作。
(1)對於帶有綜合屬性x的終結符X,把x的值存入為X,x設置的變數中。然後產生一個匹配X的調用,並繼續讀入一個輸入符號。
(2)對於每個非終結符號B,產生一個右邊帶有函數調用的賦值語句c=B(b1,b2,…,bk)
(3)對於語義動作,把動作的代碼抄進分析器中,用代表屬性的變數來代替對應屬性的每一次引用。
4.3語法制導翻譯
在語法分析過程中,隨著分析的步步進展,根據每個產生式所對應的語義子程序(或語義規則描述的語義動作)進行翻譯。屬性文法的每個符號有屬性,所以每個符號入棧時,必須連屬性一起入棧,這樣,棧符號就由文法符號及存放該符號屬性的域所組成。由於屬性類型不同,屬性域存放的內容就要根據屬性的類型來定。有的可能直接存放屬性值,也有的存放的是指向屬性值的指針。對於綜合屬性,其屬性域不存放其屬性值,而是存放一個指針,指向存貯該屬性值的單元。對於繼承屬性,其屬性域直接保存其屬性值。繼承屬性的屬性域剛入棧時為空,但是在該棧符號變成棧頂符號之前的某一時刻,它們必須接受相應的屬性值,即在成為棧頂時,繼承屬性的屬性域必須有值。
5詳細的演算法描述
5.1 文法
/*
文法G(s)
s-->WEDG
G-->c=R
R-->dTe|d
T -> +|-|*|/|%E-->aFb
F--> >|==|<
*/
5.2 查錯
按照遞歸下降法求Wa<bDa=a+b,程序的執行順序應該是S()W()EF()D()G()R()T()
S()
void S()
{
printf("%d\tS-->WEDG\n",total);total++;
W();
E();
}

W()
void W()
{
if(ch!='W')
{
printf("有非法字元%c請按回車返回!!",ch);
getchar();
getchar();
exit(1);
}
}

E()
void E()
{
ch=a[++i1];
if(ch!='a')
{
printf("有非法字元%c %c請按回車返回!!",ch);
getchar();
getchar();
exit(1);
}
printf("%d\tE-->aFb\n",total);total++;
F();
}

F()
void F()
{
int i;
ch=a[++i1];
i=i1+1;
if(a[i]!='b')
{
printf("有非法字元%c請按回車返回!!",a[i]);
getchar();
getchar();
exit(1);
}
switch(ch)
{
case '>':
printf("%d\tF-->>\n",total);total++;
break;
case '==':
printf("%d\tF-->==\n",total);total++;
break;
default:
printf("%d\tF--><\n",total);total++;
break;
}
D();
G();
}

D()
void D()
{
++i1;
ch=a[++i1];
if(ch!='D')
{
printf("有非法字元%c請按回車返回!!",ch);
getchar();
getchar();
exit(1);}
ch=a[++i1];
}

G()
void G()
{
int i=i1+1;
if(ch!='c'&&a[i]!='=')
{
printf("有非法字元%c %c請按回車返回!!",ch,a[i]);
getchar();
getchar();
exit(1);
}
printf("%d\tG-->c=R\n",total);total++;
R();
}

R()
void R()
{
int i;
i=i1+1;
i1=i1+2;
ch=a[i1];
if(a[i]!='='&&ch!='d')
{
printf("有非法字元%c %c請按回車返回!!",a[i],ch);
getchar();
getchar();
exit(1);
}
else
{
if((a[i1+1]=='+')||(a[i1+1]=='-')||(a[i1+1]=='*')||(a[i1+1]=='/'))
{
printf("%d\tR-->dTe\n",total);total++;
T();
}
else
{
printf("%d\tR-->d\n",total);total++;
W();
E();
}
}
}

T()
void T()
{
ch=a[++i1];
switch(ch)
{
case '+':
printf("%d\tT-->+\n",total);total++;
break;
case '-':
printf("%d\tT-->-\n",total);total++;
break;
case '*':
printf("%d\tT-->*\n",total);total++;
break;
default:
printf("%d\tT-->/\n",total);total++;
break;
}
ch='#';
}

6測試方法和測試結果
6.1測試方法
在C++環境下,設計幾個有代表的用例,進行測試,例如:輸入語句Wa<bDa=a+b#(其中d表示do ,w表示while)。若得出的不是預期的結果,那麼程序就出現問題。如果有問題的話就進行單步調試找到程序中出現的邏輯問題。

6.2測試結果
測試結果如下:

7設計的特點、不足、收獲與體會
7.1設計的特點
本次設計是採用遞歸下降的方法對輸入的while--do 循環語句進行語法,語義分析,並輸出四元式。因此程序中充分體現了遞歸下降的思想。

7.2設計的不足,收獲與體會
本次的設計的不足主要是我沒將程序一般化,實現不了用戶自動輸入代碼進行詞法分析的四元式輸出,此程序只能實現對Wa<bDa=a+b#的分析與四元式輸出,由於我所設計的棧中只能一個字元一個字元的存放,因此只能用D W分別表示do while;而且我對語法制導翻譯這一塊很不熟悉,因此我始終不能用程序實現語法制導翻譯輸出四元式,於是根據自己的理解,直接把四元式寫了出來。
本次課程設計鞏固了我所學習的關於遞歸下降法這一方面的知識,並且使我對WHILE—DO循環語句也有了更深刻的理解,提高了我的動手能力。

8 課程設計參考資料
1張幸兒 《編譯原理》(第二版)清華大學出版社
2何炎祥 《編譯原理》華中理工大學出版社
3陳火旺 《程序設計語言編譯原理》(第3版)國防工業出版社

本科生課程設計成績評定表
班級:軟體0701姓名:周璐萍學號:0120710680129
序號 評分項目 滿分 實得分
1 學習態度認真、遵守紀律 10
2 設計分析合理性 10
3 設計方案正確性、可行性、創造性 20
4 設計結果正確性 40
5 設計報告的規范性 10
6 設計驗收 10
總得分/等級
評語:

註:最終成績以五級分制記。優(90-100分)、良(80-89分)、中(70-79分)、
及格(60-69分)、60分以下為不及格
源程序
#include <stdio.h>
#include<dos.h>
#include<stdlib.h>
#include<string.h>

char a[50],g[50][50];
char ch;
int n1,i1=0,i2=0;
int total=0;

void S();
void D();
void G();
void W();
void E();
void R();
void T();
void F();

void main()
{
int j=0;

printf("文法G(s)為:\n");
printf("s-->DGWE\n");
printf("G-->c=R\n");
printf("R-->dTe|d\n");
printf("T-->+|-|*|/\n");
printf("E-->aFb\n");
printf("F--> >|==|<\n");

printf("請輸入while-do語句(D代表do,W代表while),並以#結束:\n");
do{
scanf("%c",&ch);
a[j]=ch;
j++;
}while(ch!='#');
n1=j;
ch=a[0];

S();
printf("\n");

if (ch=='#')
{ printf("輸出四元式為:\n");
printf("100 (<,a,b,102)\n");
printf("101 (j,_,_,105)\n");
printf("102 (+,a,b,n)\n");
printf("103 (=,n,_,a)\n");
printf("104 (j,_,_,100)\n");
printf("105 \n");

}

else {

printf("error\n");

printf("press any key to continue..\n");

getchar();getchar();

return;

}

printf("\n");

printf("press any key to continue..\n");

getchar();
getchar();
}

/*出錯情況分析*/

void S()
{
printf("%d\tS-->WEDG\n",total);total++;
W();
E();
}

void W()
{

if(ch!='W')
{
printf("有非法字元%c請按回車返回!!",ch);
getchar();
getchar();
exit(1);
}
}

void E()
{
ch=a[++i1];
if(ch!='a')
{
printf("有非法字元%c %c請按回車返回!!",ch);
getchar();
getchar();
exit(1);
}
printf("%d\tE-->aFb\n",total);total++;
F();
}

void F()
{
int i;
ch=a[++i1];
i=i1+1;
if(a[i]!='b')
{
printf("有非法字元%c請按回車返回!!",a[i]);
getchar();
getchar();
exit(1);
}
switch(ch)
{
case '>':
printf("%d\tF-->>\n",total);total++;

break;
case '==':
printf("%d\tF-->==\n",total);total++;

break;
default:
printf("%d\tF--><\n",total);total++;

break;

}
D();
G();
}

void D()
{ ++i1;
ch=a[++i1];
if(ch!='D')
{ printf("有非法字元%c請按回車返回!!",ch);
getchar();
getchar();
exit(1);}
ch=a[++i1];

}

void G()
{ int i=i1+1;

if(ch!='c'&&a[i]!='=')
{ printf("有非法字元%c %c請按回車返回!!",ch,a[i]);
getchar();
getchar();
exit(1);}
printf("%d\tG-->c=R\n",total);total++;
R();
}

void R()
{
int i;
i=i1+1;
i1=i1+2;
ch=a[i1];
if(a[i]!='='&&ch!='d')
{
printf("有非法字元%c %c請按回車返回!!",a[i],ch);
getchar();
getchar();
exit(1);
}
else
{
if((a[i1+1]=='+')||(a[i1+1]=='-')||(a[i1+1]=='*')||(a[i1+1]=='/'))
{
printf("%d\tR-->dTe\n",total);total++;

T();

}
else
{
printf("%d\tR-->d\n",total);total++;

W();
E();
}
}

}

void T()
{
ch=a[++i1];
switch(ch)
{
case '+':
printf("%d\tT-->+\n",total);total++;

break;
case '-':
printf("%d\tT-->-\n",total);total++;

break;
case '*':
printf("%d\tT-->*\n",total);total++;

break;

default:
printf("%d\tT-->/\n",total);total++;

break;
}
ch='#';

}

指導教師簽名:
2010 年月日

❷ 遞歸下降分析程序(最好用java和C++編寫)

package com.hyh.recursive;

import java.util.Scanner;
/*
* 測試( a ) + b或者a * + b
*
*
*/
public class test2 {

private static int index = 0;
private static String[] string = new String[5];

static void E() {
// E ->TE』
T();
E1();
System.out.println("E-> TE』");
}

static void E1() {
// E』-> +E|ε
if (string[index].equals("+")) {
index++;
E();
System.out.println("E』-> +E");
}
}

static void T() {
// T ->FT』
F();
T1();
System.out.println("T-> FT』");
}

static void T1() {
// T' ->T|ε
T();
System.out.println("T'-> T");
}

static void F() {
// F ->PF'
P();
F1();
System.out.println("F-> PF'");
}

static void F1() {
//F' ->*F'|ε
if (string[index].equals("*")) {
index++;
F1();
System.out.println("F』-> *F'");
}
}

static void P() {
//P ->(E)|a|b|^
if (string[index].equals("a")) {
index++;
System.out.println("F-> a");
} else if(string[index].equals("b")) {
index++;
System.out.println("F-> b");
}else if(string[index].equals("^")) {
index++;
System.out.println("F-> ^");
}else if (string[index].equals("(")) {
index++;
E();
if (string[index].equals(")")) {
index++;
System.out.println("F->(E)");
} else {
System.out.println("分析失敗");
}
} else {
System.out.println("分析失敗");
}
}

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);
//System.out.println("請輸入算數表達式");
while (!sc.equals("----")) {
System.out.println("請輸入算數表達式");
string[4] = "#";
for (int i = 0; i < 4; i++) {
string[i] = sc.next();
try {
if (string[i].matches("^(-?\\d+)(\\.\\d+)?$")) {
string[i] = "i";
} else if (string[i].matches("^-?\\d+$")) {
string[i] = "i";
}
} catch (Exception e) {
}
}
E();
System.out.println("正確語句");
//string.notifyAll();
}
}
}

❸ 遞歸下降分析器的設計

http://hi..com/srlhc/blog
我放了一個在里飢叢歷面,希望對鄭閉你爛搜有用。

❹ 遞歸下降分析法

# include <iostream>
# include <cstdlib>
# include <跡亂滾cctype>
# include <csetjmp>

// Error jmp_buf buffer
static std::jmp_buf errjb;

// Function prototypes.
int addsubt();
int multdiv();
int number();
void error();

// Global expression buffer.
static char expr[81];
static int pos;

////////////////////////////
// The main() function. //
/陪鬧//////////////////////////
int main()
{
int ans;

do
{
// Mark the top of the parsing descent.

if(setjmp(errjb) == 0)
{
// Initialize the string subscript.
pos = 0

// Read an expression.
std::cout << "Enter expression(0 to quit):" << std::endl;
std::cin >>expr;

// Evaluate the expression.
ans = addsubt();
if(expr[pos] != '\0')
error();
if(ans != 0)
std::cout << ans << std:endl;
}
else
{
// An error occurred.
std::cout << "Try again" << std::endl;
ans = 1;
}
}
while(ans != 0);

return 0;
}

///////////////////////////////////////////////////
// Top of recursive descent: add/subtract. //
//////////////////////////////////////////////
int addsubt()
{
int rtn = multdiv();
while(expr[pos] == '+' || expr[pos] == '-')
{
int op = expr[pos++];
int opr2 = multdiv();
if(op == '+')
rtn += opr2;
else
rtn -= opr2;
}

return rtn;
}

/////////////////////////////////姿余///////////
// Highest precedence: multiply/divide. //
///////////////////////////////////////////
int multdiv()
{
int rtn = number();
while(expr[pos] == '*' || expr[pos] == '/')
{
int op = expr[pos++];
int opr2 = number();

if(op == '*')
rtn *= opr2;
else
rtn /= opr2;
}

return rtn;
}

/////////////////////////
// Extract a number //
///////////////////////
int number()
{
int rtn;
if(expr[pos] == '(')
{
// Parenthetical expression.
pos++;
rtn = addsubt(); // Back to top.
if(expr[pos++] != ')') // Must have ')'
error();
return rtn;
}

// Extract the number.
if(!isdigit(expr[pos]))
error;
rtn = atoi(expr + pos);
while(isdigit(expr[pos]))
pos++;

return rtn;
}

////////////////////////
// Syntax error. //
//////////////////////
void error()
{
std::cout << '\r';
while(pos--) // Position error pointer.
std::cout << ' ';
std::cout << "A syntax error" << std::endl << '\a';

// Return to the top of the program.
std::longjmp(errjb,1);
}
}

❺ 求一個遞歸下降的語法分析程序

3.對修改後的文法,用遞歸子程序法構造語法分析程序 (C語言編寫) 常工院的仁兄啊?

❻ 編譯遞歸下降分析法

#include <stdio.h>
#include<dos.h>
#include<stdlib.h>
#include<string.h>

char a[50] ,b[50],d[200],e[10];
char ch;
int n1,i1=0,flag=1,n=5;

int E();
int E1();
int T();
int G();
int S();
int F();
void input();
void input1();
void output();

void main() /*遞歸分析*/
{
int f,p,j=0;
char x;
d[0]='E';
d[1]='=';
d[2]='';
d[3]='T';
d[4]='G';
d[5]='#';
printf("請輸入字元串(長度<50,以#號結束)\n");
do{
scanf("%c",&ch);
a[j]=ch;
j++;
}while(ch!='#');
n1=j;
ch=b[0]=a[0];
printf("文法\t分析串\t\t分析字元\t剩餘串\n");
f=E1();
if (f==0) return;
if (ch=='#')
{ printf("accept\n");
p=0;
x=d[p];
while(x!='#') {
printf("%c",x);p=p+1;x=d[p]; /*輸出推導式*/
}
}
else {
printf("error\n");
printf("回車返回\n");
getchar();getchar();
return;
}
printf("\n");
printf("回車返回\n");
getchar();
getchar();
}

int E1()
{ int f,t;
printf("E--TG\t");
flag=1;
input();
input1();
f=T();
if (f==0) return(0);
t=G();
if (t==0) return(0);
else return(1);
}

int E()
{ int f,t;
printf("E--TG\t");
e[0]='E';e[1]='=';e[2]='';e[3]='T';e[4]='G';e[5]='#';
output();
flag=1;
input();
input1();
f=T();
if (f==0) return(0);
t=G();
if (t==0) return(0);
else return(1);
}

int T()
{ int f,t;
printf("T--FS\t");
e[0]='T';e[1]='=';e[2]='';e[3]='F';e[4]='S';e[5]='#';
output();
flag=1;
input();
input1();
f=F();
if (f==0) return(0);
t=S();
if (t==0) return(0);
else return(1);
}

int G()
{ int f;
if(ch=='+') {
b[i1]=ch;
printf("G--+TG\t");
e[0]='G';e[1]='=';e[2]='';e[3]='+';e[4]='T';e[5]='G';e[6]='#';
output();
flag=0;
input();input1();
ch=a[++i1];
f=T();
if (f==0) return(0);
G();
return(1);
}
printf("G--^\t");
e[0]='G';e[1]='=';e[2]='';e[3]='^';e[4]='#';
output();
flag=1;
input();input1();
return(1);
}

int S()
{
int f,t;
if(ch=='*') {
b[i1]=ch;printf("S--*FS\t");
e[0]='S';e[1]='=';e[2]='';e[3]='*';e[4]='F';e[5]='S';e[6]='#';
output();
flag=0;
input();input1();
ch=a[++i1];
f=F();
if (f==0) return(0);
t=S();
if (t==0) return(0);
else return(1);}
printf("S--^\t");
e[0]='S';e[1]='=';e[2]='';e[3]='^';e[4]='#';
output();
flag=1;
a[i1]=ch;
input();input1();
return(1);
}

int F()
{ int f;
if(ch=='(') {
b[i1]=ch;printf("F--(E)\t");
e[0]='F';e[1]='=';e[2]='';e[3]='(';e[4]='E';e[5]=')';e[6]='#';
output();
flag=0;
input();input1();
ch=a[++i1];
f=E();
if (f==0) return(0);
if(ch==')') {
b[i1]=ch;printf("F--(E)\t");
flag=0;input();input1();
ch=a[++i1];
}
else {
printf("error\n");
return(0);
}
}
else if(ch=='i') {
b[i1]=ch;printf("F--i\t");
e[0]='F';e[1]='=';e[2]='';e[3]='i';e[4]='#';
output();
flag=0;input();input1();
ch=a[++i1];
}
else {printf("error\n");return(0);}
return(1);
}

void input()
{
int j=0;
for (;j<=i1-flag;j++)
printf("%c",b[j]); /*輸出分析串*/
printf("\t\t");
printf("%c\t\t",ch); /*輸出分析字元*/
}

void input1()
{
int j;
for (j=i1+1-flag;j<n1;j++)
printf("%c",a[j]); /*輸出剩餘字元*/
printf("\n");
}

void output(){ /*推導式計算*/
int m,k,j,q;
int i=0;
m=0;k=0;q=0;
i=n;
d[n]='=';d[n+1]='';d[n+2]='#';n=n+2;i=n;
i=i-2;
while(d[i]!=''&&i!=0) i=i-1;
i=i+1;
while(d[i]!=e[0]) i=i+1;
q=i;
m=q;k=q;
while(d[m]!='') m=m-1;
m=m+1;
while(m!=q) {
d[n]=d[m];m=m+1;n=n+1;
}
d[n]='#';
for(j=3;e[j]!='#';j++){
d[n]=e[j];
n=n+1;
}
k=k+1;
while(d[k]!='=') {
d[n]=d[k];n=n+1;k=k+1;
}
d[n]='#';
}

這是我們用到的程序,看看對你有沒有幫助。
至於下面的兩個問題,很簡單,可以容易做出來吧!
不會可以和我交流!
祝 進步!

❼ 構造如下文法的遞歸下降分析程序 exp→+ num exp | - num exp | ε

文法遞歸,從實現來上,自主要進行兩個步驟:
1 詞法分析後,建立語法單元。
2 將語法單元根據操作符建立語法樹,並對語法進行正確性校驗。

如果不是要求一定要全部自己寫,完全可以依賴詞法和語法分析工具
例如:
1 老牌的有yacc/lex,新版本bison/flex。
2 新的有java的javacc,跨語言的ANTLR。
這些都是寫好符合自己規則的文法解析規則,自動生成解析程序。然後自己加入文法執行邏輯。

如果不是學習,不建議自己寫文法分析程序。如果是學習,主要的工作還是在語法樹的建立上。

❽ 求解編譯原理題 構造遞歸下降分析程序

兄弟,其實這個問題不難,只是我感覺你給的問題不完全,不知道怎麼給你解決,要不我給你個遞歸下降分析方法分析文法的例子吧,你先看看。我估計你看了這個後絕對就明白整體的分析思想了~
文法如下:
S->do s1 begin s2 end while s2
s1->s2mB
s2->AmB
A->B
B->VAL //VAL是所有非關鍵字的字元字元組和數字
m->+|-|*|/|>|<|=

/////////////*語法遞歸分析*/////////////////
int B(int * c,int & q)
{
if(c[q]==13)
{ q++;
return 1;
}
else if(c[q]==5)
{ q++;
return 1;
}
else {cout<<"error 'B'"<<endl;return 0;}
}

int A(int * b,int & o)
{
if(B(b,o))
{
return 1;
}
else {cout<<"error 'A'"<<endl;return 0;}
}
int OP(int *r,int & v)
{
if(r[v]==6)
{
v++;
return 1;
}
else if(r[v]==7)
{
v++;
return 1;
}
else if(r[v]==8)
{
v++;
return 1;
}
else if(r[v]==9)
{
v++;
return 1;
}
else if(r[v]==10)
{
v++;
return 1;
}
else if(r[v]==11)
{
v++;
return 1;
}
else if(r[v]==12)
{
v++;
return 1;
}
else {cout<<"error 'OP'"<<endl;return 0;}
}
int S2(int * d,int & h)
{
if(A(d,h))
{
if(OP(d,h))
{
if(B(d,h))
return 1;
else {cout<<"條件語句右部出錯 "<<endl;return 0;}
}
else {cout<<"條件語句缺少運算符 "<<endl;return 0;}
}
else {cout<<"條件語句左部出錯 "<<endl;return 0;}
}

int S1(int * d,int & h)
{
if(S2(d,h))
{
if(OP(d,h))
{
if(B(d,h))

return 1;
else {cout<<"循環語句右部出錯 "<<endl;return 0;}
}
else {cout<<"循環語句缺少運算符 "<<endl;return 0;}
}
else {cout<<"循環語句左部出錯 "<<endl;return 0;}
}

int S(int *a,int & z)
{
if (a[z++]==1)
{
if (S1(a,z))
{
if (a[z++]==2)
{
if (S2(a,z))
{
if(a[z++]==3)
{
if(a[z++]==4)
{
if(S2(a,z))
{
cout<<"語法分析成功!"<<endl;
return 1;
}
else return 0;
}
else {cout<<"error 'while'"<<endl; return 0;}
}
else {cout<<"error 'end'"<<endl; return 0;}
}
else return 0;
}
else {cout<<"error 'begin'"<<endl; return 0;}
}
else return 0;
}
else {cout<<"error 'do'"<<endl; return 0;}
}
/////////////////////// 語法分析結束 ////////////////////////

詞法分析中單詞代號 詞法分析中可識別到的所有單詞
1 do
2 begin
3 end
4 while
5 除關鍵字以外的所有字元或字元串
6 +
7 -
8 *
9 /
10 =
11 >
12 <
13 數字
14 結束標識符「#」

自己好好琢磨琢磨吧,其實思想挺簡單的~

❾ 具有左遞歸的文法對遞歸下降分析有何影響

你說的應該是編譯原理吧。
遞歸下降分析程序的實現思想是:識別程序由一組子程序組成。每個子程序對應於一個非終結符號。
每一個子程序的功能是:選擇正確的右部,掃描完相應的字。在右部中有非終結符號時,調用該非終結符號對應的子程序來完成。

所以,當有左遞歸出現時,遞歸下降分析程序就會出現回朔,將可能產生無限的循環,所以遞歸下降分析的前提條件之一就是消除左遞歸。

❿ 編譯原理,遞歸下降子程序語法分析

沒學過編譯原理,看描述,是讓寫一個腳本執行軟體。
終結符我查了下,就是不可再分的。比如iε。
輸入是EGTSFI*/ε組成的字元串。
規則需要預處理。注意轉意符在字元串中的效果。因為有/字元。

不會c或c++,只會c#。你可以到貼吧發帖。強人工智慧吧 就挺好。演算法吧有點亂。

最重要的,不要錢。

閱讀全文

與遞歸下降識別程序相關的資料

熱點內容
windows7系統共享文件 瀏覽:62
ps前往文件夾 瀏覽:694
信捷plc編程用哪個軟體 瀏覽:939
vba導入文件 瀏覽:690
更新後版本英文怎麼說 瀏覽:267
桌面雲配置文件分離 瀏覽:505
iphone5如何升級4g網路 瀏覽:5
團購是在哪個app 瀏覽:897
打開多個word文檔圖片就不能顯示 瀏覽:855
騰訊新聞怎麼切換版本 瀏覽:269
app安裝失敗用不了 瀏覽:326
桌面文件滑鼠點開會變大變小 瀏覽:536
手機誤刪系統文件開不了機 瀏覽:883
微信兔子甩耳朵 瀏覽:998
android藍牙傳文件在哪裡 瀏覽:354
蘋果6s軟解是真的嗎 瀏覽:310
c語言代碼量大 瀏覽:874
最新網路衛星導航如何使用 瀏覽:425
以下哪些文件屬於圖像文件 瀏覽:774
zycommentjs 瀏覽:414

友情鏈接