當前位置:編程學習大全網 - 編程語言 - 編譯原理究竟有沒有用?對編程的人

編譯原理究竟有沒有用?對編程的人

我跟妳說,編譯原理太有用了。

我是做手機遊戲的,現在做壹個遊戲引擎。既然是引擎,就需要提供抽象的東西給上層使用。這裏,我引入了腳本系統。

這個腳本系統包括壹堆我根據實際需求自行設計的指令集,包括基本的輸入輸出,四則運算,系統功能調用,函數聲明,調用等等(其實妳要是用過lua或者其他遊戲腳本妳就知道了。)整個結構包括指令集、編譯器、虛擬機等部分。這樣,引擎提供壹些基礎服務,比如繪圖,計算位置等,腳本就可以非常簡單控制遊戲。甚至快速構建新遊戲。妳應該知道QUAKE引擎吧?

這裏提供給妳壹個計算器的小程序,應用了EBNF理論,支持表達式,比如(2+3*6)*4+4,妳自己體驗壹下它的簡潔和強大。

/*

simple integer arithmetic calculator according to the EBNF

<exp> -> <term>{<addop><term>}

<addop>->+|-

<term>-><factor>{<mulop><factor>}

<mulop> -> *

<factor> -> ( <exp> )| Number

Input a line of text from stdin

Outputs "Error" or the result.

*/

#include <stdio.h>

#include <stdlib.h>

#include <ctype.h>

char token;/*global token variable*/

/*function prototypes for recursive calls*/

int exp(void);

int term(void);

int factor(void);

void error(void)

{

fprintf(stderr,"Error\n");

exit(1);

}

void match(char expectedToken)

{

if(token==expectedToken)token=getchar();

else error();

}

main()

{

int result;

token = getchar();/*load token with first character for lookahead*/

result = exp();

if(token=='\n')/*check for end of line */

printf("Result = %d\n",result);

else error();/*extraneous cahrs on line*/

return 0;

}

int exp(void)

{

int temp = term();

while((token=='+')||(token=='-'))

switch(token)

{

case '+':

match('+');

temp+=term();

break;

case '-':

match('-');

temp-=term();

break;

}

return temp;

}

int term(void)

{

int temp = factor();

while (token=='*')

{

match('*');

temp*=factor();

}

return temp;

}

int factor(void)

{

int temp;

if(token=='('){

match('(');

temp = exp();

match(')');

}

else if(isdigit(token)){

ungetc(token,stdin);

scanf("%d",&temp);

token = getchar();

}

else error();

return temp;

}

其實編程學到壹定程度總是沒有方向了,總是在問學C/C++下壹步怎麽學啊,覺得掌握了該語言了雲雲,實際上,妳缺少的就是這些軟的東西,缺少的是理論。

編譯原理不是單壹的理論,它涵蓋了壹個niche,裏面可以學到很多其他知識,比如正則表達式、BNF、EBNF、分析樹、語法樹還有很多運行時環境等知識

這些給妳帶來的是非常豐厚的回報。不說多了,學完運行時,妳就會加深對C++語言本身的理解。

妳要想有好的發展,還是學吧。

  • 上一篇:求壹個用csocket類實現聊天室程序
  • 下一篇:我的電腦為何常常出現ox5adc1513指令引用ox00000014內存該內存不能為read..該怎麽解決這個問題?
  • copyright 2024編程學習大全網