當前位置:首頁 » 編程語言 » c語言簡單文法
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言簡單文法

發布時間: 2023-06-05 12:01:52

A. c語言什麼是語法啊

C語言是Combined
Language(組合語言)的中英混合簡稱。是一種計算機程序設計語言。它既具有高級語言的特點,又具有匯編語言的特點。它可以作為工作系統設計語言,編寫系統應用程序,也可以作為應用程序設計語言,編寫不依賴計算機硬體的應用程序。因此,它的應用范圍廣泛,不僅僅是在軟體開發上,而且各類科研都需要用到C語言,具體應用比如單片機以及嵌入式系統開發。

B. 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語言看不懂

對於第一個的一大堆的if條件語句,只能逐個的使用play_state的值去與後面的比較,當然要遵守&&運算符的短路規則,也就是對於a&&b,如果a的值為0,那麼就不用去判斷b是多少了,因為不管b是多少,對於&&運算符來說與0的運算其結果都為0。
至於你說的找不到play_state的值,play_state這個值,如果是在其他函數中或者if語句中定義的,那麼這個值可以不用看,因為這個值是局部變數,局部變數一但離開定義他的語句塊或者函數,其值將不會存在,而且在語句塊或函數外的程序是看不到局部變數的。因此對於play_state的值,你應該查找,是不是定義了一個全局變數,應找到全局變數的值才是有效的值。
至於邏輯預處理指令#if,其實這個語句就與C中的if語句是一樣的,如果#if後面的條件成立,則執行後面的語句,否則就跳出。並不是像你所說#ifdef語句,是否後面沒有定義就不用看了,應該判斷後面的條件是否成立。一般#if語句是與#define連合使用的,下面給你介紹一下#if語句。
邏輯預處理器指令:
1、邏輯#if指令:該指令原理與條件語句if相同,如果測試為真就執行後面的語句,如果為假則跳過後面的語句。該指令有兩種用法,其一可以用#if指令測試某個符號以前是否用#define指令定義過,這是最常用的用法,其二可以用來測試某個條件表達式是否為真。
2、#if指令用法一:測試某個符號是否以前用#define定義過,該用法的指令如下#if
defined
標識符
….
#endif
其縮寫形式為#ifdef
標識符….#endif表示如果,指定的標識符已被#define定義,則中間的語句就包含在源文件中,如果該標識符還未被#define定義,則跳過#if和#endif之間的語句,該語句以#endif結束,還要注意的是標識符前的關見字是defined比define多一個字母d。
3、測試標識符是否不存在:其語法為#if
!defined
標識符…..#endif縮寫形式為#ifnedf
標識符…..#endif表示如果指定的標識符沒有定義,則把#if和#endif之間的語句包含在源文件中,如果標識符已定義則跳過#if和#endif之間的代碼,實標上#ifndef語句比#ifdef語句使用得更頻繁,因為系統使用該語句防止頭文件被多次包含,
4、防止頭文件被包含多次的方:其方法為#ifndef
HY
#define
HY語句#endif程序在開始遇到標識符HY時沒有被定義,這時執行後面的語句,再第二次被使用時則標識符HY已經被定義,這時不會執行後面的語句,從而防止了同一頭文件被包含多次的情況。這里要注意使用#define後面定義的標識符不需要值。
5、#if語句還可以使用邏輯運算符以測試多個值,比如#if
defined
HY1&&HY2….#endif當HY1與HY2都為真時才執行,同樣還可以使用其他邏輯運算符。
6、#if指令用法二:測試某個表達式的值是否為真,其語法格式為:#if
常量表達式….#endif,注意常量表達式的求值結果應是整數常量表達式,比如#if
a=2
….
#endif測試a的值是否為2,如果為2則執行#if與#endif之間的語句。
4.7、多個#if選擇塊:和常規的if語句一樣#if也有對應的#else和#elif語句,比如#if
a=3
….
#else
….
#endif表示如果a=3則執行if後面且在#else前面的語句,如果為假則執行#else與#endif間的語句。#elif用來實現多個選擇,該語句和常規語句的else
if相似,比如#if
a=1
….
#elif
a=2
….
#elif
a=3….
#else
….
#endif表示,如果a=1則執行#if後的語句,如果a=2則執行該條件後的語句。
執行開始後,按順序先執行x=0;,因為它不在循環體內,所以只執行1次,
接下來是個循環嵌套,外層循環的for(i=1;i<n;i++)語句要執行n次,(因為要進行n次判斷),內層循環要執行n+(n-1)+(n-2)+...+1次,用等差數列求和公式表示這個和,就是n(n+1)/2次

D. c語言特點

1、簡潔緊湊、靈活方便。

C語言一共只有32個關鍵字,9種控制語句,程序書寫自由,主要用小寫字母表示。它把高級語言的基本結構和語句與低級語言的實用性結合起來。C語言可以象匯編語言一樣對位、位元組和地址進行操作,而這三者是計算機最基本的工作單元。

2、運算符豐富。

C的運算符包含的范圍很廣泛,共有種34個運算符。C語言把括弧、賦值、強制類型轉換等都作為運算符處理。從而使C的運算類型極其豐富表達式類型多樣化,靈活使用各種運算符可以實現在其它高級語言中難以實現的運算。

3、數據結構豐富。

