當前位置:首頁 » 編程軟體 » 編譯原理預測分析法代碼

編譯原理預測分析法代碼

發布時間: 2022-03-12 12:14:52

1. 編譯原理用c語言實現基於LR(1)或SLR(1)語法分析程序代碼,最好還有報告,急。。。

#include <iostream>
using namespace std;
#define m 45
#define n 100
#define t 10
int main()
{
FILE *fp;
char filename[20],c[n];
printf("Type the file name which you want to open:");
scanf("%s",&filename);
fp=fopen(filename,"r+"); /*以r-只讀方式打開指定文件*/
if((fp=fopen(filename,"r"))==NULL) /*文件不存在輸出錯誤*/
{cout<<"文件不存在!"<<endl;exit(-1);}
cout<<"文件中內容如下:"<<endl;
for(int j=0;!feof(fp);j++){
c[j]=fgetc(fp);//從流中讀取字元
}
char keyword[m][t]={"include","int","string","cout","cin","auto","break","case","char","class","const",
"continue","default","delete","do","double","else","enum","extern","float","for","friend","if","inline",
"int","long","new","operator","private","protected","public","register","return","short","sizeof","static",
"struct","switch","template","this","typedef","union","virtual","void","while"};//關鍵字數組
char a[t],*p=c,*q=a,*s=a;
bool w=0,r=0;
int i=0;
for(i=0;i<10;i++)a[i]=NULL;//初始化臨時數組
while (*p !=NULL){
q=s=a;
if((*p>='a'&&*p<='z')||(*p>='A'&&*p<='Z')||*p=='_'){//識別標識符
*q=*p;p++;q++;
while ((*p>='a'&&*p<='z')||(*p>='A'&&*p<='Z')||(*p>='0'&&*p<='9')||*p=='_'){
*q=*p;p++;q++;
}
for(i=0;i<m;i++)if(strcmp(keyword[i],a)==0){r=1;break;}
if(r==1){cout<<"關鍵字為:";r=0;}
else cout<<"標識符為:";
while(s!=q){
cout<<*s;
s++;
}
cout<<endl;
for(i=0;i<t;i++)a[i]=NULL;
}
else if(*p=='\''){//識別字元常量
p++;
while(*p!='\''){

*q=*p;
p++;
q++;
}
cout<<"字元常量為:";
while(s!=q){
cout<<*s;
s++;
}
cout<<endl;
for(i=0;i<t;i++)a[i]=NULL;
p++;
}
else if(*p=='\"'){//識別字元串常量
p++;
while(*p!='\"'){
*q=*p;
p++;
q++;
}
cout<<"字元串常量為:";
while(s!=q){
cout<<*s;
s++;
}
cout<<endl;
for(i=0;i<t;i++)a[i]=NULL;
p++;
}
else if(*p=='+'||*p=='-'||*p=='*'||*p=='/'||*p=='='||*p=='%'||*p=='/'){//識別運算符
cout<<"運算符為:"<<*p;
cout<<endl;
p++;
}
else if(*p==';'||*p==','){//識別分解符
cout<<"分界符為:"<<*p;
cout<<endl;
p++;
}
else if(*p>='0'&&*p<='9'){
s=q=a;
*q=*p;p++;q++;
while(*p>='0'&&*p<='9'||*p=='.'){
*q=*p;p++;q++;
}
while(s!=q){
if(*s=='.'){w=1;break;}//識別實型常量
s++;
}
s=a;
if(w ==1){
cout<<"實型常量為:";
while(s!=q){
cout<<*s;
s++;
}
for(i=0;i<t;i++)a[i]=NULL;
}
else {
cout<<"整型常量為:";
while(s!=q){//識別整型常量
cout<<*s;
s++;
}
for(i=0;i<t;i++)a[i]=NULL;
}
cout<<endl;
}
else p++;
}
return 0;
}

2. 編譯原理:預測分析法判斷輸入串i*i+i是否是文法G3的句子

