[kic]kic語言定義,kic是C語言的子集

前言

本來想看《自制編程語言,基於C語言》,但是沒有拿到手的電子書只有目錄和兩個章節,所以只能看《自己動手製作編譯器、鏈接器》。這本書聽說有些bug,其實無所謂了,一般的書上都找的出問題,就算是“大國重器”的教材書中也有打印錯誤。這本書中作者實現了一個simple c,我做這個項目是爲了之後做kiana語言打下基礎,所以我這門在simple c上修改的語言就叫kic了!


kic語言定義

kic語言是c語言的子集(增加了elif的功能),下面就是kic語言的EBNF定義
EBNF中,{}是0次或多次,[]是0次或一次,<>是非終結符,""是終結符






目錄
1.kic詞法定義
1.1.關鍵字
1.2.標識符
1.3.整型常量
1.4.字符常量
1.5.字符串常量
1.6.運算符及分隔符
1.7.註釋
2.kic語法定義
2.1.外部定義
2.1.1.函數定義
2.1.2.聲明
2.1.3.類型
2.1.4.聲明符
2.2.語句
2.2.1.複合語句
2.2.2.表達式語句與空語句
2.2.3.選擇語句
2.2.4.循環語句
2.2.5.跳轉語句
2.3.表達式
2.3.1.賦值表達式
2.3.2.是否相等表達式
2.3.3.判斷表達式
2.3.4.四則運算表達式
2.3.5.一元表達式
2.3.6.後綴表達式












---------------------------------1.kic詞法定義--------------------------------------------------------------------------------------------
1.1.關鍵字
char                        x86:1 byte     x64:1 byte
ATTENTION:pointer           x86:4 byte     x64:8 byte        
int                         x86:4 byte     x64:4 byte
double                      x86:8 byte     x64:8 byte
void 
struct
if
elif
else
for
continue
break
return
sizeof
__cdecl        調用約定
__stdcall      調用約定
__align        __align(n)強制結構成員對齊到n




1.2.標識符
<identifier>    ->   <no digit>{<digit>|<no digit>}
<no digit>      ->   "_"|"a"|"b"|"c"|"d"|"e"|"f"|"g"|"h"|"i"|"j"|"k"|"l"|"m"|"n"|"o"|"p"|"q"|"r"|"s"|"t"|"u"|"v"|"w"|"x"|"y"|
                     "z"|"A"|"B"|"C"|"D"|"E"|"F"|"G"|"H"|"I"|"J"|"K"|"L"|"M"|"N"|"O"|"P"|"Q"|"R"|"S"|"T"|"U"|"V"|"W"|"X"|"Y"|"Z"
<digit>         ->   "0"|"1"|"2"|"3"|"4"|"5"|"6"|"7"|"8"|"9"



1.3.整型常量
<const number>  ->   <digit>{<digit>}


