編譯原理 算符優先法文法分析

2021-10-25 05:33:38 字數 4572 閱讀 2780

#include

#include

#include

#include

using

namespace std;

//輸入文法

string input;

//儲存文法

vectorcontext;

//非終結符

vector<

char

>vn;

//終結符

vector<

char

>vt;

//firstvt集

map<

char

, string>firstvt;

//lastvt集

map<

char

, string>lastvt;

//算符優先關係表

map<

char

, map<

char

,char

>>operatortable;

//處理文法、終結符、非終結符

void

inputcontext()

}if(input[i]

>=

'a'&& input[i]

<=

'z')}}

} cout << endl;

//輸出終結符

cout <<

"終結符: "

;for

(int i =

0; i < vn.

size()

; i++

) cout << endl;

//輸出非終結符

cout <<

"非終結符: "

;for

(int i =

0; i < vt.

size()

; i++

) cout << endl;

}//計算firstvt集,one

void

initfirstvt()

if(context[i]

.size()

>=4)

}if(context[i][3

]>=

'a'&& context[i][3

]<=

'z'&& context[i][3

]!= context[i][0

])}}

//輸出

cout << endl;

cout <<

"中間結果"

<< endl;

map<

char

, string>

::iterator it;

for(it = firstvt.

begin()

; it != firstvt.

end(

); it++)}

//計算firstvt集,two

void

calfirstvt

(char start)}}

//計算firstvt,finally

void

allcalfirstvt()

}//計算lastvt集,one

void

initlastvt()

if(context[i]

.size()

>=5)

}if(context[i]

[context[i]

.size()

-1]>=

'a'&& context[i]

[context[i]

.size()

-1]<=

'z'&& context[i]

[context[i]

.size()

-1]!= context[i][0

])}}

//輸出

cout << endl;

cout <<

"中間結果"

<< endl;

map<

char

, string>

::iterator it;

for(it =lastvt.

begin()

; it != lastvt.

end(

); it++)}

//計算lastvt集,two

void

callastvt

(char start)

}//cout << lastvt[start] << endl;

}//計算firstvt,finally

void

allcallastvt()

}//生成算符優先關係表

void

caloperatortable()

}if((

(context[i]

[j+1

]>=

'a'&& context[i]

[j +1]

<=

'z')

|| context[i]

[j +1]

=='('

|| context[i]

[j +1]

==')'

|| context[i]

[j +1]

=='+'

|| context[i]

[j +1]

=='*'

|| context[i]

[j +1]

=='!'

|| context[i]

[j +1]

=='#'

)&& context[i]

[j]>=

'a'&& context[i]

[j]<=

'z')}}

if(context[i]

.size()

>=

5&&j <

(context[i]

.size()

-1))

}if(context[i]

.size()

>=

6&& j <

(context[i]

.size()

-2))

}}} cout << endl;

cout <<

"算符優先關係表"

<< endl;

map<

char

, map<

char

,char

>>

::iterator it;

for(it = operatortable.

begin()

; it != operatortable.

end(

); it++)}

}//規約

void

gy(string s)

char c = stks[len -1]

;if(operatortable[c]

[s[i]]!=

'>'

&& operatortable[c]

[s[i]]!=

'<'

&& operatortable[c]

[s[i]]!=

'=')

if(operatortable[c]

[s[i]]==

'<'

) cout << stks <<

" "

<<

'<'

<<

" "

<< output <<

" "

<<

"移進"

<< endl;

stks +

= s[i]

;continue;}

if(operatortable[c]

[s[i]]==

'>'

) cout << stks <<

" "

<<

'>'

<<

" "

<< output <<

" "

<<

"規約"

<< endl;

for(

int k = stks.

size()

-1; k >=

0; k--)if

((stks[k]

=='*'

|| stks[k]

=='+'

)&&stks[k+1]

=='e'

&&stks[k-1]

=='e')if

(stks[k]

=='('

&& stks[k +2]

==')')}

continue;}

if(operatortable[c]

[s[i]]==

'=')

for(

int t =

0; t < stks.

size()

; t++)}

if(num==1)

else}}

}int

main()

編譯原理 算符優先分析法

一 算符文法的定義 二 定義任意兩個終結符號之間的優先關係 解釋 1 ab兩個可以同時規約,故優先順序相等 2 將r的推導式代入p的產生式中,最終也會形成如 1 中一樣的p的產生式的形式,但此時需要對b先進行規約,再規約a,故a的優先順序小於b的優先順序 3 同理可得,b的優先順序小於a的優先順序 ...

編譯原理 算符運算優先

1 移動規約分析法 自底向上的語法分析方法,也稱為 移動歸約分析法 2 文法g s s a 且a b則稱b是句型 b 相對於非終結符a的短語 素短語與最左素短語 g的句型的素短語是乙個短語,它至少包含乙個終結符,且除自身外不再包含其他素短語。處於句型最左邊的素短語為最左素短語 控制代碼 乙個句型的最...

編譯原理 算符優先分析法詳解

算符優先分析法 operator precedence parse 是仿效四則運算的計算過程而構造的一種語法分析方法。算符優先分析法的關鍵是比較兩個相繼出現的終結符的優先順序而決定應採取的動作。優點 簡單,有效,適合表示式的分析。缺點 只適合於算符優先文法,是乙個不大的文法類。定義 短語 設有文法g...