不管什麼法,有表就是查表。 分析自頂向下,搞個棧 ,再搞個輸入:
#E i*i+i$
#E'T i*i+i$
#E'T'F i*i+i$
#E'T'i i*i+i$ 匹配i
#E'T' *i+i$
#E'T'F* *i+i$ 匹配*
#E'T'F i+i$
#E'T'i i+i$ 匹配i
#E'T' +i$
#E' +i$
#E'T+ +i$ 匹配+
#E'T i$
#E'T'F i$
#E'T'i i$ 匹配i
#E'T' $
#E' $
# $ 接受
這樣OK?

3. 速求編譯原理詞法分析器,要求說明語言有完整代碼能運行。急!!!

不好意思 發成語法的了
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <malloc.h>
#include <conio.h>
#define NULL 0
#define true1 1
#define false1 0
FILE *fp;
char ch;
char *keyword[17]={"break","char","continue","do","double",
"else","extern","float","for","if","int","long",
"short","static","switch","void","while"};
char *operatornum[3]={"+","-","*"};
char *comparison[6]={"<","<=","=",">",">=","<>"};
char *interpunction[5]={",",";",":=","(",")"};
char *biaoshifu[6]={"%","$","^","&","_","#"};//
char *zhushifu[3]={"//","/*","*/"};//
char *luoji[3]={"&&","||","!"};//
char search(char searchstr[],int wordtype) {
int i; switch (wordtype) {
case 1:for(i=0;i<=16;i++) {
if(strcmp(keyword[i],searchstr)==0)
return(true1);
} case 2:{
for(i=0;i<=2;i++)
{
if(strcmp(operatornum[i],searchstr)==0)
return(true1);
} break;
} case 3: {
for(i=0;i<=5;i++)
{
if(strcmp(comparison[i],searchstr)==0)
return(true1);
}
break;
}
case 4: for(i=0;i<=4;i++)
{
if(strcmp(interpunction[i],searchstr)==0)
return(true1); }
break; case 5: for(i=0;i<=5;i++)
{
if(strcmp(biaoshifu[i],searchstr)==0)
return(true1);
}break; case 6: for(i=0;i<=2;i++)
{
if(strcmp(zhushifu[i],searchstr)==0)
return(true1);
}break; case 7: for(i=0;i<=2;i++)
{ if(strcmp(luoji[i],searchstr)==0)
return(true1);
}break; }
return(false1);
}
char letterprocess (char ch)
{
int i=-1; char letter[20];
while (isalnum(ch)!=0) {
letter[++i]=ch;
ch=fgetc(fp);
};
letter[i+1]='\0';
if (search(letter,1))
{ if(strcmp(letter,"main"))//
printf("<1,關鍵字,%s>\n",letter);//
else printf("<2,自定義標識符,%s>\n",letter);//
} else { printf("<2,自定義標識符,%s>\n",letter);//
} return(ch); }
char numberprocess(char ch)//
{ int i=-1; char num[20];
while (isdigit(ch)!=0) {
num[++i]=ch;
ch=fgetc(fp); }
if(isalpha(ch)!=0) {
while(isspace(ch)==0)
{
num[++i]=ch;
ch=fgetc(fp);
} num[i+1]='\0';
printf("3,數字,%s\n",num);
goto u; }
num[i+1]='\0'; printf("<3,數字,%s>\n",num);//
u: return(ch); }
char otherprocess(char ch) {
int i=-1;
char other[20];
if (isspace(ch)!=0)
{
ch=fgetc(fp);
goto u; }
while ((isspace(ch)==0)&&(isalnum(ch)==0)) {
other[++i]=ch;
ch=fgetc(fp); }
other[i+1]='\0';
if (search(other,2))
printf("<4,運算符,%s>\n",other);//
else if (search(other,3))
printf("<4,運算符,%s>\n",other);//
else if (search(other,4)) printf("<5,分隔符號,%s>\n",other);//
else if (search(other,5)) printf("<%s,特殊標識符號>\n",other);
else if (search(other,6)) printf("<%s,注釋符號>\n",other);
else if (search(other,7)) printf("<%s,邏輯運算符號>\n",other);
else
printf("錯誤! 非法字元:%s\n",other);
u: return (ch); }
void main () {
char str,c; printf("**********************************詞法分析器***********************************\n");
if((fp=fopen("源程序.txt","r"))==NULL)
printf("源程序無法打開!\n");
else { str =fgetc(fp);

while (str!=EOF) {
if (isalpha(str)!=0)
str=letterprocess(str);
else {
if (isdigit(str)!=0)
str=numberprocess(str);
else
str=otherprocess(str);
}
};
printf("詞法分析結束,謝謝使用!\n");
printf("點任意鍵退出\n");
}
c=getch();
}

