注重版权,若要转载烦请附上作者和链接
作者:Joshua_yi
链接:https://blog.csdn.net/weixin_44984664/article/details/109227729
C++ 是 C 语言的继承,它既可以进行 C 语言的过程化程序设计,又可以进行以抽象数据类型为特点的基于对象的程序设计,还可以进行以继承和多态为特点的面向对象的程序设计。C++ 不仅拥有计算机高效运行的实用性特征,同时还致力于提高大规模程序的编程质量与程序设计语言的问题描述能力。
一个完整的 c++ 程序在进行词法分析后被分解成了一个个的 token(单词),而语法分析则是在一个个独立的 token 中给它们建立联系,得到相应的逻辑关系。
c++ 的关键字不能在程序中重新定义或重载;除了关键字,有些标识符也有特殊含义,有时可以作变量或函数名,但在一些特定环境下有特殊含义。
c++ 的标识符在命名时必须避开关键字,标识符可以代表 C++ 程序中的变量名、函数名、类名等等。
常量可以是数字、字符、字符串,在程序中有不变的值。
声明语句由类型 (type) 和你想要在程序中使用的实体构成,同时你也可以定义该实体的作用域。每种实体的定义方式不同,通过定义实体可以在程序中发挥作用。
上下文无关文法,是一种用来描述语言语法规则的文法,属于 2 型文法 (产生式左边有且仅有一个非终结符,右边可以含有若干个终结符和非终结符)。
上下文无关文法是一个四元组,由终结符、非终结符、开始符号、产生式构成。
终结符构成句子的实际内容。词法单元
非终结符表示在句子中不同的短语或者子句,体现出语言的层次结构,实质是由单个或多个终结符构成。语法变量
开始符号存在且唯一,属于非终结符.
产生式定义了终结符、非终结符之间的关系和层次,是上下文无关文法的基本规则。产生式主要用来表示某个构造的某种书写方式,也可以检验某一字符串是否符合该文法。
其命名规则为:字母、下划线开头,后跟任意长度的字母、数字或下划线字符。 i d 1 ⟶ _ ∣ a ∣ b ∣ c ∣ . . . ∣ z ∣ A ∣ B ∣ C ∣ . . . ∣ Z i d 2 ⟶ _ a ∣ b ∣ c ∣ . . . ∣ z ∣ A ∣ B ∣ C ∣ . . . ∣ Z ∣ 0 ∣ 1 ∣ 2 ∣ . . . ∣ 9 i d ⟶ i d 1 ∣ i d i d 2 \begin{aligned} id1 \longrightarrow &\ \_\\ &|\ a|b|c|...|z\\ &|\ A|B|C|...|Z\\ id2 \longrightarrow &\ \_\\ &\ a|b|c|...|z\\ &|\ A|B|C|...|Z\\ &|\ 0|1|2|...|9\\ id \longrightarrow &\ id1\\ &|\ id\ id2 \end{aligned} id1⟶id2⟶id⟶ _∣ a∣b∣c∣...∣z∣ A∣B∣C∣...∣Z _ a∣b∣c∣...∣z∣ A∣B∣C∣...∣Z∣ 0∣1∣2∣...∣9 id1∣ id id2
id1 可以是字母 (大小写)、下划线;id2 可以是字母 (大小写)、下划线、数字。id 是由字母、下划线开头,后跟若干个字母、下划线、数字组成
s t o r e ⟶ a u t o ∣ r e g i s t e r ∣ s t a t i c ∣ e x t e r n ∣ ϵ t y p e ⟶ i n t ∣ b o o l ∣ f l o a t ∣ d o u b l e ∣ c h a r ∣ c h a r ∗ ∣ s t r i n g i d l i s t ⟶ i d , l i s t ∣ i d d e c l ⟶ s t o r e t y p e i d l i s t \begin{aligned} store \longrightarrow & \ auto|\ register|\ static|\ extern |\ \epsilon\\ type \longrightarrow & \ int|\ bool|\ float|\ double|\ char|\ char*|\ string\\ idlist \longrightarrow & \ id,\ list|\ id\\ decl \longrightarrow & store\ type\ idlist \end{aligned} store⟶type⟶idlist⟶decl⟶ auto∣ register∣ static∣ extern∣ ϵ int∣ bool∣ float∣ double∣ char∣ char∗∣ string id, list∣ idstore type idlist store 表示变量的作用域,type 表示变量类型,idlist 表示标识符列表,decl 表示声明语句
分为8进制, 10进制,16进制的常量 i n t e g e r − c o n s t ⟶ d e c i m a l − c o n s t ∣ o c t a l − c o n s t ∣ h e x a d e c i m a l − c o n s t d e c i m a l − c o n s t ⟶ n o n z e r o − d i g i t ∣ d e c i m a l − c o n s t d i g i t o c t a l − c o n s t ⟶ O ∣ o c t a l − c o n s t o c t a l − d i g i t h e x a d e c i m a l − c o n s t ⟶ h e x a d e c i m a l − p r e f i x h e x a d e c i m a l − d i g i t ∣ h e x a d e c i m a l − c o n s t h e x a d e c i m a l − d i g i t h e x a d e c i m a l − p r e f i x ⟶ ′ 0 x ′ ∣ ′ 0 X ′ n o n z e r o − d i g i t ⟶ 1 ∣ 2 ∣ 3 ∣ 4 ∣ 5 ∣ 6 ∣ 7 ∣ 8 ∣ 9 o c t a l − d i g i t ⟶ 0 ∣ 1 ∣ 2 ∣ 3 ∣ 4 ∣ 5 ∣ 6 ∣ 7 h e x a d e c i m a l − d i g i t ⟶ 0 ∣ 1 ∣ 2 ∣ 3 ∣ 4 ∣ 5 ∣ 6 ∣ 7 ∣ 8 ∣ 9 ∣ A ∣ B ∣ C ∣ D ∣ E ∣ F \begin{aligned} integer-const \longrightarrow &\ decimal-const|\ octal-const|\ hexadecimal-const\\ decimal-const \longrightarrow &\ nonzero-digit|\ decimal-const digit\\ octal-const \longrightarrow &\ O|\ octal-const octal-digit\\ hexadecimal-const \longrightarrow &\ hexadecimal-prefix\ hexadecimal-digit\\ &|\ hexadecimal-const\ hexadecimal-digit\\ hexadecimal-prefix \longrightarrow &\ '0x'|\ '0X'\\ nonzero-digit \longrightarrow &\ 1|2|3|4|5|6|7|8|9\\ octal-digit \longrightarrow &\ 0|1|2|3|4|5|6|7\\ hexadecimal-digit \longrightarrow &\ 0|1|2|3|4|5|6|7|8|9|A|B|C|D|E|F \end{aligned} integer−const⟶decimal−const⟶octal−const⟶hexadecimal−const⟶hexadecimal−prefix⟶nonzero−digit⟶octal−digit⟶hexadecimal−digit⟶ decimal−const∣ octal−const∣ hexadecimal−const nonzero−digit∣ decimal−constdigit O∣ octal−constoctal−digit hexadecimal−prefix hexadecimal−digit∣ hexadecimal−const hexadecimal−digit ′0x′∣ ′0X′ 1∣2∣3∣4∣5∣6∣7∣8∣9 0∣1∣2∣3∣4∣5∣6∣7 0∣1∣2∣3∣4∣5∣6∣7∣8∣9∣A∣B∣C∣D∣E∣F constant 指的是常量的值。
无符号数 u n s i g n e d _ i n t ⟶ i n t \begin{aligned} unsigned\_int \longrightarrow &\ int \end{aligned} unsigned_int⟶ int
有符号数 s i g n e d _ i n t ⟶ + i n t ∣ − i n t ∣ i n t \begin{aligned} signed\_int \longrightarrow &\ +int\\ &|\ -int\\ &|\ int \end{aligned} signed_int⟶ +int∣ −int∣ int
指数 s i g n e d _ n u m l i s t ⟶ + n u m l i s t ∣ − n u m l i s t ∣ n u m l i s t e x p o n e n t ⟶ s i g n e d _ i n t s i g n e d _ n u m l i s t ∣ s i g n e d _ i n t E ∣ e s i g n e d _ n u m l i s t \begin{aligned} signed\_numlist \longrightarrow &\ +numlist\\ &|\ -numlist\\ &|\ numlist\\ exponent \longrightarrow &\ signed\_int\ signed\_numlist\\ &|\ signed\_int\ E|e signed\_numlist \end{aligned} signed_numlist⟶exponent⟶ +numlist∣ −numlist∣ numlist signed_int signed_numlist∣ signed_int E∣esigned_numlist
指数由三部分构成,前面的小数 (signed_int),中间的字符 E/e,后面的指数(signed_numlist)
浮点型 n u m l i s t ⟶ n u m ∣ n u m n u m l i s t d o u b l e ⟶ i n t ∣ s i g n e d _ i n t . n u m l i s t ∣ e x p o n e n t \begin{aligned} numlist \longrightarrow &\ num\\ &|\ num\ numlist\\ double \longrightarrow &\ int\\ &|\ signed\_int.numlist\\ &|\ exponent \end{aligned} numlist⟶double⟶ num∣ num numlist int∣ signed_int.numlist∣ exponent numlist 是指数字串,一位或多位的 0-9 的数字。浮点型数字需要满足最高位数字不为 0,最末为数字为 0 也是合法的,例如:3.1200,且末位有多少位 0 都可以,不做要求;同时浮点型也可以用指数来表示
字符常量
numlist -> 基本源字符集减去单引号、反斜杠或换行符的字符
字符串常量 c o n s t _ c h a r ∗ ⟶ c o n s t _ c h a r ∣ c o n s t _ c h a r c o n s t _ c h a r ∗ \begin{aligned} const\_char* \longrightarrow &\ const\_char\\ &|\ const\_char\ const\_char* \end{aligned} const_char∗⟶ const_char∣ const_char const_char∗
等于、加等于、减等于、乘等于、除等于 (=, +=, -=, *=, /=) s t m t ⟶ i d = e x p r ∣ i d + = e x p r ∣ i d − = e x p r ∣ i d ∗ = e x p r ∣ i d / = e x p r \begin{aligned} stmt \longrightarrow & \ id\ =\ expr\\ & |\ id\ +=\ expr\\ & |\ id\ -=\ expr\\ & |\ id\ *=\ expr\\ & |\ id\ /=\ expr \end{aligned} stmt⟶ id = expr∣ id += expr∣ id −= expr∣ id ∗= expr∣ id /= expr
注意:除法的分母不能为 0!
前++, 后++ + + _ e x p r ⟶ e x p r + + ∣ + + e x p r \begin{aligned} ++\_expr \longrightarrow &\ expr++|\ ++expr \end{aligned} ++_expr⟶ expr++∣ ++expr
前–, 后-- − − _ e x p r ⟶ e x p r − − ∣ − − e x p r \begin{aligned} --\_expr \longrightarrow &\ expr--|\ --expr \end{aligned} −−_expr⟶ expr−−∣ −−expr
按照运算的优先级:从高到低
num 指的是整型、浮点型常量 expr指表达式
() e x p r ⟶ ( e x p r ) \begin{aligned} expr \longrightarrow &\ (expr) \end{aligned} expr⟶ (expr)
-负号, 右结合 e x p r ⟶ − e x p r \begin{aligned} expr \longrightarrow &\ -expr \end{aligned} expr⟶ −expr
逻辑!运算符,单目 e x p r ⟶ ! e x p r \begin{aligned} expr \longrightarrow &\ !expr \end{aligned} expr⟶ !expr
按位取反,单目 e x p r ⟶ ∼ e x p r \begin{aligned} expr \longrightarrow \thicksim expr \end{aligned} expr⟶∼expr
* / %表达式 e x p r ⟶ t e r m ∗ e x p r ∣ t e r m / e x p r ∣ t e r m % e x p r \begin{aligned} expr \longrightarrow &\ term*expr\\ &|\ term/expr\\ &|\ term\%expr \end{aligned} expr⟶ term∗expr∣ term/expr∣ term%expr
+ -表达式 e x p r ⟶ t e r m + e x p r ∣ t e r m − e x p r \begin{aligned} expr \longrightarrow &\ term+expr\\ &|\ term-expr \end{aligned} expr⟶ term+expr∣ term−expr
移位运算 e x p r ⟶ e x p r < < t e r m ∣ e x p r > > t e r m \begin{aligned} expr \longrightarrow &\ expr<<term\\ &|\ expr>>term \end{aligned} expr⟶ expr<<term∣ expr>>term
关系运算 e x p r ⟶ e x p r > e x p r ∣ e x p r > = e x p r ∣ e x p r < e x p r ∣ e x p r < = e x p r ∣ e x p r = = e x p r ∣ e x p r ! = e x p r \begin{aligned} expr \longrightarrow &\ expr>expr\\ &|\ expr>=expr\\ &|\ expr<expr\\ &|\ expr<=expr\\ &|\ expr==expr\\ &|\ expr!=expr \end{aligned} expr⟶ expr>expr∣ expr>=expr∣ expr<expr∣ expr<=expr∣ expr==expr∣ expr!=expr
位运算 e x p r ⟶ e x p r & t e r m ∣ e x p r ∧ t e r m ∣ e x p r ∣ t e r m \begin{aligned} expr \longrightarrow &\ expr\&term\\ &|\ expr\land term\\ &|\ expr|term \end{aligned} expr⟶ expr&term∣ expr∧term∣ expr∣term
逻辑与,逻辑非 e x p r ⟶ e x p r & & t e r m ∣ e x p r ∣ ∣ t e r m \begin{aligned} expr \longrightarrow &\ expr\&\&term\\ &|\ expr||term \end{aligned} expr⟶ expr&&term∣ expr∣∣term
s t m t ⟶ i f ( e x p r ) s t m t ∣ i f ( e x p r ) s t m t e l s e s t m t \begin{aligned} stmt \longrightarrow &\ if(expr)\ stmt\\ &|\ if(expr)\ stmt\ else\ stmt \end{aligned} stmt⟶ if(expr) stmt∣ if(expr) stmt else stmt
c a s e ⟶ c a s e ( c o n s t a n t ) s t m t ∣ c a s e ( c o n s t a n t ) s t m t b r e a k ∣ ϵ c a s e s ⟶ c a s e ∣ c a s e s c a s e s t m t ⟶ s w i t c h ( e x p r ) c a s e s d e f a u l t : s t m t ∣ c a s e s ( e x p r ) c a s e s ∣ ϵ \begin{aligned} case \longrightarrow &\ case(constant)\ stmt\\ &|\ case(constant)\ stmt\ break\\ &|\ \epsilon\\ cases \longrightarrow &\ case\\ &|\ cases\ case\\ stmt \longrightarrow &\ switch(expr)\ cases\ default\ :\ stmt\\ &|\ cases(expr)\ cases\\ &|\ \epsilon \end{aligned} case⟶cases⟶stmt⟶ case(constant) stmt∣ case(constant) stmt break∣ ϵ case∣ cases case switch(expr) cases default : stmt∣ cases(expr) cases∣ ϵ
m i d _ e x p r ⟶ e x p r ∣ ϵ s t m t ⟶ f o r ( m i d _ e x p r ; m i d _ e x p r ; m i d _ e x p r ) s t m t \begin{aligned} mid\_expr \longrightarrow &\ expr\\ &|\ \epsilon\\ stmt \longrightarrow &\ for(mid\_expr;\ mid\_expr;\ mid\_expr)\ stmt \end{aligned} mid_expr⟶stmt⟶ expr∣ ϵ for(mid_expr; mid_expr; mid_expr) stmt
s t m t ⟶ w h i l e ( e x p r ) s t m t \begin{aligned} stmt \longrightarrow \ while(expr)\ stmt \end{aligned} stmt⟶ while(expr) stmt
s t m t ⟶ d o s t m t w h i l e ( e x p r ) ; \begin{aligned} stmt \longrightarrow \ do\ stmt\ while(expr); \end{aligned} stmt⟶ do stmt while(expr);
这里的 function_name 是函数名,param 是单个参数,params 是参数列表。 f u n c _ d e l ⟶ t y p e f u n c t i o n _ n a m e ( p a r a m s ) ; p a r a m s ⟶ p a r a m ∣ p a r a m , p a r a m s ∣ ϵ p a r a m ⟶ t y p e i d \begin{aligned} func\_del \longrightarrow &\ type\ function\_name(params);\\ params \longrightarrow &\ param\\ &|\ param,\ params\\ &|\ \epsilon\\ param \longrightarrow &\ typeid \end{aligned} func_del⟶params⟶param⟶ type function_name(params); param∣ param, params∣ ϵ typeid
f u n c _ d e f ⟶ f u n c _ d e l s t m t \begin{aligned} func\_def \longrightarrow \ func\_del\ stmt \end{aligned} func_def⟶ func_del stmt
f u n c _ c a l l ⟶ f u n c t i o n _ n a m e ( p a r a m s ) ; \begin{aligned} func\_call \longrightarrow \ function\_name(params); \end{aligned} func_call⟶ function_name(params);
基于预备工作 1 https://blog.csdn.net/weixin_44984664/article/details/109006690的阶乘程序,编写相应的汇编程序,生成可执行文件,并对代码进行解释说明。
c++代码如下
#include<iostream> using namespace std; int main() { int i, n, f; cin >> n; i = 2; f = 1; while (i <= n) { f = f * i; i = i + 1; } cout << f << endl; return 0; }阶乘程序的x86汇编代码:
# data 段 存储初始化的全局变量 .data f: .long 1 .align 4 i: .long 2 .align 4 # bss 段 存储全局变量 .bss n: .zero 4 .align 4 # rodata 段 存储常量 .section .rodata STR0: .string "%d" STR1: .string "the result is: %d\n" # main函数 .text .globl main .type main, @function main: # scanf("%d", &n) pushl $n # 从右向左压入参数 pushl $STR0 call scanf addl $8, %esp # 不再需要参数 mov n, %eax mov f, %ebx mov i, %ecx #while(i<=n) L0: cmpl %ecx,%eax jl L1 #f=f*i imul %ecx,%ebx #i=i+1 add $1,%ecx jmp L0 #printf("%d",f) L1: pushl %ebx pushl $STR1 call printf addl $8, %esp # return 0 movl $0, %eax ret进行测试
gcc main.s -m32 -o main.out qemu-i386 main.outhttps://blog.csdn.net/m0_37149062/article/details/108240050 Latex输入特殊字符
https://blog.csdn.net/alan00000/article/details/51724252 arm-none-linux-gnueabi-gcc 下载地址
https://blog.csdn.net/2497242041/article/details/50095717 ARM汇编语言最常用指令
https://azeria-labs.com/writing-arm-assembly-part-1/ ARM官方指南
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=21098 GNU .note.GNU-stack的说明
https://blog.csdn.net/wangbotao1990/article/details/86995910 .section progbits的意思
