[kic]Language definition, kic is a subset of C language------------Reference: "Make your own compiler and linker"

Preface

I wanted to read "Homemade Programming Language, Based on C Language", but the only e-book I didn't get was a table of contents and two chapters, so I could only read "Do it yourself compiler and linker". I heard that there are some bugs in this book, but it doesn't really matter. The problems are found in general books, and even the textbooks of the "big country heavy equipment" have printing errors. The author in this book has implemented a simple c. I did this project to lay the foundation for other languages ​​in the future, so my language modified on simple c is called kic!


kic language definition

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>}




Reference: "Make your own compiler and linker"

Guess you like

Origin blog.csdn.net/weixin_41374099/article/details/105765821