這個

4. 學完編譯原理這門課,用c語言或者c++語言,編一個預測分析的程序,對預測分析也至少測試三個句子(含錯誤

我寫好的.
scan.h

/*
* scan.h
* ccompiler
*
* Created by on 09-10-12.
* Copyright 2009 __MyCompanyName__. All rights reserved.
*
*/

#ifndef _SCAN_H_
#define _SCAN_H_

#include <string>
#include <fstream>
using namespace std;

typedef enum
{
ENDFILE,ERROR,
ELSE,IF,INT,RETURN,VOID,WHILE,
ID,NUM,
ASSIGN,EQ,LT,GT,LE,GE,NE,ADD,SUB,MUL,DIV,SEMI,LPAREN,RPAREN,LZK,RZK,LDK,RDK,COMMA
}
TokenType;

class Scan
{
private:
string tokenStr;
string linebuffer;
ifstream * in;
int linepos;
int lineno;
bool EOF_Flag;
bool traceScan;
void printToken(TokenType tt,const string &tok);
public:
Scan(ifstream * in)
{
this->in=in;
linepos=0;
linebuffer="";
lineno=0;
EOF_Flag=false
traceScan=true;
}
char getNextChar();

void ungetNextChar();

TokenType reservedLookup(string &s);

void setTraceScan(bool f);

bool getTraceScan();

TokenType getToken();

string getTokenStr();

};
#endif

scan.cpp

/*
* scan.cpp
* ccompiler
*
* Created by on 09-10-12.
* Copyright 2009 __MyCompanyName__. All rights reserved.
*
*/

#include <string>
#include <fstream>
#include <iostream>
using namespace std;

#include "scan.h"

typedef enum

StateType;

static struct
{
string str;
TokenType tok;
} reservedWords[6]
=,,,,,};

char Scan::getNextChar()
{
if(linepos>=linebuffer.size())
{
if(getline(*in,linebuffer))
{
linebuffer+="\n";
lineno++;
linepos=0;
return linebuffer[linepos++];
}
else
{
EOF_Flag=true;
return EOF;
}
}
else
return linebuffer[linepos++];
}

void Scan::ungetNextChar()
{
if(!EOF_Flag) linepos--;
}

TokenType Scan::reservedLookup(string &s)
{
for(int i=0;i<6;i++)
if(s==reservedWords[i].str)
return reservedWords[i].tok;
return ID;
}

void Scan::setTraceScan(bool f)
{
traceScan=f;
}

bool Scan::getTraceScan()
{
return traceScan;
}

