‘壹’ 编译原理与嵌入式的关系
总有一天你会想学的。现在不想学可以先放放,能过就行。兴趣最重要,别磨灭了。以后用到了自然就想学了。
编译原理主要讲的是如何去做个编译器。其中的很多概念对开发很有用,如有限状态自动机。编译器如何代码优化,优化了那些部分。做嵌入式应该知道怎么优化,毕竟资源是有限的。
‘贰’ 编译原理课程设计
%{
/* FILENAME: C.Y */
%}
#define YYDEBUG_LEXER_TEXT (yylval) /* our lexer loads this up each time */
#define YYDEBUG 1 /* get the pretty debugging code to compile*/
#define YYSTYPE char * /* interface with flex: should be in header file */
/* Define terminal tokens */
/* keywords */
%token AUTO DOUBLE INT STRUCT
%token BREAK ELSE LONG SWITCH
%token CASE ENUM REGISTER TYPEDEF
%token CHAR EXTERN RETURN UNION
%token CONST FLOAT SHORT UNSIGNED
%token CONTINUE FOR SIGNED VOID
%token DEFAULT GOTO SIZEOF VOLATILE
%token DO IF STATIC WHILE
/* ANSI Grammar suggestions */
%token IDENTIFIER STRINGliteral
%token FLOATINGconstant INTEGERconstant CHARACTERconstant
%token OCTALconstant HEXconstant
/* New Lexical element, whereas ANSI suggested non-terminal */
%token TYPEDEFname /* Lexer will tell the difference between this and
an identifier! An identifier that is CURRENTLY in scope as a
typedef name is provided to the parser as a TYPEDEFname.*/
/* Multi-Character operators */
%token ARROW /* -> */
%token ICR DECR /* ++ -- */
%token LS RS /* << >> */
%token LE GE EQ NE /* <= >= == != */
%token ANDAND OROR /* && || */
%token ELLIPSIS /* ... */
/* modifying assignment operators */
%token MULTassign DIVassign MODassign /* *= /= %= */
%token PLUSassign MINUSassign /* += -= */
%token LSassign RSassign /* <<= >>= */
%token ANDassign ERassign ORassign /* &= ^= |= */
%start translation_unit
%%
/* CONSTANTS */
constant:
INTEGERconstant
| FLOATINGconstant
/* We are not including ENUMERATIONconstant here because we
are treating it like a variable with a type of "enumeration
constant". */
| OCTALconstant
| HEXconstant
| CHARACTERconstant
;
string_literal_list:
STRINGliteral
| string_literal_list STRINGliteral
;
/************************* EXPRESSIONS ********************************/
primary_expression:
IDENTIFIER /* We cannot use a typedef name as a variable */
| constant
| string_literal_list
| '(' comma_expression ')'
;
postfix_expression:
primary_expression
| postfix_expression '[' comma_expression ']'
| postfix_expression '(' ')'
| postfix_expression '(' argument_expression_list ')'
| postfix_expression {} '.' member_name
| postfix_expression {} ARROW member_name
| postfix_expression ICR
| postfix_expression DECR
;
member_name:
IDENTIFIER
| TYPEDEFname
;
argument_expression_list:
assignment_expression
| argument_expression_list ',' assignment_expression
;
unary_expression:
postfix_expression
| ICR unary_expression
| DECR unary_expression
| unary_operator cast_expression
| SIZEOF unary_expression
| SIZEOF '(' type_name ')'
;
unary_operator:
'&'
| '*'
| '+'
| '-'
| '~'
| '!'
;
cast_expression:
unary_expression
| '(' type_name ')' cast_expression
;
multiplicative_expression:
cast_expression
| multiplicative_expression '*' cast_expression
| multiplicative_expression '/' cast_expression
| multiplicative_expression '%' cast_expression
;
additive_expression:
multiplicative_expression
| additive_expression '+' multiplicative_expression
| additive_expression '-' multiplicative_expression
;
shift_expression:
additive_expression
| shift_expression LS additive_expression
| shift_expression RS additive_expression
;
relational_expression:
shift_expression
| relational_expression '<' shift_expression
| relational_expression '>' shift_expression
| relational_expression LE shift_expression
| relational_expression GE shift_expression
;
equality_expression:
relational_expression
| equality_expression EQ relational_expression
| equality_expression NE relational_expression
;
AND_expression:
equality_expression
| AND_expression '&' equality_expression
;
exclusive_OR_expression:
AND_expression
| exclusive_OR_expression '^' AND_expression
;
inclusive_OR_expression:
exclusive_OR_expression
| inclusive_OR_expression '|' exclusive_OR_expression
;
logical_AND_expression:
inclusive_OR_expression
| logical_AND_expression ANDAND inclusive_OR_expression
;
logical_OR_expression:
logical_AND_expression
| logical_OR_expression OROR logical_AND_expression
;
conditional_expression:
logical_OR_expression
| logical_OR_expression '?' comma_expression ':'
conditional_expression
;
assignment_expression:
conditional_expression
| unary_expression assignment_operator assignment_expression
;
assignment_operator:
'='
| MULTassign
| DIVassign
| MODassign
| PLUSassign
| MINUSassign
| LSassign
| RSassign
| ANDassign
| ERassign
| ORassign
;
comma_expression:
assignment_expression
| comma_expression ',' assignment_expression
;
constant_expression:
conditional_expression
;
/* The following was used for clarity */
comma_expression_opt:
/* Nothing */
| comma_expression
;
/******************************* DECLARATIONS *********************************/
/* The following is different from the ANSI C specified grammar.
The changes were made to disambiguate typedef's presence in
declaration_specifiers (vs. in the declarator for redefinition);
to allow struct/union/enum tag declarations without declarators,
and to better reflect the parsing of declarations (declarators
must be combined with declaration_specifiers ASAP so that they
are visible in scope).
Example of typedef use as either a declaration_specifier or a
declarator:
typedef int T;
struct S { T T;}; /* redefinition of T as member name * /
Example of legal and illegal statements detected by this grammar:
int; /* syntax error: vacuous declaration * /
struct S; /* no error: tag is defined or elaborated * /
Example of result of proper declaration binding:
int a=sizeof(a); /* note that "a" is declared with a type in
the name space BEFORE parsing the initializer * /
int b, c[sizeof(b)]; /* Note that the first declarator "b" is
declared with a type BEFORE the second declarator is
parsed * /
*/
declaration:
sue_declaration_specifier ';'
| sue_type_specifier ';'
| declaring_list ';'
| default_declaring_list ';'
;
/* Note that if a typedef were redeclared, then a declaration
specifier must be supplied */
default_declaring_list: /* Can't redeclare typedef names */
declaration_qualifier_list identifier_declarator {} initializer_opt
| type_qualifier_list identifier_declarator {} initializer_opt
| default_declaring_list ',' identifier_declarator {} initializer_opt
;
declaring_list:
declaration_specifier declarator {} initializer_opt
| type_specifier declarator {} initializer_opt
| declaring_list ',' declarator {} initializer_opt
;
declaration_specifier:
basic_declaration_specifier /* Arithmetic or void */
| sue_declaration_specifier /* struct/union/enum */
| typedef_declaration_specifier /* typedef*/
;
type_specifier:
basic_type_specifier /* Arithmetic or void */
| sue_type_specifier /* Struct/Union/Enum */
| typedef_type_specifier /* Typedef */
;
declaration_qualifier_list: /* const/volatile, AND storage class */
storage_class
| type_qualifier_list storage_class
| declaration_qualifier_list declaration_qualifier
;
type_qualifier_list:
type_qualifier
| type_qualifier_list type_qualifier
;
declaration_qualifier:
storage_class
| type_qualifier /* const or volatile */
;
type_qualifier:
CONST
| VOLATILE
;
basic_declaration_specifier: /*Storage Class+Arithmetic or void*/
declaration_qualifier_list basic_type_name
| basic_type_specifier storage_class
| basic_declaration_specifier declaration_qualifier
| basic_declaration_specifier basic_type_name
;
basic_type_specifier:
basic_type_name /* Arithmetic or void */
| type_qualifier_list basic_type_name
| basic_type_specifier type_qualifier
| basic_type_specifier basic_type_name
;
sue_declaration_specifier: /* Storage Class + struct/union/enum */
declaration_qualifier_list elaborated_type_name
| sue_type_specifier storage_class
| sue_declaration_specifier declaration_qualifier
;
sue_type_specifier:
elaborated_type_name /* struct/union/enum */
| type_qualifier_list elaborated_type_name
| sue_type_specifier type_qualifier
;
typedef_declaration_specifier: /*Storage Class + typedef types */
typedef_type_specifier storage_class
| declaration_qualifier_list TYPEDEFname
| typedef_declaration_specifier declaration_qualifier
;
typedef_type_specifier: /* typedef types */
TYPEDEFname
| type_qualifier_list TYPEDEFname
| typedef_type_specifier type_qualifier
;
storage_class:
TYPEDEF
| EXTERN
| STATIC
| AUTO
| REGISTER
;
basic_type_name:
INT
| CHAR
| SHORT
| LONG
| FLOAT
| DOUBLE
| SIGNED
| UNSIGNED
| VOID
;
elaborated_type_name:
aggregate_name
| enum_name
;
aggregate_name:
aggregate_key '{' member_declaration_list '}'
| aggregate_key identifier_or_typedef_name
'{' member_declaration_list '}'
| aggregate_key identifier_or_typedef_name
;
‘叁’ 璁$畻链虹戝︿笌鎶链涓扑笟锛氭墦阃犵紪绋嬮珮镓
璁$畻链虹戝︿笌鎶链涓扑笟娑电洊浜嗘暟瀛︺佽嫳璇銆佺数瀛愭妧链銆佽$畻链虹戝︾悊璁哄拰搴旂敤绛夊氢釜棰嗗烟銆傚湪杩欎釜鍏呮弧鎸戞垬鍜屾満阆囩殑涓扑笟閲岋纴浣犲皢涓嶆柇𨰾揿𪾢镊宸辩殑鐭ヨ瘑鍜岃兘锷涳纴鎴愪负璁$畻链虹戝︿笌鎶链棰嗗烟镄勪郊浣艰咃紒
📈镓扑笅鍧氩疄镄勬暟瀛﹀熀纭
浠庨珮绛夋暟瀛︺佹傜巼缁熻″埌绂绘暎鏁板︼纴浣犲皢镓扑笅鍧氩疄镄勬暟瀛﹀熀纭锛屼负浣犳湭𨱒ョ殑璁$畻链虹戝﹀拰搴旂敤鎻愪緵链夊姏鏀鎸併
🌍锲介檯鑸炲彴镊淇′氦娴
澶у﹁嫳璇鍜屼笓涓氲嫳璇灏嗗姪浣犱竴镊备箣锷涳纴鍦ㄥ浗闄呰垶鍙颁笂镊淇′氦娴侊纴涓轰綘镄勮亴涓氩彂灞曟墦寮镟村箍阒旂殑瑙嗛噹銆
🔌镓扑笅鐢靛瓙鎶链锘虹
浣犲皢瀛︿範鐢佃矾銆佹ā𨰾熺数瀛愬拰鏁板瓧鐢靛瓙锛屼负璁$畻链虹‖浠剁殑璁捐″拰寮鍙戞墦涓嫔潥瀹炲熀纭銆
💻娣卞叆璁$畻链虹戝︾悊璁
浣犲皢娣卞叆浜呜В鏁版嵁缁撴瀯銆佹搷浣灭郴缁熴佺紪璇戝师鐞嗐佽$畻链虹绣缁滃拰鏁版嵁搴揿师鐞嗭纴涓轰綘链𨱒ョ殑杞浠跺紑鍙戝拰绯荤粺璁捐℃彁渚涙湁锷涙敮鎸併
👨💻鎴愪负缂栫▼楂樻坠
浣犲皢鎺ヨЕ鍒版眹缂栬瑷銆丆++绋嫔簭璁捐°佹帴鍙f妧链銆丣ava鍜孷C++绛夌紪绋嬭瑷鍜屽紑鍙戝伐鍏凤纴璁╀綘鎴愪负缂栫▼楂樻坠銆
🔒淇濋┚鎶よ埅淇℃伅瀹夊叏
浣犲皢瀛︿範璁$畻链虹梾姣掑垎鏋愩佺绣缁沧敾鍑讳笌阒叉姢銆佸瘑镰佸﹀簲鐢ㄧ瓑缃戠粶瀹夊叏鐭ヨ瘑锛屼负淇℃伅瀹夊叏淇濋┚鎶よ埅銆
🎮瀹炵幇娓告垙寮鍙戞ⅵ𨱍
濡傛灉浣犲规父鎴忓紑鍙戞劅鍏磋叮锛岄偅涔堢绣缁沧父鎴忕悊璁恒佹父鎴忚捐″拰涓夌淮锷ㄧ敾绛夎剧▼灏呜╀綘瀹炵幇娓告垙寮鍙戠殑姊︽兂銆
‘肆’ 编译原理和算法分析与设计哪个更难
编译原理和算法分析与设计相比,算法分析与设计更难。
算法分析的话比较偏重整数规划,数列的求解,组合数学等等,设计那就要靠悟性了,而且要见多识广,不管你使用的是什么语言,也不管语言怎么发展,数据结构是变不了多少的。算法设计也差不多,帮助你改善解决问题的思维。
算法分析与设计的内容:
算法设计与分析是整个CS课程体系当中最为重要的几门课程之一,因为这门课是现代计算机科学发展的核心课程,和离散数学、数理逻辑四论地位相当,号称必修中的必修,不过一般CS系不需要学数理逻辑四论,国内大学的四论教学开展的也不多。因此请大家一定要在这门课打好基础,学好这门课能让你未来的工作和学习非常轻松。
‘伍’ 编译原理课程设计-词法分析器设计(C语言)
#include"stdio.h"/*定义I/O库所用的某些宏和变量*/
#include"string.h"/*定义字符串库函数*/
#include"conio.h"/*提供有关屏幕窗口操作函数*/
#include"ctype.h"/*分类函数*/
charprog[80]={'