当前位置:首页 » 编程软件 » 编译原理预测分析法代码

编译原理预测分析法代码

发布时间: 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 12:55:25 浏览:221
可编程脉冲电源 发布:2025-01-22 12:49:22 浏览:830
欧规墨规美规中东哪个配置高 发布:2025-01-22 12:48:00 浏览:778
安卓机怎么用不了多久 发布:2025-01-22 12:47:44 浏览:762
安卓怎么录屏别人直播 发布:2025-01-22 12:35:20 浏览:386
1030怎么配置电脑 发布:2025-01-22 12:35:19 浏览:90
sql数据库的端口 发布:2025-01-22 12:20:02 浏览:363
安卓最终幻想8怎么设置中文 发布:2025-01-22 12:19:23 浏览:652
怎么查电脑配置和网络 发布:2025-01-22 12:19:16 浏览:587
linuxsnmp查看 发布:2025-01-22 12:17:49 浏览:38