TokenType Scan::getToken()
{
tokenStr="";
TokenType currentToken;
StateType state=START;

while(state!=DONE)
{
bool save=false;
char c=getNextChar();
switch (state) {
case START:
if(c>='0'&&c<='9'){
state=INNUM;
save=true;
}
else if((c>='a'&&c<='z')||(c>='A'&&c<='Z')){
state=INID;
save=true;
}
else if(c==' '||c=='\t'||c=='\n')
{
state=START;
}
else if(c=='/'){
state=SLASH;
}
else if(c=='='){
state=TEMPE;
}
else if(c=='>')
state=TEMPG;
else if(c=='<')
state=TEMPL;
else if(c=='!')
state=INNOTEQ;
else
{
state=DONE;
switch (c) {
case EOF:
currentToken=ENDFILE;
break;
case '+':
currentToken=ADD;
break;
case '-':
currentToken=SUB;
break;
case '*':
currentToken=MUL;
break;
case '(':
currentToken=LPAREN;
break;
case ')':
currentToken=RPAREN;
break;
case '[':
currentToken=LZK;
break;
case ']':
currentToken=RZK;
break;
case '{':
currentToken=LDK;
break;
case '}':
currentToken=RDK;
break;
case ';':
currentToken=SEMI;
break;
case ',':
currentToken=COMMA;
break;
default:
currentToken=ERROR;
break;
}
}
break;
case INNUM:
if(c<'0'||c>'9')
{
ungetNextChar();
state=DONE;
currentToken=NUM;
}
else
save=true;
break;
case INID:
if(!((c>='a'&&c<='z')||(c>='A'&&c<='Z')))
{
ungetNextChar();
state=DONE;
currentToken=ID;
}
else
save=true;
break;
case SLASH:
if (c!='*')
{
state=DONE;
currentToken=DIV;
}
else
state=INCOMMENT1;
break;
case INCOMMENT1:
if (c!='*')
state=INCOMMENT1;
else if(c==EOF){
state=DONE;
currentToken=ENDFILE;
}
else
state=INCOMMENT2;
break;
case INCOMMENT2:
if (c=='*') {
state=INCOMMENT2;
}else if(c=='/'){
state=START;
}else if(c==EOF){
state=DONE;
currentToken=ENDFILE;
}else {
state=INCOMMENT1;
}
break;
case TEMPE:
if (c=='=') {
state=DONE;
currentToken=EQ;
}else{
state=DONE;
ungetNextChar();
currentToken=ASSIGN;
}
break;
case TEMPG:
if (c=='=') {
state=DONE;
currentToken=GE;
}else{
state=DONE;
ungetNextChar();
currentToken=GT;
}
break;
case TEMPL:
if (c=='=') {
state=DONE;
currentToken=LE;
}else{
state=DONE;
ungetNextChar();
currentToken=LT;
}
break;
case INNOTEQ:
if (c=='=') {
state=DONE;
currentToken=NE;
}else {
state=DONE;
ungetNextChar();
currentToken=ERROR;
}
break;

default:
cerr<<"Scanner Bug: state= "<<state<<endl;
state=DONE;
currentToken=ERROR;
break;
}
if(save){
string newChar(1,c);
tokenStr+=newChar;
}
if (state==DONE&¤tToken==ID)
currentToken=reservedLookup(tokenStr);
}
if (traceScan) {
cout<<"Scan at line "<<lineno<<" token: ";
printToken(currentToken, tokenStr);
cout<<endl;
}
return currentToken;
}

string Scan::getTokenStr()
{
return tokenStr;
}

void Scan::printToken(TokenType tt,const string &tok)
{
string type;
switch (tt) {
case ENDFILE:
type="EOF";
break;
case ERROR:
type="ERROR";
break;
case ELSE:
case IF:
case INT:
case RETURN:
case VOID:
case WHILE:
type="reserved word";
break;
case ID:
type="ID";
break;
case NUM:
type="NUM";
break;
case ASSIGN:
type="=";
break;
case EQ:
type="==";
break;
case LT:
type="<";
break;
case GT:
type=">";
break;
case LE:
type="<=";
break;
case GE:
type=">=";
break;
case NE:
type="!=";
break;
case ADD:
type="+";
break;
case SUB:
type="-";
break;
case MUL:
type="*";
break;
case DIV:
type="/";
break;
case SEMI:
type=";";
break;
case LPAREN:
type="(";
break;
case RPAREN:
type=")";
break;
case LZK:
type="[";
break;
case RZK:
type="]";
break;
case LDK:
type="{";
case RDK:
type="}";
break;
case COMMA:
type=",";
break;
default:
break;
}
cout << type<<": "<<tok;
}

