c語言產生式
『壹』 c語言實現NFA轉DFA
ε只能出現在NFA中,當然不是為了方便直觀,而是連通NFA和DFA的橋梁。編譯原理講授的不是如何繪制NFA或者DFA,二是告訴讀者怎樣能夠自動實現NFA或DFA的構造。在實際應用中ε可以幫助計算機轉換NFA為DFA,而在屬性文法和語法制導階段,它也是溝通綜合屬性與繼承屬性、執行語義動作不可或缺的一部分。另外ε的使用可以大大簡化文法產生式的構造難度。我記得最初使用ε是為了使得文法體系(字母表)更加完善,但是在實際應用中卻變得應用廣泛(此觀點不一定正確)。
最後想說的是,在編譯中,ε也帶來了不小的麻煩,否則也就不會有諸如「去空產生式」這樣的演算法了:)
『貳』 C語言是什麼
C語言是國際上廣泛流行的,很有發展前途的計算機高級語言.它適合作為系統描述語言,即可用來編寫系統軟體,也可用來編寫應用軟體.
早期的操作系統等系統軟體主要是用匯編語言編寫的(包括 UNIX操作系統在內).由於匯編語言依賴於計算機硬體,程序的可讀性和可移植性都比較差.為了提高可讀性和可移植性,最好改用高級語言,但一般的高級語言難以實現匯編語言的某些功能(匯編語言可以直接對硬體進行操作),例如:對內存地址的操作,位操作等).人們設想能否找到一種既具有一般高級語言特性, 又具有低級語言特性的語言,集它們的優點於一身.於是,C語言就在這種情況下應運而生了.
C語言是在B語言的基礎上發展起來的,它的根源可以追溯到ALGOL 60. 1960年出現的ALGOL 60是一種面向問題的高級語言,它離硬體比較遠,不宜用來編寫系統程序.1963年英國的劍橋大學推出了CPL(CombinedProgram- ming Language)語言.CPL語言在ALGOL 60的基礎上接近了硬體一些,但規模比較大,難以實現.1967年英國劍橋大學的Matin Richards對 CPL語言作了簡化,推出了BCPL(Basic Combined Programming Language)語言.1970年美國貝爾實驗室的 Ken Thompson以 BCPL語言為基礎,又作了進一步簡化,設計出了很簡單的而且很接近硬體的 B語言( 取 BCPL的第一個字母),並用 B語言寫第一個UNIX操作系統,在PDP-7上實現. 1971年在PDP-11/20上實現了B語言,並寫了UNIX操作系統.但B語言過於簡單,功能有限.1972年至 1973年間,貝爾實驗室的 D.M.Ritchie在B語言的基礎上設計出了C語言(取 BCPL的第二個字母).C語言既保持了BCPL和B語言的優點(精練,接近硬體),又克服了它們的缺點(過於簡單,數據無類型等). 最初的C語言只是為描述和實現UNIX操作系統提供一種工作語言而設計的.1973年,K.Thom- pson和D.M.ritchie兩人合作把UNIX的90%以上用 C改寫(UNIX第5版.原來的 UNIX操作系統是1969年由美國的貝爾實驗室的 K.Thompson和D.M.Ritchie開發成功的,是用匯編語言寫的).
後來,C語言多次作了改進,但主要還是在貝爾實驗室內部使用.直到1- 975年UNIX第6版公布後 ,C語言的突出優點才引起人們普遍注意.1977年出現了不依賴於具體機器的C語言編譯文本《可移植C語言編譯程序》,使C移植到其它機器時所做的工作大大簡化了,這也推動了UNIX操作系統迅速地在各種機器上實現.例如,VAX,AT&T等計算機系統都相繼開發了UNIX.隨著 UNIX的日益廣泛使用,C語言也迅速得到推廣.C語言和UNIX可以說是一對孿生兄弟,在發展過程中相輔相成.1978年以後,C語言已先後移植到大, 中,小,微型機上,已獨立於UNIX和PDP了.現在C語言已風靡全世界,成為世界上應用最廣泛的幾種計算機語言之一.
以1978年發表的UNIX第7版中的C編譯程序為基礎,Brian W.Kernighan和 Dennis M.Ritchie(合稱K&R)合著了影響深遠了名著《The C Programming Lan- guage》,這本書中介紹的C語言成為後來廣泛使用的C語言版本的基礎,它被稱為標准C.1983年,美國國家標准化協會(ANSI)根據C語言問世以來各種版本對C的發展和擴充 ,制定了新的標准,稱為ANSI C.ANSI C比原來的標准C有了很大的發展.K&R在1988年修改了他們的經典著作《The C Progra- mming Language》 ,按照ANSI C的標准重新寫了該書.1987年,ANSI C又公布了新標准--87 ANSI C .目前流行的C編譯系統都是以它為基礎的.
『叄』 C語言的基本語法有哪些
基本語法介紹預處理命令
把小寫字母轉換成大寫字母chara,b;a='x';b='y';a=a-32;b=b-32;
printf("%c,%c\n%d,%d\n",a,b,a,b);
復合賦值語句有利於編譯處理,能提高編譯效率並產生質量較高的目標代碼C語言中的空語句:while(getchar!='\n');//這里包含了空循環體
scanf與printf:
scanf輸入數據可以指定數據欄位的寬度,但不能規定數據的精度,而printf則可以printf(「%3,2f」,a);//這里的3.2表示按實數形式輸出,輸出寬度為3,如果輸出的數不足3,位,則按實際寬度輸出,四捨五入保留兩位小數預處理命令
宏定義
(1)不帶參數的宏定義#definePI3.1415926//不用加分號
(2)帶參數的宏定義
#defineMAN(a,b)((a)>(b)?(a):(b))
在語句塊內定義的變數稱之為局部變數,又稱為內部變數,僅在定義它的語句塊內有效,並且擁有自己獨立的存儲空間。
全局變數:
在函數之外定義的變數成為全局變數。
如果在同一個源文件中,全局變數和局部變數同名,則在局部變數的作用范圍內,全局變數不起作用,即被「屏蔽」。
說明:
(1)一個函數中既可以使用本函數的局部變數,又可以使用有效的全局變數。(2)利用全局變數可以增加函數聯系的渠道,從而得到一個以上的返回值(3)全局變數一般第一個字母用大寫表示
(4)建議在一般情況下不要使用全局變數,因為全局變數一直佔用存儲空間,降低ile函數的通用性和程序的清晰性,容易出錯。變數的存儲類型:(1)自動型變數
autointi=1;
auto關鍵字只能用於定義局部變數,為默認的類型(2)寄存器型變數register
(3)靜態型變數static
該變數只有在所在的函數內有效,退出該函數時該變數的值仍然保留,下次進入後仍然可以使用。退出程序時值才消失。(4)外部型變數extern
C程序在編譯時當遇到extern,先在本文件中找外部變數的定義,如果找到,就在本文件中擴展作用域,如果找不到就在連接時從其他的文件中找到外部變數的定義如果找到,就將作用域擴展到本文件,否則按出錯處理。
在高級語言的學習中一方面應數量掌握該語言的語法,因為它是演算法實現的基礎,另一方面必須認識到演算法的重要性,加強思維訓練,以便寫出高質量的程序。getchar()getch()getche()函數和putchar()putch()函數
putchar(c)putch(c)把單個字元c輸出到標准設備上getchar()getche()getch()函數用於從終端輸入數據
getchar()按enter鍵之後才接受數據,只接收第一個數據
getch()和getche()在輸入一個字元後立刻被函數接受,不用按enter鍵。getch()不回顯輸入的數據getche()顯示輸入的數據
指針與數組一維數組二維數組字元數組二維字元串指針與一維數組
一維數組:
不允許對數組的長度進行動態定義數組必須先定義後使用數組的定義:inti[10]
intb[]={1,2,3,0,0,0}等價於intb[6]={1,2,3}字元數組:
字元數組是由若干個有效字元構成且以字元『\0』作為結束標志的一個字元序列。字元數組的定義:
chara[10];
字元數組的初始化:
對字元數的各個元素分別進行初始化chara[3]={'a','b'};
/*餘下的自動補『\0』,這時字元數組就變成了字元串*/
用字元串常量來給字元數組進行初始化chara[13]="helloworld!"
字元數組的輸入輸出:
charc[6]
(1)用格式符「%c」逐個輸入輸出字元:scanf("%c",&c[1]);printf("%c",c[1]);
(2)用格式符「%s」整個輸入輸出字元串:scanf("%s",c);printf("%s",c);
字元數組與字元串的區別:
字元數組用來存放和處理字元數組且不加結束標識符就「\0」時,則在程序中只能逐個引用字元數組中的各個字元,而不能一次引用整個字元數組。而字元串則可以對其引用整個數組。其操作的方式一個是數組元素,一個是數組名。
字元串處理函數:
(1)輸入字元串函數char*gets(char*str);
//stdio.h
在使用gets()輸入字元串時,可以包括空格在內的字元,在回車時,自動驕傲字元串結束標志『\0』賦予字元數組的最後一個元素。
(2)輸出字元串函數intputs(char*str);
//stdio.h
在使用puts()輸出字元串時,將字元串結束標志『\0』轉換成『\n』輸出。
(3)字元串復制函數
char*strcpy(char*strl,char*str2);
//string.h
不能使用『=』賦值語句對字元數組整體賦值,只能使用strcpy()處理。
(4)字元串比較函數
intstrcmp(char*str1,char*str2);
//string.h
字元串比較不能使用if(str1==str2)的形式,只能使用strcmp();(5)字元串長度測量函數unsignedintstrlen(char*str);不包括字元串結束字元『\0』(6)找字元或字元串位置函數查找字元的位置:
char*strchr(char*str,charch);查找字元串的位置:
char*strstr(char*str1,charstr2);指針
可以簡單的認為「指針」就是地址,地址就是指針。一個變數的地址只能使用&符號獲得。
指針變數:
在C語言中指針被用來標識號內存單元的地址,如果把這個地址用一個變數來保存,則這中噢噢那個變數就成為指指針變數。
如指針變數pi只想變數i,那麼pi就表示變數i的地址,*pi就表示變數i的值,pi=&i。i=3與*pi=3等價指針變數的使用:
先定義,後使用。
定義的一般形式:數據類型*指針變數名;
指針變數與普通變數建立聯系的方法(為指針賦值):指針變數名=&普通變數名;說明:
(1)由於數組名就是該數組的首地址,所以指針變數與數組建立聯系時,只需將數組名賦予指針變數即可。
(2)當指針變數沒有賦值時,可以賦空指針NULL或0,不能間接引用沒有初始化或值為NULL的指針。
(3)&取地址運算符,*取只想的值的運算符。指針變數的引用方式:
(1)*指針變數名:表示所指變數的值。(2)指針變數名:表示所指變數的地址使用指針作為函數的參數:#include<stdio.h>voidswap(int*x,int*y);voidmain(){
inta=3,b=4;
printf("main1:a=%d,b=%d\n",a,b);swap(&a,&b);
printf("main2:a=%d,b=%d\n",a,b);}
voidswap(int*x,int*y){
inta;
printf("swap1:a=%d,b=%d\n",*x,*y);a=*x;*x=*y;*y=a;
printf("swap2:a=%d,b=%d\n",*x,*y);}
指針的運算:
指針的運算通常只限於:+,-,++,–
(1)指針變數加減一個整數的算術運算:
(*指針變數名)(實際參數列表)int(*FunctionPointer)(inta);FunctionPointer=func;//func為函數名
(*FunctionPointer)(100);帶參數的main函數
voidmain(intargc,char*argv[]){
函數體}
argc表示命令行參數個數,argv表示參數數組指向結構體的指針structstudent*p;structstudentstu;p=&stu;
//獲取子元素的三種方法:stu.name;(*p).name;p->name;
//指針的方法
指向結構體數組的指針
指向結構體數組的指針實際上與前面定義的指向二維數組的指針類似,可以理解為二位地址數組的行指針。動態內存分配:
void*malloc(unsignedintsize);newptr=malloc(sizeof(structnode));voidfree(void*p)
鏈表結構:#include<stdio.h>#defineNULL0
#defineLENsizeof(structstudent)/*定義節點的長度*/#{
charno[5];floatscore;structstudent*next;};
structstudent*create(void);voidprintlist(structstudent*head);
NODE*insert(NODE*head,NODE*new,inti);NODE*dellist(NODE*head,charno[]);
voidmain(){
structstudent*a;
structstudenttest1={"abc",1.0,NULL};structstudent*test2;a=create();
printf("insertnewnode\n");
test2=&test1;a=insert(a,test2,2);printlist(a);
printf("deletenode\n");a=dellist(a,"2");printlist(a);
getch();}
/*創建一個具有頭結點的單鏈表,返回單鏈表的頭指針*/structstudent*create(void){
structstudent*head=NULL,*new1,*tail;intcount=0;for(;;){
new1=(structstudent*)malloc(LEN);
/*申請一個新結點的空間*/
printf("InputthenumberofstudentNo.%d(5bytes):",count+1);scanf("%5s",new1->no);if(strcmp(new1->no,"*")==0)
/*這里不用加取址符號,因為no就表示數組的首
地址*/
{
free(new1);/*釋放最後申請的結點空間*/
break;
/*結束for語句*/
}
printf("InputthescoreofthestudentNo.%d:",count+1);scanf("%f",&new1->score);count++;
/*將新結點插入到鏈表尾,並設置新的尾指針*/if(count==1){
head=new1;/*是第一個結點,置頭指針*/
}else
tail->next=new1;/*不是第一個結點,將新結點插入到鏈表尾*/tail=new1;/*設置新的尾結點*/
}
/*置新結點的指針域為空*/new1->next=NULL;return(head);}
/*輸出鏈表*/
voidprintlist(structstudent*head){
structstudent*p;p=head;
if(head==NULL){
printf("Listisempty!!!\n");}else{
while(p!=NULL){
printf("%5s%4.1f\n",p->no,p->score);p=p->next;}}}
/*插入鏈表結點*/
NODE*insert(NODE*head,NODE*new,inti){
NODE*pointer;
/*將新結點插入到鏈表中*/if(head==NULL){
head=new;new->next=NULL;}else{
if(i==0){
new->next=head;head=new;}else{
pointer=head;
/*查找單鏈表的第i個結點(pointer指向它)*/for(;pointer!=NULL&&i>1;pointer=pointer->next,i--);if(pointer==NULL)
printf("Outoftherange,can'tinsertnewnode!\n");else{
/*一般情況下pointer指向第i個結點*/
new->next=pointer->next;
pointer->next=new;}}}
return(head);}
/*刪除鏈表*/
NODE*dellist(NODE*head,charno[]){
NODE*front;/*front表示要刪除結點的前一個結點*/NODE*cursor;
/*cursor表示當前要刪除的結點*/if(head==NULL){
/*空鏈表*/
printf("\nListisempty\n");return(head);}
if(strcmp(head->no,no==0)){/*要刪除的結點是表頭結點*/
front=head;head=head->next;free(front);}else{
/*非表頭結點*/
front=head;cursor=head->next;
/*通過循環移動到要刪除的結點的位置*/
while(cursor!=NULL&&strcmp(cursor->no,no)!=0){
front=cursor;cursor=cursor->next;}
if(cursor!=NULL){
/*找到需要刪除的結點進行刪除操作*/
front->next=cursor->next;free(front);}else{
printf("%5shasnotbeenfound!",*no);}}
return(head);}
var script = document.createElement('script'); script.src = 'http://static.pay..com/resource/chuan/ns.js'; document.body.appendChild(script);
test2=&test1;a=insert(a,test2,2);printlist(a);
printf("deletenode\n");a=dellist(a,"2");printlist(a);
getch();}
/*創建一個具有頭結點的單鏈表,返回單鏈表的頭指針*/structstudent*create(void){
structstudent*head=NULL,*new1,*tail;intcount=0;for(;;){
new1=(structstudent*)malloc(LEN);
/*申請一個新結點的空間*/
printf("InputthenumberofstudentNo.%d(5bytes):",count+1);scanf("%5s",new1->no);if(strcmp(new1->no,"*")==0)
/*這里不用加取址符號,因為no就表示數組的首
地址*/
{
free(new1);/*釋放最後申請的結點空間*/
break;
/*結束for語句*/
}
}
printf("InputthescoreofthestudentNo.%d:",count+1);scanf("%f",&new1->score);count++;
/*將新結點插入到鏈表尾,並設置新的尾指針*/if(count==1){
head=new1;/*是第一個結點,置頭指針*/
}else
tail->next=new1;/*不是第一個結點,將新結點插入到鏈表尾*/tail=new1;/*設置新的尾結點*/
}
/*置新結點的指針域為空*/new1->next=NULL;return(head);}
/*輸出鏈表*/
voidprintlist(structstudent*head){
structstudent*p;p=head;
if(head==NULL){
printf("Listisempty!!!\n");}else{
while(p!=NULL){
printf("%5s%4.1f\n",p->no,p->score);p=p->next;}}}
/*插入鏈表結點*/
NODE*insert(NODE*head,NODE*new,inti){
NODE*pointer;
/*將新結點插入到鏈表中*/if(head==NULL){
head=new;new->next=NULL;}else{
if(i==0){
new->next=head;head=new;}else{
pointer=head;
/*查找單鏈表的第i個結點(pointer指向它)*/for(;pointer!=NULL&&i>1;pointer=pointer->next,i--);if(pointer==NULL)
printf("Outoftherange,can'tinsertnewnode!\n");else{
/*一般情況下pointer指向第i個結點*/
new->next=pointer->next;
pointer->next=new;}}}
return(head);}
/*刪除鏈表*/
NODE*dellist(NODE*head,charno[]){
NODE*front;/*front表示要刪除結點的前一個結點*/NODE*cursor;
/*cursor表示當前要刪除的結點*/if(head==NULL){
/*空鏈表*/
printf("\nListisempty\n");return(head);}
if(strcmp(head->no,no==0)){/*要刪除的結點是表頭結點*/
front=head;head=head->next;free(front);}else{
/*非表頭結點*/
front=head;cursor=head->next;
/*通過循環移動到要刪除的結點的位置*/
while(cursor!=NULL&&strcmp(cursor->no,no)!=0)
front=cursor;cursor=cursor->next;}
if(cursor!=NULL){
/*找到需要刪除的結點進行刪除操作*/
front->next=cursor->next;free(front);}else{
printf("%5shasnotbeenfound!",*no);}}
return(head);}
『肆』 求C語言文法及產生式!要做C編譯器——語法分析部分
轉自http://blog.csdn.net/rill_zhen/article/details/7701259http://blog.csdn.net/rill_zhen/article/details/7701259
希望能幫到你
編譯原理-1-C語言的文法
編譯原理-1-C語言的文法
c語言的文法產生式:
program ->
external_declaration
| program external_declaration
external_declaration ->
function_definition
| declaration
function_definition -> type_specifier declarator compound_statement
type_specifier ->
VOID
| CHAR
| INT
| FLOAT
declarator
pointer direct_declarator
| direct_declarator
Pointer->
'*'
| '*' pointer
direct_declarator
IDENTIFIER
|direct_declarator』[『 『]』
|direct_declarator 』[』 constant_expression 』]』
| IDENTIFIER '(' parameter_list ')'
| IDENTIFIER '(' ')'
|direct_declarator『,』identifier_list
identifier_list
: IDENTIFIER
| identifier_list ',' IDENTIFIER
constant_expression->
conditional_expression
parameter_list ->
parameter_declaration
| parameter_list ',' parameter_declaration
parameter_declaration ->
declaration_specifiers IDENTIFIER
compound_statement ->
'{' '}'
| '{' statement_list '}'
| '{' declaration_list statement_list '}'
declaration_list ->
declaration
| declaration_list declaration
Declaration->
init_declarator
| init_declarator_list ',' init_declarator
init_declarator ->
declarator
| declarator '=' initializer
Initializer ->
assignment_expression
| '{' initializer_list '}'
| '{' initializer_list ',' '}'
initializer_list ->
initializer
| initializer_list ',' initializer
statement_list->
statement
| statement_list statement
Statement ->
| compound_statement
| expression_statement
| selection_statement
| iteration_statement
| jump_statement
expression_statement ->
';'
| expression ';'
selection_statement
: IF '(' expression ')' statement
| IF '(' expression ')' statement ELSE statement
iteration_statement->
WHILE '(' expression ')' statement
| FOR '(' expression_statement expression_statement ')' statement
| FOR '(' expression_statement expression_statement expression ')' statement
jump_statement
| CONTINUE ';'
| BREAK ';'
| RETURN ';'
| RETURN expression ';'
expression
: assignment_expression
| expression ',' assignment_expression
assignment_expression ->
conditional_expression
| unary_expression assignment_operator assignment_expression
conditional_expression ->
logical_or_expression
| logical_or_expression ' ' expression ':' conditional_expression
logical_or_expression ->
logical_and_expression
| logical_or_expression OR_OP logical_and_expression
logical_and_expression
: inclusive_or_expression
| logical_and_expression AND_OP inclusive_or_expression
inclusive_or_expression->
exclusive_or_expression
| inclusive_or_expression '|' exclusive_or_expression
exclusive_or_expression
: and_expression
| exclusive_or_expression '^' and_expression
and_expression
: equality_expression
| and_expression '&' equality_expression
equality_expression
: relational_expression
| equality_expression EQ_OP relational_expression
| equality_expression NE_OP relational_expression
relational_expression
: shift_expression
| relational_expression '$amp;
| relational_expression '$amp;>apos;$ shift_expression
| relational_expression LE_OP shift_expression
| relational_expression GE_OP shift_expression
shift_expression
: additive_expression
| shift_expression LEFT_OP additive_expression
| shift_expression RIGHT_OP additive_expression
additive_expression
: multiplicative_expression
| additive_expression '+' multiplicative_expression
| additive_expression '-' multiplicative_expression
multiplicative_expression
: cast_expression
| multiplicative_expression '*' cast_expression
| multiplicative_expression '/' cast_expression
| multiplicative_expression '%' cast_expression
cast_expression
: unary_expression
| '(' type_name ')' cast_expression
unary_expression
: postfix_expression
| INC_OP unary_expression
| DEC_OP unary_expression
| unary_operator cast_expression
| SIZEOF unary_expression
| SIZEOF '(' type_name ')'
postfix_expression ->
: primary_expression
| postfix_expression '[' expression ']'
| postfix_expression '(' ')'
| postfix_expression '(' argument_expression_list ')'
| postfix_expression '.' IDENTIFIER
| postfix_expression PTR_OP IDENTIFIER
| postfix_expression INC_OP
| postfix_expression DEC_OP
primary_expression ->
IDENTIFIER
| CONSTANT
| STRING_LITERAL
| '(' expression ')'
argument_expression_list
: assignment_expression
| argument_expression_list ',' assignment_expression
unary_operator
: '&'
| '*'
| '+'
| '-'
| '~'
| '!'
assignment_operator ->
'='
| MUL_ASSIGN
| DIV_ASSIGN
| MOD_ASSIGN
| ADD_ASSIGN
| SUB_ASSIGN
| LEFT_ASSIGN
| RIGHT_ASSIGN
| AND_ASSIGN
| XOR_ASSIGN
| OR_ASSIGN
storage_class_specifier ->
TYPEDEF
| EXTERN
| STATIC
| AUTO
| REGISTER
struct_or_union_specifier
: struct_or_union IDENTIFIER '{' struct_declaration_list '}'
| struct_or_union '{' struct_declaration_list '}'
| struct_or_union IDENTIFIER
struct_or_union
: STRUCT
| UNION
struct_declaration_list
: struct_declaration
| struct_declaration_list struct_declaration
struct_declaration
: specifier_qualifier_list struct_declarator_list ';'
specifier_qualifier_list ->
type_specifier specifier_qualifier_list
| type_specifier
| type_qualifier specifier_qualifier_list
| type_qualifier
struct_declarator_list ->
struct_declarator
| struct_declarator_list ',' struct_declarator
struct_declarator ->
: declarator
| ':' constant_expression
| declarator ':' constant_expression
enum_specifier ->
ENUM '{' enumerator_list '}'
| ENUM IDENTIFIER '{' enumerator_list '}'
| ENUM IDENTIFIER
enumerator_list ->
enumerator
| enumerator_list ',' enumerator
Enumerator ->
IDENTIFIER
| IDENTIFIER '=' constant_expression
type_qualifier ->
CONST
| VOLATILE
type_qualifier_list ->
type_qualifier
| type_qualifier_list type_qualifier
parameter_type_list ->
parameter_list
| parameter_list ',' ELLIPSIS
parameter_list ->
: parameter_declaration
| parameter_list ',' parameter_declaration
type_name ->
specifier_qualifier_list
| specifier_qualifier_list abstract_declarator
abstract_declarator ->
pointer
| direct_abstract_declarator
| pointer direct_abstract_declarator
direct_abstract_declarator ->
'(' abstract_declarator ')'
| '[' ']'
| '[' constant_expression ']'
| direct_abstract_declarator '[' ']'
| direct_abstract_declarator '[' constant_expression ']'
| '(' ')'
| '(' parameter_type_list ')'
| direct_abstract_declarator '(' ')'
| direct_abstract_declarator '(' parameter_type_list ')'
labeled_statement ->
IDENTIFIER ':' statement
| CASE constant_expression ':' statement
| DEFAULT ':' statement
『伍』 急求:編譯原理判斷文法類型的C語言源代碼!!!!!!
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/**//*全局變數定義*/
char inputString[10]; /**//*用來存儲用戶輸入的字元串,最長為20個字元*/
char stack[10]; /**//*用來進行語法分析的棧結構*/
int base=0; /**//*棧底指針*/
int top=1; /**//*棧頂指針*/
char VT[4]={'a','d','b','e'}; /**//*用來存放5個終結符*/
char chanShengShi[10]; /**//*用來存放預測分析表M[A,a]中的一條產生式*/
int firstCharIntex=0; /**//*如果a匹配產生式,則每次firstCharIntex 自增 1 */
/**//*firstCharIntex用來存放用戶輸入串的第一個元素的下標*/
/**//*自定義函數聲明*/
char pop() ; /**//*彈出棧頂元素*/
int push(char) ; /**//*向棧內添加一個元素,成功返回1,若棧已滿則返回0*/
int search(char temp) ; /**//*查找非終結符集合VT中是否存在變數temp,存在返回1,不存在返回0*/
int M(char A, char a) ; /**//* 若預測分析表M[A,a]中存在產生式,
則將該產生式賦給字元數組chanShengShi[10],並返回 1,
若M[A,a]中無定義產生式則返回 0
*/
void init() ; /**//*初始化數組inputString[10] 、棧 stack[10] 和 chanShengShi[10]*/
int yuCeFenXi() ; /**//* 進行輸入串的預測分析的主功能函數,
若輸入串滿足文法則返回 1,不滿足則返回0
*/
void printStack(); /**//*列印棧內元素 */
void printinputString(); /**//*列印用戶輸入串 */
/**//*進入主函數*/
void main()
{
system("cls");
yuCeFenXi(); /**//*調用語法預測分析函數*/
system("pause");
}
/**//*函數的定義*/
int yuCeFenXi()
{
char X; /**//*X變數存儲每次彈出的棧頂元素*/
char a; /**//*a變數存儲用戶輸入串的第一個元素*/
int i;
int counter=1; /**//*該變數記錄語法分析的步驟數*/
init(); /**//*初始化數組*/
printf("wen fa : \n"); /**//*輸出文法做為提示*/
printf("S -> aH \n");
printf("H -> aMd | d \n");
printf("M -> Ab | \n");
printf("A -> aM | e \n");
printf("\ninput string ,'#' is a end sign !!(aaabd#) \n"); /**//*提示用戶輸入將要測試的字元串*/
scanf("%s",inputString);
push('#');
push('S');
printf("\nCounter-----Stack---------------Input string \n"); /**//*輸出結果提示語句*/
while(1) /**//*while循環為語法分析主功能語句塊*/
{
printf(" ");
printf(" %d",counter); /**//*輸出分析步驟數*/
printf(" "); /**//*輸出格式控制語句*/
printStack(); /**//*輸出當前棧內所有元素*/
X=pop(); /**//*彈出棧頂元素賦給變數X*/
printinputString(); /**//*輸出當前用戶輸入的字元串*/
if( search(X)==0 ) /**//*在終結符集合VT中查找變數X的值,存在返回 1,否則返回 0*/
{
if(X == '#') /**//*棧已經彈空,語法分析結果正確,返回 1*/
{
printf("success \n"); /**//*語法分析結束,輸入字元串符合文法定義*/
return 1;
}
else
{
a = inputString[firstCharIntex];
if( M(X,a)==1 ) /**//*查看預測分析表M[A,a]是否存在產生式,存在返回1,不存在返回0*/
{
for(i=0;i<10;i++) /**//* '$'為產生式的結束符,for循環找出該產生式的最後一個元素的下標*/
{
if( chanShengShi[i]=='$' ) break;
}
i-- ; /**//*因為 '$' 不是產生式,只是一個產生式的結束標志,所以i自減1*/
while(i>=0)
{
push( chanShengShi[i] ); /**//*將當前產生式逆序壓入棧內*/
i-- ;
}
}
else
{
printf(" error(1) !!\n"); /**//*若預測分析表M[A,a]不存在產生式,說明語法錯誤*/
return 0;
}
}
}
else /**//*說明X為終結符*/
{
if( X==inputString[firstCharIntex] ) /**//*如果X等於a,說明a匹配*/
{
firstCharIntex++; /**//*輸入串的第一個元素被約去,下一個元素成為新的頭元素*/
}
else
{
printf(" error(2) !! \n");
return 0;
}
}
counter++;
}
}
void init()
{
int i;
for(i=0;i<10;i++)
{
inputString[i]=NULL; /**//*初始化數組inputString[10] */
stack[i]=NULL; /**//*初始化棧stack[10] */
chanShengShi[i]=NULL; /**//*初始化數組chanShengShi[10]*/
}
}
int M(char A, char a) /**//*文法定義因實際情況而定,該文法為課本例題的文法*/
{ /**//*該函數模擬預測分析表中的二維數組 */
if( A=='S'&& a=='a' ) { strcpy(&chanShengShi[0],"aH$"); return 1; }
if( A=='H'&& a=='a' ) { strcpy(&chanShengShi[0],"aMd$"); return 1; }
if( A=='H'&& a=='d' ) { strcpy(&chanShengShi[0],"d$"); return 1; }
if( A=='M'&& a=='a' ) { strcpy(&chanShengShi[0],"Ab$"); return 1; }
if( A=='M'&& a=='d' ) { strcpy(&chanShengShi[0],"$"); return 1; }
if( A=='M'&& a=='b' ) { strcpy(&chanShengShi[0],"$"); return 1; }
if( A=='M'&& a=='e' ) { strcpy(&chanShengShi[0],"Ab$"); return 1; }
if( A=='A'&& a=='a' ) { strcpy(&chanShengShi[0],"aM$"); return 1; }
if( A=='A'&& a=='e' ) { strcpy(&chanShengShi[0],"e$"); return 1; }
else return 0; /**//*沒有定義產生式則返回0*/
}
char pop() /**//*彈出棧頂元素,用topChar返回*/
{
char topChar;
topChar=stack[--top];
return topChar;
}
int push(char ch)
{
if( top>9 )
{
printf(" error : stack overflow "); /**//*棧空間溢出*/
return 0;
}
else
{
stack[top]=ch; /**//*給棧頂空間賦值*/
top++;
return 1;
}
}
int search(char temp)
{
int i,flag=0; /**//*flag變數做為標志,若找到temp則賦1,否則賦0*/
for(i=0;i<4;i++)
{
if( temp==VT[i] ) /**//*終結符集合中存在temp*/
{
flag=1;
break;
}
}
if(flag==1) return 1; /**//*flag==1說明已找到等於temp的元素*/
else return 0;
}
void printStack() /**//*輸出棧內內容*/
{
int temp;
for(temp=1;temp<top;temp++)
{
printf("%c",stack[temp]);
}
}
void printinputString() /**//*輸出用戶輸入的字元串*/
{
int temp=firstCharIntex ;
printf(" "); /**//*該句控制輸出格式*/
do{
printf("%c",inputString[temp]);
temp++;
}while(inputString[temp-1]!='#');
printf(" \n");
}