以下代碼應該生成輸入表達式的解析樹,但問題在於輸出E,T,F,S(代碼中使用的函數)。我希望它是這樣的:在c中生成分析樹
A + B * C => E * c => E + B * C => A + B * C
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
char next;
void E(void);void T(void);
void S(void);void F(void);
void error(int);void scan(void);
void enter(char);
void leave(char);
void spaces(int);
int level = 0;
//The main should always be very simple
//First scan the string
//second check for end of string reached , if yes success and if not error.
//P ---> E '#'
int main(void){
printf("Input:");
scan(); E();
if (next != '#') error(1);
else printf("***** Successful parse *****\n");
}
//E ---> T {('+'|'-') T}
void E(void){
enter('E');
T();
while (next == '+' || next == '-') {
scan();
T();
}
leave('E');
}
//T ---> S {('*'|'/') S}
void T(void)
{
enter('T'); S();
while (next == '*' || next == '/') {
scan(); S();
}
leave('T');
}
//S ---> F '^' S | F
void S(void)
{
enter('S'); F();
if (next == '^') {
scan(); S();
}
leave('S');
}
//F ---> char | '(' E ')'
void F(void)
{
enter('F');
if (isalpha(next))
{
scan();
}
else if (next == '(') {
scan(); E();
if (next == ')')
scan();
else
error(2);
}
else {
error(3);
}
leave('F');
}
//Scan the entire input
void scan(void){
while (isspace(next = getchar()));
}
void error(int n)
{
printf("\n*** ERROR: %i\n", n);
exit(1);
}
void enter(char name)
{
spaces(level++);
printf("+-%c\n", name);
}
void leave(char name)
{
spaces(--level);
printf("+-%c\n", name);
}
//TO display the parse tree
void spaces(int local_level)
{
while (local_level-- > 0)
printf("| ");
}
我完全困惑。你是什麼意思,「問題是輸出E,T,F,S」? (當我在同一個輸入上運行它時,我看不到任何類似輸出。)「a + b * c」與「E * c」等價嗎?你爲什麼最終會用同樣的表達方式?你想要的序列與程序生成輸出的方式有什麼關係(這似乎是正確的,順便說一句)? –
實際上a + b * c只是一個例子,並且認爲它是樹,所以'*'優先級高,它將首先評估,左邊部分將被視爲表達式,即'E',類似地a + b,當然你在樹中獲得與實際相同的表達式。 –
問題是這段代碼是以E,T,F,S的形式給出輸出,當它需要輸入表達式時,它應該給出同樣的表達式......:/我很困惑應該做什麼樣的改變要做到這一點... :) –