main.cpp

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
#include "scan.h"

int main (int argc, char * const argv[]) {
string fileName="/Users/huanglongyin/scan_in.txt";
//cout<< "File name: ";
//cin>>fileName;
ifstream in(fileName.c_str());
if(!in){
cerr<<"Error occurs when openning file "<<fileName<<endl;
return -1;
}
Scan scan(&in);
while(scan.getToken()!=ENDFILE);
return 0;
}

5. 求編譯原理的詞法分析器源碼

/* 我上編譯原理課時的第一次作業就是這個,flex源碼. */
%{
#include<math.h>
int num_lines=0;
%}
DIGIT [0-9]
ID [a-zA-Z_][a-zA-Z0-9]*
%%
"#include" {
printf("<包含頭文件,請手動合並文件\\>\n");
fprintf(yyout,"<包含頭文件,請手動合並文件\\>\n");
}
{DIGIT}+ {
printf("(3整數, \"%s\")\n", yytext);
fprintf(yyout,"(3整數, \"%s\")\n", yytext);
}
{DIGIT}+"."{DIGIT}* {
printf("(3浮點數, \" %s\")\n",yytext);
fprintf(yyout,"(3浮點數, \" %s\")\n",yytext);
}
auto |
break |
case |
char |
const |
continue |
default |
do |
double |
else |
enum |
extern |
float |
for |
goto |
if |
int |
long |
register |
return |
short |
signed |
sizeof |
static |
struct |
switch |
typedef |
union |
unsigned |
void |
volatile |
while {
fprintf(yyout,"(1, \"%s\")\n",yytext);
fprintf(yyout,"(1, \"%s\")\n",yytext);
}
{ID} {
printf("(2, \"%s\")\n",yytext);
fprintf(yyout,"(2, \"%s\")\n",yytext);
}
"+" |
"++" |
"+=" |
"-" |
"--" |
"-=" |
"->" |
"*" |
"**" |
"*=" |
"/" |
"/=" |
"=" |
"==" |
">" |
">>" |
">=" |
">>=" |
"<" |
"<<" |
"<=" |
"<<=" |
"!" |
"!=" |
"%" |
"%=" |
"&" |
"&&" |
"&=" |
"|" |
"||" |
"|=" |
"^" |
"^=" {
printf("(4, \"%s\")\n",yytext);
fprintf(yyout,"(4, \"%s\")\n",yytext);
}
"{" |
"}" |
"(" |
")" |
";" |
"," |
"'" |
"\"" |
"." |
"?" |
"[" |
"]" |
"\\" |
":" {
printf("(5, \"%s\")\n",yytext);
fprintf(yyout,"(5, \"%s\")\n",yytext);
}
\n {
++num_lines;
}
"/*"[^(*/)\n]*"*/"
(" ")+
[\t]+
. {
printf("(不能識別字元, \"%s\")\n",yytext);
fprintf(yyout,"(不能識別字元, \"%s\")\n",yytext);
}
%%
main(argc,argv)
int argc;
char **argv;
{
++argv,--argc;
if(argc>0)
yyin=fopen(argv[0],"r");
else
yyin=stdin;
yyout=fopen("output.txt","w");
yylex();
fclose(yyout);
}
int yywrap()
{
return 1;
}

