當前位置:九游会j9娱乐平台-九游ag登录中心网址 » 編程軟體 » 大學編譯原理課程設計

大學編譯原理課程設計-九游会j9娱乐平台

發布時間: 2024-01-14 22:42:19

1. 急(高懸賞 幫個忙) 求編譯原理課程設計---c語言實現c-的語法分析,在線等

新建一個文本文檔在你工程目錄下,名字起為"輸入.txt",裡面的內容可以為
begin a:=1 7*(6 3);b:=1end#

輸出是在"輸出.txt"中查看,以下為輸出情況:

詞法分析結果如下:
(1, begin)
(10, a)
(18, :=)
(11, 1)
(13, )
(11, 7)
(15, *)
(27, ()
(11, 6)
(13, )
(11, 3)
(28, ))
(26, ;)
(10, b)
(18, :=)
(11, 1)
(6, end)
(0, #)
語法分析結果如下:(以四元式形式輸出)
( , 6, 3, t1)

( *, 7, t1, t2)

( , 1, t2, t3)

( =, t3, __, a)

( =, 1, __, b)

//提供一個編譯原理的語義分析程序 你可以直接復制 用tc進行調試
#include "stdio.h"
#include "string.h"
#include
#include
#include "stdlib.h"

char prog[100],token[8],ch;
char *rwtab[6]={"begin","if","then","while","do","end"};
int syn,p,m,n,sum,q;
int kk;
//四元式表的結構如下:
struct
{
char result1[8];
char ag11[8];
char op1[8];
char ag21[8];
}quad[20];

char *factor();
char *expression();
int yucu();
char *term();
int statement();
int lrparser();
char *newtemp();
void scaner();
void emit(char *result,char *ag1,char *op,char *ag2);

void main()
{
file *fp1,*fp2;

if((fp1=fopen("輸入.txt","rt"))==null)
{
printf("cannot open 輸入.txt\n");
getch();
exit(1);
}
if((fp2=fopen("輸出.txt","wt "))==null)
{
printf("cannot create 輸出.txt file.strike any key exit");
getch();
exit(1);
}

int j;
q=p=kk=0;
p=0;
//printf("please input a string(end with '#'):\n");
while(ch!='#')
{
ch = fgetc(fp1);
if(ch == eof)
{
printf("文件為空,請檢查後再嘗試!");
return ;
}

prog[p ]=ch;
}
if(prog[p]=='#')
{
printf("輸入的待分析的串不是以'#'結尾,請修改之後再嘗試!\n");
return;
}
p=0;
char buffer1[200] = {0};
sprintf(buffer1,"詞法分析結果如下:\n");
fputs(buffer1,fp2);
//printf("詞法分析結果如下:\n");
do
{
scaner();
switch(syn)
{
case 11:
//printf("(%d,%d)\n",syn,sum);
sprintf(buffer1,"(%d, %d) \n",syn,sum);
fputs(buffer1,fp2);
break;
default:
//printf("(%d,%s)\n",syn,token);
sprintf(buffer1,"(%d, %s)\n",syn,token);
fputs(buffer1,fp2);
break;
}
}while(syn!=0);
printf("\n");

p=0;
char buffer[200]={0};
sprintf(buffer,"語法分析結果如下:(以四元式形式輸出)\n");
fputs(buffer,fp2);
//printf("語法分析結果如下:(以四元式形式輸出)\n");
scaner();//掃描函數
lrparser();
if(q>19)
printf(" to long sentense!\n");
else
{

for (j=0;j {
//printf("( %s, %s, %s, %s) \n\n",quad[j].op1,quad[j].ag11,quad[j].ag21,quad[j].result1);
sprintf(buffer,"( %s, %s, %s, %s) \n\n",quad[j].op1,quad[j].ag11,quad[j].ag21,quad[j].result1);
fputs(buffer,fp2);
}
}
printf("已把相應的詞法和語法的結果保存到相應的文件中,請查閱!\n");
fclose(fp1);
fclose(fp2);
}
int lrparser()
{
int schain=0;
kk=0;
if (syn==1) //得到begin
{
scaner();//掃描下個字元
schain=yucu();
if(syn==6)//得到end
{
scaner();//掃描下個字元
if((syn==0)&&(kk==0)) //得到#
printf("success!\n");
}
else
{
if(kk!=1)
printf("short of 'end' !\n");
kk=1;
getch();
exit(0);
}
}
else
{
printf("short of 'begin' !\n");
kk=1;
getch();
exit(0);
}
return (schain);
}
int yucu()
{
int schain=0;
schain=statement();
while(syn==26)
{
scaner();
schain=statement();
}
return (schain);
}
int statement()
{
char tt[8],eplace[8];
int schain=0;
if (syn==10)
{
strcpy(tt,token); //tt中保存的是第一個字元
scaner();
if(syn==18) //檢測到=號
{
scaner();
strcpy(eplace,expression());
emit(tt,eplace,"=","__");
schain=0;
}
else
{
printf("short of sign ':=' !\n");
kk=1;
getch();
exit(0);
}
return (schain);
}
}
char *expression()
{
char *tp,*ep2,*eplace,*tt;
tp=(char *)malloc(12);
ep2=(char *)malloc(12);
eplace=(char *)malloc(12);
tt=(char *)malloc(12);

strcpy(eplace,term());

while((syn==13)||(syn==14))
{
if (syn==13)
strcpy(tt," ");
else
strcpy(tt,"-");

scaner();
strcpy(ep2,term());
strcpy(tp,newtemp());
emit(tp,eplace,tt,ep2);
strcpy(eplace,tp);
}
return (eplace);
}
char *term()
{
char *tp,*ep2,*eplace,*tt;
tp=(char *)malloc(12);
ep2=(char *)malloc(12);
eplace=(char *)malloc(12);
tt=(char *)malloc(12);

strcpy(eplace,factor());

while((syn==15)||(syn==16))
{
if (syn==15)
strcpy(tt,"*");
else
strcpy(tt,"/");
scaner();
strcpy(ep2,factor());
strcpy(tp,newtemp());
emit(tp,eplace,tt,ep2);
strcpy(eplace,tp);
}
return (eplace);
}
char *factor()
{
char *fplace;
fplace=(char *)malloc(12);
strcpy(fplace,"");

if(syn==10) //得到字元
{
strcpy(fplace,token);
scaner();
}
else if(syn==11) //得到數字
{
itoa(sum,fplace,10);
scaner();
}
else if(syn==27) //得到)
{
scaner();
fplace=expression();
if(syn==28) //得到(
scaner();
else
{
printf("error on ')' !\n");
kk=1;
getch();
exit(0);
}
}
else
{
printf("error on '(' !\n");
kk=1;
getch();
exit(0);
}
return (fplace);
}
//該函數回送一個新的臨時變數名,臨時變數名產生的順序為t1,t2...
char *newtemp()
{
char *p;
char m[8];
p=(char *)malloc(8);

kk ;
itoa(kk,m,10);
strcpy(p 1,m);
p[0]='t';
return(p); //設置中間變數名放在一個字元數組中,字元數組的第一個字元為t第二個字元為m表示的數值
}
void scaner()
{
sum=0;
///for(m=0;m<8;m )
//token[m ]=null;
memset(token,0,8);
m=0;
ch=prog[p ];
while(ch==' ')
ch=prog[p ];
if(((ch<='z')&&(ch>='a'))||((ch<='z')&&(ch>='a')))
{
while(((ch<='z')&&(ch>='a'))||((ch<='z')&&(ch>='a'))||((ch>='0')&&(ch<='9')))
{
token[m ]=ch;
ch=prog[p ];
}
p--;
syn=10;
token[m ]='\0';
for(n=0;n<6;n )
if(strcmp(token,rwtab[n])==0)
{
syn=n 1;
break;
}
}
else if((ch>='0')&&(ch<='9'))
{
while((ch>='0')&&(ch<='9'))
{
sum=sum*10 ch-'0';
ch=prog[p ];
}
p--;
syn=11;
}
else switch(ch)
{
case '<':m=0;
ch=prog[p ];
if(ch=='>')
{
syn=21;
}
else if(ch=='=')
{
syn=22;
}
else
{
syn=20;
p--;
}
break;
case '>':m=0;
ch=prog[p ];
if(ch=='=')
{
syn=24;
}
else
{
syn=23;
p--;
}
break;
case ':':m=0;
token[m ] = ch;
ch=prog[p ];
if(ch=='=')
{
syn=18;
token[m ] = ch;
}
else
{
syn=17;
p--;
}
break;

case ' ': syn=13;token[0] = ch; break;
case '-': syn=14;token[0] = ch; break;
case '*': syn=15;token[0] = ch;break;
case '/': syn=16;token[0] = ch;break;
case '(': syn=27;token[0] = ch;break;
case ')': syn=28;token[0] = ch;break;
case '=': syn=25;token[0] = ch;break;
case ';': syn=26;token[0] = ch;break;
case '#': syn=0;token[0] = ch;break;
default: syn=-1;break;
}
}
//該函數是生成一個三地址語句送到四元式表中
void emit(char *result,char *ag1,char *op,char *ag2)
{
strcpy(quad[q].result1,result);
strcpy(quad[q].ag11,ag1);
strcpy(quad[q].op1,op);
strcpy(quad[q].ag21,ag2);
q ; //統計有多少個四元式
}

2. 編譯原理的課程設計,構造正規式r*(閉包運算)的nfa的程序實現。求源代碼,求文檔。 謝謝

個人感覺畫出nfa最直觀易懂了。前一個正規式僅有一個狀態(開始和接受狀態同),後一個雖然是三個狀態,但是其中一個是繞著a閉包的狀態,一個是繞著b閉包的狀態,而這兩個狀態又是繞著第三狀態(既是開始狀態又是接受狀態)進行閉包,所以實際上可合並為一種狀態,即是說這兩個正規式對應於同一個nfa,所以相等是顯然成立的。

3. 編譯原理課程設計的第5章錯誤處理

5.1.1錯誤的種類
5.1.2錯誤的診察和報告
5.1.3錯誤處理技術
5.1.4錯誤處理實現中的要點 5.2.1錯誤處理數據結構定義和相關函數
5.2.2詞法錯誤處理
5.2.3語法錯誤
5.2.4語義錯誤
5.2.5限制重復報告錯誤

4. 編譯原理課程設計

%{

/* filename: c.y */

%}
#define yydebug_lexer_text (yylval) /* our lexer loads this up each time */
#define yydebug 1 /* get the pretty debugging code to compile*/
#define yystype char * /* interface with flex: should be in header file */
/* define terminal tokens */
/* keywords */
%token auto double int struct
%token break else long switch
%token case enum register typedef
%token char extern return union
%token const float short unsigned
%token continue for signed void
%token default goto sizeof volatile
%token do if static while
/* ansi grammar suggestions */
%token identifier stringliteral
%token floatingconstant integerconstant characterconstant
%token octalconstant hexconstant
/* new lexical element, whereas ansi suggested non-terminal */
%token typedefname /* lexer will tell the difference between this and
an identifier! an identifier that is currently in scope as a
typedef name is provided to the parser as a typedefname.*/
/* multi-character operators */
%token arrow /* -> */
%token icr decr /* -- */
%token ls rs /* << >> */
%token le ge eq ne /* <= >= == != */
%token andand oror /* && || */
%token ellipsis /* ... */
/* modifying assignment operators */
%token multassign divassign modassign /* *= /= %= */
%token plusassign minusassign /* = -= */
%token lsassign rsassign /* <<= >>= */
%token andassign erassign orassign /* &= ^= |= */
%start translation_unit
%%
/* constants */
constant:
integerconstant
| floatingconstant
/* we are not including enumerationconstant here because we
are treating it like a variable with a type of "enumeration
constant". */
| octalconstant
| hexconstant
| characterconstant
;

string_literal_list:
stringliteral
| string_literal_list stringliteral
;
/************************* expressions ********************************/
primary_expression:
identifier /* we cannot use a typedef name as a variable */
| constant
| string_literal_list
| '(' comma_expression ')'
;
postfix_expression:
primary_expression
| postfix_expression '[' comma_expression ']'
| postfix_expression '(' ')'
| postfix_expression '(' argument_expression_list ')'
| postfix_expression {} '.' member_name
| postfix_expression {} arrow member_name
| postfix_expression icr
| postfix_expression decr
;
member_name:
identifier
| typedefname
;
argument_expression_list:
assignment_expression
| argument_expression_list ',' assignment_expression
;
unary_expression:
postfix_expression
| icr unary_expression
| decr unary_expression
| unary_operator cast_expression
| sizeof unary_expression
| sizeof '(' type_name ')'
;
unary_operator:
'&'
| '*'
| ' '
| '-'
| '~'
| '!'
;
cast_expression:
unary_expression
| '(' type_name ')' cast_expression
;
multiplicative_expression:
cast_expression
| multiplicative_expression '*' cast_expression
| multiplicative_expression '/' cast_expression
| multiplicative_expression '%' cast_expression
;
additive_expression:
multiplicative_expression
| additive_expression ' ' multiplicative_expression
| additive_expression '-' multiplicative_expression
;
shift_expression:
additive_expression
| shift_expression ls additive_expression
| shift_expression rs additive_expression
;
relational_expression:
shift_expression
| relational_expression '<' shift_expression
| relational_expression '>' shift_expression
| relational_expression le shift_expression
| relational_expression ge shift_expression
;
equality_expression:
relational_expression
| equality_expression eq relational_expression
| equality_expression ne relational_expression
;
and_expression:
equality_expression
| and_expression '&' equality_expression
;
exclusive_or_expression:
and_expression
| exclusive_or_expression '^' and_expression
;
inclusive_or_expression:
exclusive_or_expression
| inclusive_or_expression '|' exclusive_or_expression
;
logical_and_expression:
inclusive_or_expression
| logical_and_expression andand inclusive_or_expression
;
logical_or_expression:
logical_and_expression
| logical_or_expression oror logical_and_expression
;
conditional_expression:
logical_or_expression
| logical_or_expression '?' comma_expression ':'
conditional_expression
;
assignment_expression:
conditional_expression
| unary_expression assignment_operator assignment_expression
;
assignment_operator:
'='
| multassign
| divassign
| modassign
| plusassign
| minusassign
| lsassign
| rsassign
| andassign
| erassign
| orassign
;
comma_expression:
assignment_expression
| comma_expression ',' assignment_expression
;
constant_expression:
conditional_expression
;
/* the following was used for clarity */
comma_expression_opt:
/* nothing */
| comma_expression
;
/******************************* declarations *********************************/
/* the following is different from the ansi c specified grammar.
the changes were made to disambiguate typedef's presence in
declaration_specifiers (vs. in the declarator for redefinition);
to allow struct/union/enum tag declarations without declarators,
and to better reflect the parsing of declarations (declarators
must be combined with declaration_specifiers asap so that they
are visible in scope).
example of typedef use as either a declaration_specifier or a
declarator:
typedef int t;
struct s { t t;}; /* redefinition of t as member name * /
example of legal and illegal statements detected by this grammar:
int; /* syntax error: vacuous declaration * /
struct s; /* no error: tag is defined or elaborated * /
example of result of proper declaration binding:
int a=sizeof(a); /* note that "a" is declared with a type in
the name space before parsing the initializer * /
int b, c[sizeof(b)]; /* note that the first declarator "b" is
declared with a type before the second declarator is
parsed * /
*/
declaration:
sue_declaration_specifier ';'
| sue_type_specifier ';'
| declaring_list ';'
| default_declaring_list ';'
;
/* note that if a typedef were redeclared, then a declaration
specifier must be supplied */
default_declaring_list: /* can't redeclare typedef names */
declaration_qualifier_list identifier_declarator {} initializer_opt
| type_qualifier_list identifier_declarator {} initializer_opt
| default_declaring_list ',' identifier_declarator {} initializer_opt
;

declaring_list:
declaration_specifier declarator {} initializer_opt
| type_specifier declarator {} initializer_opt
| declaring_list ',' declarator {} initializer_opt
;

declaration_specifier:
basic_declaration_specifier /* arithmetic or void */
| sue_declaration_specifier /* struct/union/enum */
| typedef_declaration_specifier /* typedef*/
;

type_specifier:
basic_type_specifier /* arithmetic or void */
| sue_type_specifier /* struct/union/enum */
| typedef_type_specifier /* typedef */
;

declaration_qualifier_list: /* const/volatile, and storage class */
storage_class
| type_qualifier_list storage_class
| declaration_qualifier_list declaration_qualifier
;

type_qualifier_list:
type_qualifier
| type_qualifier_list type_qualifier
;

declaration_qualifier:
storage_class
| type_qualifier /* const or volatile */
;

type_qualifier:
const
| volatile
;

basic_declaration_specifier: /*storage class arithmetic or void*/
declaration_qualifier_list basic_type_name
| basic_type_specifier storage_class
| basic_declaration_specifier declaration_qualifier
| basic_declaration_specifier basic_type_name
;

basic_type_specifier:
basic_type_name /* arithmetic or void */
| type_qualifier_list basic_type_name
| basic_type_specifier type_qualifier
| basic_type_specifier basic_type_name
;

sue_declaration_specifier: /* storage class struct/union/enum */
declaration_qualifier_list elaborated_type_name
| sue_type_specifier storage_class
| sue_declaration_specifier declaration_qualifier
;

sue_type_specifier:
elaborated_type_name /* struct/union/enum */
| type_qualifier_list elaborated_type_name
| sue_type_specifier type_qualifier
;

typedef_declaration_specifier: /*storage class typedef types */
typedef_type_specifier storage_class
| declaration_qualifier_list typedefname
| typedef_declaration_specifier declaration_qualifier
;

typedef_type_specifier: /* typedef types */
typedefname
| type_qualifier_list typedefname
| typedef_type_specifier type_qualifier
;

storage_class:
typedef
| extern
| static
| auto
| register
;

basic_type_name:
int
| char
| short
| long
| float
| double
| signed
| unsigned
| void
;

elaborated_type_name:
aggregate_name
| enum_name
;

aggregate_name:
aggregate_key '{' member_declaration_list '}'
| aggregate_key identifier_or_typedef_name
'{' member_declaration_list '}'
| aggregate_key identifier_or_typedef_name
;

5. 編譯原理課程設計-詞法分析器設計(c語言)

#include"stdio.h"/*定義i/o庫所用的某些宏和變數*/

#include"string.h"/*定義字元串庫函數*/

#include"conio.h"/*提供有關屏幕窗口操作函數*/

#include"ctype.h"/*分類函數*/

charprog[80]={''},

token[8];/*存放構成單詞符號的字元串*/

charch;

intsyn,/*存放單詞字元的種別碼*/

n,

sum,/*存放整數型單詞*/

m,p;/*p是緩沖區prog的指針,m是token的指針*/

char*rwtab[6]={"begin","if","then","while","do","end"};

voidscaner(){

m=0;

sum=0;

for(n=0;n<8;n )

token[n]='';

ch=prog[p ];

while(ch=='')

ch=prog[p ];

if(isalpha(ch))/*ch為字母字元*/{

while(isalpha(ch)||isdigit(ch))/*ch為字母字元或者數字字元*/{

token[m ]=ch;

ch=prog[p ];}

token[m ]='';

ch=prog[p--];

syn=10;

for(n=0;n<6;n )

if(strcmp(token,rwtab[n])==0)/*字元串的比較*/{

syn=n 1;

break;}}

else

if(isdigit(ch))/*ch是數字字元*/{

while(isdigit(ch))/*ch是數字字元*/{

sum=sum*10 ch-'0';

ch=prog[p ];}

ch=prog[p--];

syn=11;}

else

switch(ch){

case'<':m=0;token[m ]=ch;ch=prog[p ];

if(ch=='>'){

syn=21;

token[m ]=ch;}

elseif(ch=='='){

syn=22;

token[m ]=ch;}

else{

syn=20;

ch=prog[p--];}

break;

case'>':m=0;token[m ]=ch;ch=prog[p ];

if(ch=='='){

syn=24;

token[m ]=ch;}

else{

syn=23;

ch=prog[p--];}

break;

case':':m=0;token[m ]=ch;ch=prog[p ];

if(ch=='='){

syn=18;

token[m ]=ch;}

else{

syn=17;

ch=prog[p--];}

break;

case' ':syn=13;token[0]=ch;break;

case'-':syn=14;token[0]=ch;break;

case'*':syn=15;token[0]=ch;break;

case'/':syn=16;token[0]=ch;break;

case'=':syn=25;token[0]=ch;break;

case';':syn=26;token[0]=ch;break;

case'(':syn=27;token[0]=ch;break;

case')':syn=28;token[0]=ch;break;

case'#':syn=0;token[0]=ch;break;

default:syn=-1;}}

main()

{

printf(" thesignificanceofthefigures: "

"1.figures1to6saidkeyword "

"2. "

"3.figures13to28saidoperators ");

p=0;

printf(" pleaseinputstring: ");

do{

ch=getchar();

prog[p ]=ch;

}while(ch!='#');

p=0;

do{

scaner();

switch(syn){

case11:printf("(%d,%d) ",syn,sum);break;

case-1:printf(" error; ");break;

default:printf("(%d,%s) ",syn,token);

}

}while(syn!=0);

getch();

}

程序測試結果

對源程序beginx:=9:ifx>9thenx:=2*x 1/3;end#的源文件,經過詞法分析後輸出如下圖5-1所示:

具體的你在修改修改吧

熱點內容
發布:2024-01-20 01:08:21 瀏覽:525
發布:2024-01-20 01:07:17 瀏覽:250
愛奇藝正義聯盟為啥不能緩存 發布:2024-01-20 00:52:13 瀏覽:248
caccess查詢資料庫 發布:2024-01-20 00:43:10 瀏覽:769
xp文件夾圖標更改 發布:2024-01-20 00:43:03 瀏覽:19
python和node 發布:2024-01-20 00:37:12 瀏覽:194
android拖拉 發布:2024-01-20 00:00:49 瀏覽:583
少兒編程課程體系介紹 發布:2024-01-20 00:00:48 瀏覽:846
我說你做下載ftp 發布:2024-01-20 00:00:47 瀏覽:8
安卓驅動培訓哪裡好 發布:2024-01-19 23:55:41 瀏覽:987
网站地图