编写一个类c语言的语法分析程序,根据LL(1)分析表和文法编制类c语言的语法分析程序
时间: 2023-11-27 22:50:44 浏览: 100
为了编写类c语言的语法分析程序,我们需要先定义类c语言的文法。以下是一个简单的类c语言文法:
```
<program> ::= <declaration-list>
<declaration-list> ::= <declaration> | <declaration> <declaration-list>
<declaration> ::= <var-declaration> | <fun-declaration>
<var-declaration> ::= <type-specifier> <var> ;
<type-specifier> ::= int | float | char
<var> ::= id
<fun-declaration> ::= <type-specifier> <id> ( <params> ) <compound-stmt>
<params> ::= <param> | <param> , <params>
<param> ::= <type-specifier> <var>
<compound-stmt> ::= { <local-declarations> <statement-list> }
<local-declarations> ::= <local-declaration> | <local-declaration> <local-declarations>
<local-declaration> ::= <type-specifier> <var> ;
<statement-list> ::= <statement> | <statement> <statement-list>
<statement> ::= <expression-stmt> | <compound-stmt> | <selection-stmt> | <iteration-stmt> | <return-stmt>
<expression-stmt> ::= <expression> ;
<selection-stmt> ::= if ( <expression> ) <statement> | if ( <expression> ) <statement> else <statement>
<iteration-stmt> ::= while ( <expression> ) <statement>
<return-stmt> ::= return ; | return <expression> ;
<expression> ::= <var> = <expression> | <simple-expression>
<simple-expression> ::= <additive-expression> <relop> <additive-expression> | <additive-expression>
<additive-expression> ::= <term> <addop> <additive-expression> | <term>
<term> ::= <factor> <mulop> <term> | <factor>
<factor> ::= ( <expression> ) | <var> | <call> | num
<call> ::= <id> ( <args> )
<args> ::= <arg-list> | ε
<arg-list> ::= <expression> | <expression> , <arg-list>
<relop> ::= <= | < | > | >= | == | !=
<addop> ::= + | -
<mulop> ::= * | /
```
接下来,我们需要构建LL(1)分析表。由于篇幅限制,我无法在这里展示具体的LL(1)分析表的构建过程,但是你可以参考一些经典的编译原理教材,例如《编译原理(龙书)》或者《编译器设计》。
最后,我们可以根据LL(1)分析表和文法编写类c语言的语法分析程序。以下是一个简单的程序示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义LL(1)分析表
char *P[20][20] = {
{"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""},
{"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""},
{"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""},
{"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""},
{"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""},
{"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""},
{"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""},
{"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""},
{"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""},
{"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""},
{"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""},
{"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""},
{"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""},
{"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""},
{"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""},
{"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""},
{"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""},
{"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""},
{"S->E", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""},
{"", "E->T E'", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""},
{"", "", "E'->+ T E'", "E'->- T E'", "E'->ε", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""},
{"", "T->F T'", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""},
{"", "", "T'->* F T'", "T'->/ F T'", "T'->ε", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""},
{"", "F->( E )", "F->id", "F->num", "F->call", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""},
{"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""},
{"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""},
{"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""},
{"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""},
{"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""},
{"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""},
{"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""},
{"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""},
{"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""},
{"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""}
};
// 定义符号栈和输入栈
char symbol_stack[100][20];
char input_stack[100][20];
int sp = -1;
// 获取产生式
char* get_production(char* top_symbol, char* input_symbol) {
int row, col;
if (strcmp(top_symbol, "id") == 0) {
row = 21;
} else if (strcmp(top_symbol, "num") == 0) {
row = 22;
} else {
row = top_symbol[0] - 'A' + 1;
}
if (strcmp(input_symbol, "+") == 0) {
col = 2;
} else if (strcmp(input_symbol, "-") == 0) {
col = 3;
} else if (strcmp(input_symbol, "*") == 0) {
col = 4;
} else if (strcmp(input_symbol, "/") == 0) {
col = 5;
} else if (strcmp(input_symbol, "(") == 0) {
col = 6;
} else if (strcmp(input_symbol, ")") == 0) {
col = 7;
} else if (strcmp(input_symbol, "{") == 0) {
col = 8;
} else if (strcmp(input_symbol, "}") == 0) {
col = 9;
} else if (strcmp(input_symbol, ";") == 0) {
col = 10;
} else if (strcmp(input_symbol, "if") == 0) {
col = 11;
} else if (strcmp(input_symbol, "else") == 0) {
col = 12;
} else if (strcmp(input_symbol, "while") == 0) {
col = 13;
} else if (strcmp(input_symbol, "return") == 0) {
col = 14;
} else if (strcmp(input_symbol, "int") == 0) {
col = 15;
} else if (strcmp(input_symbol, "float") == 0) {
col = 16;
} else if (strcmp(input_symbol, "char") == 0) {
col = 17;
} else if (strcmp(input_symbol, "$") == 0) {
col = 18;
} else {
col = 1;
}
return P[row][col];
}
// 入栈
void push(char* stack[], char* symbol) {
stack[++sp] = symbol;
}
// 出栈
char* pop(char* stack[]) {
return stack[sp--];
}
// 匹配
int match(char* input_symbol, char* top_symbol) {
if (strcmp(input_symbol, top_symbol) == 0) {
return 1;
} else {
return 0;
}
}
// 分析程序
void analyze() {
int i = 0;
push(symbol_stack, "$");
push(symbol_stack, "S");
while (1) {
char* top_symbol = pop(symbol_stack);
char* input_symbol = input_stack[i];
if (match(input_symbol, top_symbol)) {
if (strcmp(top_symbol, "$") == 0) {
printf("语法分析成功!\n");
break;
}
i++;
} else {
char* production = get_production(top_symbol, input_symbol);
if (strcmp(production, "") == 0) {
printf("语法分析失败!\n");
break;
} else {
int j;
for (j = strlen(production) - 1; j >= 0; j--) {
if (production[j] != 'ε') {
push(symbol_stack, &production[j]);
}
}
}
}
}
}
int main() {
// 初始化输入栈
strcpy(input_stack[0], "int");
strcpy(input_stack[1], "main");
strcpy(input_stack[2], "(");
strcpy(input_stack[3], ")");
strcpy(input_stack[4], "{");
strcpy(input_stack[5], "int");
strcpy(input_stack[6], "a");
strcpy(input_stack[7], ";");
strcpy(input_stack[8], "a");
strcpy(input_stack[9], "=");
strcpy(input_stack[10], "10");
strcpy(input_stack[11], ";");
strcpy(input_stack[12], "}");
strcpy(input_stack[13], "$");
analyze();
return 0;
}
```
以上程序展示了如何根据LL(1)分析表和文法编写类c语言的语法分析程序。该程序针对一个简单的类c语言程序进行语法分析,如果分析成功,则输出“语法分析成功!”;如果分析失败,则输出“语法分析失败!”
阅读全文