/* 附:我們第一次作業的要求。
實驗一:用高級語言編寫詞法分析器(用lex生成)一、實驗目的:編制一個識別C語言子集的詞法分析器。從輸入的源程序中,識別出各個具有獨立意義的記號,即基本保留字、標識符、常數、運算符、分隔符五大類。並依次輸出各個記號的內部編碼及記號符號自身值。(遇到錯誤時可顯示「Error」,然後跳過錯誤部分繼續顯示)二、實驗過程和指導:(一)准備:1.閱讀課本有關章節,明確語言的詞法,寫出基本保留字、標識符、常數、運算符、分隔符和程序例。2.初步編制好程序。3.准備好多組測試數據。(二)程序要求:程序輸入/輸出示例:如源程序為C語言。輸入如下一段:main(){ int a,b; a = 10; b = a + 20;}要求輸出如下:(2,」main」)(5,」(「)(5,」)「)(5,」{「)(1,」int」)(2,」a」)(5,」,」)(2,」b」)(5,」;」)(2,」a」)(4,」=」)(3,」10」)(5,」;」)(2,」b」)(4,」=」)(2,」a」)(4,」+」)(3,」20」)(5,」;」)(5,」)「}
要求(滿足以下要求可獲得70%該題的得分):識別保留字:if、int、for、while、do、return、break、continue其他的都識別為標識符;常數為無符號整形數;運算符包括:+、-、*、/、=、>、<、>=、<=、!=分隔符包括:,、;、{、}、(、)以上為參考,具體可自行增刪。 三、實驗檢查:1.程序:輸入:測試數據(以文件形式);輸出:二元組(以文件形式)。2.實驗報告:(1)功能描述:該程序具有什麼功能?(2)狀態轉換圖。(2)程序結構描述:函數調用格式、參數含義、返回值描述、函數功能;函數之間的調用關系圖、程序總體執行流程圖。(4)源程序代碼。(5)實驗過程記錄:出錯次數、出錯嚴重程度、解決辦法摘要。(6)實驗總結:你在編程過程中花時多少?多少時間在紙上設計?多少時間上機輸入和調試?多少時間在思考問題?遇到了哪些難題?你是怎麼克服的?你對你的程序的評價?你的收獲有哪些?

另可附加:關鍵字 有符號數 符號表填寫 行號記錄,等
*/

6. 編譯原理 預測分析程序的C++實現(急啊)

以前做實驗時編了個類似的程序,也許有一定的參考
http://hi..com/dhshy/blog/item/1eebd8589603a284810a184f.html

7. 求 編譯原理 語法分析程序

可以參考我的空間,我在三年前寫過類似代碼。
http://hi..com/逄韶華/ihome/myblog

8. 編譯原理 詞法分析 C 版 老師要求由編譯原理課後PL0完整源代碼改編

用LEX和YACC可以自動生成詞法分析和語法分析。
你要分析什麼語法,沒有明確講啊。

9. 編譯原理 語義分析 算術表達式求值代碼

java字元串算術表達式求值:importjava.util.ArrayList;importjava.util.Stack;/****@authoryhh**/publicclassCalculate{/***將字元串轉化成List*@paramstr*@return*/publicArrayListgetStringList(Stringstr){ArrayListresult=newArrayList();Stringnum="";for(inti=0;igetPostOrder(ArrayListinOrderList){ArrayListresult=newArrayList();Stackstack=newStack();for(inti=0;ipostOrder){Stackstack=newStack();for(inti=0;i

熱點內容
搶杠演算法 發布:2025-01-22 16:15:02 瀏覽:71
圖片伺服器ftp 發布:2025-01-22 15:52:33 瀏覽:506
sql打開bak文件 發布:2025-01-22 15:47:32 瀏覽:106
opengl伺服器源碼 發布:2025-01-22 15:40:02 瀏覽:908
python部署服務 發布:2025-01-22 15:38:46 瀏覽:282
壓縮機卡裝 發布:2025-01-22 15:37:04 瀏覽:446
每天跑步40分鍾可以緩解壓力嗎 發布:2025-01-22 15:33:24 瀏覽:448
線性表的鏈式存儲結構與順序存儲 發布:2025-01-22 15:32:45 瀏覽:295
解壓縮大師 發布:2025-01-22 15:26:51 瀏覽:386
xp訪問win7共享列印機無許可權 發布:2025-01-22 15:23:22 瀏覽:830