『壹』 編譯原理與嵌入式的關系
總有一天你會想學的。現在不想學可以先放放,能過就行。興趣最重要,別磨滅了。以後用到了自然就想學了。
編譯原理主要講的是如何去做個編譯器。其中的很多概念對開發很有用,如有限狀態自動機。編譯器如何代碼優化,優化了那些部分。做嵌入式應該知道怎麼優化,畢竟資源是有限的。
『貳』 編譯原理課程設計
%{
/* 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]={'