C的數據類型有:整型、實型、字元型、數組類型、指陪含針類型、結構體類型、共用蘆前笑體類型等。能用來實現各種復雜的數據類型的運算。並引入了指針概念,使程序效率更高。另外C語言具有強大的圖形功能,支持多種顯示器和驅動器。且計算功能、邏輯判斷功能強大。

4、C是結構式語言。

結構式語言的顯著特點是代碼及數據的分隔化,即程序的各個部分除了必要的信息交流外彼此獨立。這種結構化方式可使程序層次清晰,便於使用、維護以及調試。C語言是以函數形式提供給用戶的,這些函數可方便的調用,並具有多種循環、條件語句控製程序流向,從而使程序完全結構化。

5、C語法限制不太嚴格、悔灶程序設計自由度大。

一般的高級語言語法檢查比較嚴,能夠檢查出幾乎所有的語法錯誤。而C語言允許程序編寫者有較大的自由度。

6、C語言允許直接訪問物理地址,可以直接對硬體進行操作。

因此既具有高級語言的功能,又具有低級語言的許多功能,能夠象匯編語言一樣對位、位元組和地址進行操作,而這三者是計算機最基本的工作單元,可以用來寫系統軟體。

7、C語言程序生成代碼質量高,程序執行效率高。

一般只比匯編程序生成的目標代碼效率低10へ20%。

c語言的內容和應用

C語言包括:數據類型、運算符與表達式、程序結構、順序、選擇、循環、數組、函數、預處理命令、指針、結構體與共用體、文件、位運算。

在編程領域中,C語言的運用非常之多,它兼顧了高級語言和匯編語言的優點,相較於其它編程語言具有較大優勢。計算機系統設計以及應用程序編寫是C語言應用的兩大領域。同時,C語言的普適較強,在許多計算機操作系統中都能夠得到適用,且效率顯著。


E. 求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

F. C語言的語法規則是什麼

(1)C源程序是由一個主函數和若干個其它函數組成的。
(2)函數名後必須有小括弧,函數體放在大括弧內。
(3)C程序必須用小寫字母書寫。
(4)每句的末尾加分號。
(5)可以一行多句。
(6)可以一句多行。
(7)可以在程序的任何位置加註釋。 一個完整的C語言程序,是由一個main()函數(又稱主函數)和若干個其它函數結合而成的,或僅由一個main()函數構成。

G. 怎麼樣用c語言編寫文法壓縮用加標記法實現

基本方法是
從識別符號向終結符過度時,從含有識別符號的規則入手,尋找所有規則中左部為識別符號的規則,然後搜索這些找到的規則的右部是否含有非終結符,若含有則從所有規則中找出左部為這個非終結符的規則,並標記,當搜完第一次後,循環查找具有標記的規則,按照上面的方式繼續標記剩下的規則,直到沒有新的規則被標記 退出 刪除未標記的規則體
反方向同理……
說什麼都不如看代碼 下面是我寫文法壓縮時候寫的壓縮函數
void Mylist::condense(char spot,CString gettofun)
{
//condense with spot way
addsignal(spot,1);
for(int i=1;i<7;i++)
{
for(int j=0;j<count;j++)
{
if(readsignal(j)==i)
{
int rightlength=findright(j).GetLength();
for(int k=0;k<rightlength;k++)
{
if(gettofun.Find(findright(j).GetAt(k))!=-1)
{
addsignal(findright(j).GetAt(k),i+1);
}
}
}
}
}
// unsigned the wordrule like s::=s*
bool caiding=false;
for(int j=0;j<count;j++)
{
int rightlength=findright(j).GetLength();
for(int k=0;k<rightlength;k++)
{
if(findleft(j)!=findright(j).GetAt(k))
caiding=true;
}
if(caiding==false)
{signal(j,0);}
}
//format every node have been signed
for(int m=0;m<count;m++)
{
if(readsignal(m)!=0)
{
signal(m,1);
}
}
//decide which to begin
for(m=0;m<count;m++)
{
if((gettofun.FindOneOf(findright(m))==-1)&&(readsignal(m)!=0))
{
signal(m,2);
}
}
//right way condense
int q=2;
for(int worry=0;worry<10;worry++){
for(int j1=0;j1<count;j1++)
{
if(readsignal(j1)==q)
{
for(int i1=0;i1<count;i1++){
//int right=findright(i1).GetLength();
for(int i2=0;i2<findright(i1).GetLength();i2++)
{
if(findleft(j1)==findright(i1).GetAt(i2))
{
signal(i1,q);
}
}

}
}
}//^
}
}
這里說明一下
其中的findright(int i) 函數的功能是從第i條文法規則中找到它的右部 返回值為CString類型
signal(int i,int j)函數 是在第i條規則中找到它的標志位(即加標記)並把j放入這個標志位 以示本條規則已經做好了標記
addsignal(char i,int j) 則是找到左部名為i的規則,並標記他的標志位為j addsignalright同理
本函數傳遞進來的有兩個參數 其中spot是識別符號Z,CString類型的是終結符號隊列,包含所有已知的終結符號
在說明一點 wordrule是一個文法體class的對象 它是一個鏈表 每一個結點(node)為一個規則:
class node
{
public:
char leftwords;
int signal;
CString rightwords;
node *next;
public:
node(char,CString);
virtual ~node();
};
希望對你有所幫助