1.4.字符常量
<const char>    ->   '<kic sign>'
<kic sign>      ->   <trope sign>|(characters other than ' and \ and new line characters)
<trope sign>    ->   (\0)|(\')|(\")|(\\)|(\a)|(\b)|(\f)|(\n)|(\r)|(\t)|(\v)
                    ATTENTION:             響鈴 推格 換頁  換行 回車 水平製表 垂直製表
                                                                       
                         

1.5.字符串常量
<const string>  ->   "{<cluster sign>}"
<cluster sign>  ->   <trope sign>|(characters other than " and \ and new line characters)


1.6.運算符及分隔符
ATTENTION:# and the rest of it are ignored
+
-
*
/     
ATTENTION:% was replaced with function int mod(int _a,int _b)
==
!=
<
<=
>
>=
=
->
.
&
*
ATTENTION:&& was replaced with function int AND(int _a,int _b)
ATTENTION:|| was replaced with function int OR(int _a,int _b)
ATTENTION:!  was replaced with function int NOT(int _a)
(
)
ATTENTION:[] was not recognized
{
}
;
,
...





1.7.註釋
//          單行註釋
/**/        多行註釋





----------------------------------2.kic語法定義-------------------------------------------------------------------------------------------
2.1.外部定義
<interpret unit>                    ->   {<outer declaration>}<EOF>
<outer declaration>                 ->   <function define>|<declaration>
    
    文件中要麼是函數定義,要是是聲明。



2.1.1.函數定義
<function define>                   ->   <type><declaration sign><function body>
<function body>                     ->   <composite statement>




2.1.2.聲明
<declaration>                       ->   <type> <first value declaration sign>{","<first value declaration sign>} ";"
<first value declaration sign>      ->   <declaration sign>["=" <assign expression>]

    聲明可以是結構體聲明



2.1.3.類型
<type>                              ->   "char"|"int"|"double"|"void"|<struct type>
<struct type>                       ->   "struct" <identifier> "{" <struct declaration table>  "}"  | 
                                         "struct"  <identifier>
<struct declaration table>          ->   <struct declaration>{<struct declaration>}
<struct declaration>                ->   <type> {<struct declaration sign table>} ";"
<struct declaration sign table>     ->   <declaration sign>{","<declaration sign>} 

    結構體聲明表是多條變量聲明語句。結構體聲明符表是單條變量聲明語句。
    結構體內的聲明不能是自己的實例,只能是自己的指針!



2.1.4.聲明符
<declaration sign>                  ->   {"*"} ["__cdecl"|"__stdcall"] [<struct member align>] <direct declaration sign>
<struct member align>               ->   "__align" "(" <const number> ")"
<direct declaration sign>           ->   <identifier>[<direct declaration sign suffix>]
<direct declaration sign suffix>    ->   "(" [<Formal parameter table>] ")" 
<Formal parameter table>            ->   <type><declaration sign> {","<type><declaration sign>} [",""..."]

    kic語言不提供數組功能!





2.2.語句
<statement>                         ->   {
                                            <composite statement> |
                                            <if statement> |
                                            <for statement> |
                                            <continue statement> |
                                            <break statement> |
                                            <return statement> |
                                            <expression statement>
                                        }




2.2.1.複合語句
<composite statement>               ->   "{" { (<declaration>|<statement>) } "}"

    不強制聲明一定寫在前面


2.2.2.表達式語句與空語句
<expression statement>              ->   [<expression>] ";"



2.2.3.選擇語句
<if statement>                      ->   "if" "(" <expression> ")" <statement> 
                                         {"elif" "(" <expression> ")" <statement>} 
                                         ["else" <statement>]



2.2.4.循環語句
<for statement>                     ->   "for" "(" <expression statement><expression statement><expression> ")" <statement>



2.2.5.跳轉語句
<continue statement>                ->   "continue" ";"
<break statement>                   ->   "break" ";"
<return statement>                  ->   "return" [<expression>] ";"




2.3.表達式
<expression>                        ->   <assign expression>{","<assign expression>}



2.3.1.賦值表達式
<assign expression>                 ->   <equal or not expression>|
                                         (<unary expression>["="<assign expression>])

    賦值語句也是表達式,判斷表達式等也是賦值語句。
    這說明kic語言中單條語句可以是沒有意義的,比如"a+b;",沒有保留計算結果。
    這樣的話,安全性就有問題!!!




2.3.2.是否相等表達式
<equal or not expression>           ->   <judge expression>["=="<judge expression>|"!="<judge expression>]



2.3.3.判斷表達式
<judge expression>                  ->   <+- expression>{("<"|"<="|">"|">=")<+- expression>}




2.3.4.四則運算表達式
<+- expression>                     ->   <*/ expression>{("+"|"-")<*/ expression>}
<*/ expression>                     ->   <unary expression>{("*"|"/")<unary expression>}




2.3.5.一元表達式
<unary expression>                  ->   <suffix expression> |
                                         "&"<unary expression> |
                                         "*"<unary expression> |
                                         "+"<unary expression> |
                                         "-"<unary expression> |
                                         <sizeof expression>
<sizeof expression>                 ->   "sizeof" "(" <type> ")"



2.3.6.後綴表達式
<suffix expression>                 ->   <identifier>(<function call>|<struct member call>)
<function call>                     ->   [
                                            "(" [<real parameter expression table>] ")" 
                                         ]
 <struct member call>               ->   {  
                                            "." <identifier> |
                                            "->" <identifier>
                                         }
<real parameter expression table>   ->   <assign expression>{","<assign expression>}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章