编译原理预测分析实验c语言
时间: 2023-06-10 20:08:37 浏览: 62
预测分析器是一种自上而下的语法分析方法,它基于输入的文本生成一个语法树。在 C 语言中,可以使用自上而下的递归下降分析器来实现预测分析。
下面是一个简单的 C 语言程序:
```c
#include <stdio.h>
int main() {
int a, b, c;
printf("Enter two numbers to add\n");
scanf("%d%d", &a, &b);
c = a + b;
printf("Sum of the numbers = %d\n", c);
return 0;
}
```
我们可以使用 LL(1) 文法来解析这个程序,其中 LL(1) 表示左到右扫描,最左推导,具有一个符号的向前查看。以下是该文法的一部分:
```
<program> ::= <declaration_list> <statement_list>
<declaration_list> ::= <declaration> | <declaration> <declaration_list>
<declaration> ::= <type> <identifier_list> ;
<type> ::= int | float | char
<identifier_list> ::= <identifier> | <identifier> , <identifier_list>
<identifier> ::= <letter> | <identifier> <digit> | <identifier> <letter>
<letter> ::= a | b | ... | z | A | B | ... | Z
<digit> ::= 0 | 1 | ... | 9
<statement_list> ::= <statement> | <statement> <statement_list>
<statement> ::= <expression_statement> | <compound_statement> | <selection_statement> | <iteration_statement> | <jump_statement>
<expression_statement> ::= <expression> ;
<expression> ::= <assignment_expression> | <expression> , <assignment_expression>
<assignment_expression> ::= <logical_or_expression> | <unary_expression> = <assignment_expression>
<logical_or_expression> ::= <logical_and_expression> | <logical_or_expression> || <logical_and_expression>
<logical_and_expression> ::= <equality_expression> | <logical_and_expression> && <equality_expression>
<equality_expression> ::= <relational_expression> | <equality_expression> == <relational_expression> | <equality_expression> != <relational_expression>
<relational_expression> ::= <additive_expression> | <relational_expression> < <additive_expression> | <relational_expression> > <additive_expression> | <relational_expression> <= <additive_expression> | <relational_expression> >= <additive_expression>
<additive_expression> ::= <multiplicative_expression> | <additive_expression> + <multiplicative_expression> | <additive_expression> - <multiplicative_expression>
<multiplicative_expression> ::= <unary_expression> | <multiplicative_expression> * <unary_expression> | <multiplicative_expression> / <unary_expression>
<unary_expression> ::= <postfix_expression> | ++ <unary_expression> | -- <unary_expression> | <unary_operator> <unary_expression>
<postfix_expression> ::= <primary_expression> | <postfix_expression> [ <expression> ] | <postfix_expression> ( ) | <postfix_expression> ( <argument_expression_list> ) | <postfix_expression> . <identifier> | <postfix_expression> -> <identifier> | <postfix_expression> ++ | <postfix_expression> --
<primary_expression> ::= <identifier> | <constant> | ( <expression> )
<identifier> ::= <letter> | <identifier> <digit> | <identifier> <letter>
<constant> ::= <integer_constant> | <floating_constant> | <character_constant>
<integer_constant> ::= <digit> | <integer_constant> <digit>
<floating_constant> ::= <digit> . <digit> | <floating_constant> <digit> . <digit>
<character_constant> ::= ' <letter> ' | ' <digit> '
```
根据这个文法,我们可以编写一个递归下降分析器来解析 C 语言程序。以下是一个简单的实现:
```c
#include <stdio.h>
#include <ctype.h>
char lookahead;
void match(char c) {
if (lookahead == c) {
lookahead = getchar();
} else {
fprintf(stderr, "syntax error\n");
exit(1);
}
}
void program() {
declaration_list();
statement_list();
}
void declaration_list() {
declaration();
while (lookahead != ';') {
match(',');
declaration();
}
match(';');
}
void declaration() {
type();
identifier_list();
}
void type() {
if (lookahead == 'i') {
match('i');
match('n');
match('t');
} else if (lookahead == 'f') {
match('f');
match('l');
match('o');
match('a');
match('t');
} else if (lookahead == 'c') {
match('c');
match('h');
match('a');
match('r');
} else {
fprintf(stderr, "syntax error\n");
exit(1);
}
}
void identifier_list() {
identifier();
while (lookahead == ',') {
match(',');
identifier();
}
}
void identifier() {
if (isalpha(lookahead)) {
match(lookahead);
} else {
fprintf(stderr, "syntax error\n");
exit(1);
}
}
void statement_list() {
statement();
while (lookahead != EOF) {
statement();
}
}
void statement() {
expression_statement();
compound_statement();
selection_statement();
iteration_statement();
jump_statement();
}
void expression_statement() {
expression();
match(';');
}
void expression() {
assignment_expression();
}
void assignment_expression() {
logical_or_expression();
if (lookahead == '=') {
match('=');
assignment_expression();
}
}
void logical_or_expression() {
logical_and_expression();
while (lookahead == '|') {
match('|');
logical_and_expression();
}
}
void logical_and_expression() {
equality_expression();
while (lookahead == '&') {
match('&');
equality_expression();
}
}
void equality_expression() {
relational_expression();
while (lookahead == '=' || lookahead == '!') {
match(lookahead);
relational_expression();
}
}
void relational_expression() {
additive_expression();
while (lookahead == '<' || lookahead == '>') {
match(lookahead);
additive_expression();
}
}
void additive_expression() {
multiplicative_expression();
while (lookahead == '+' || lookahead == '-') {
match(lookahead);
multiplicative_expression();
}
}
void multiplicative_expression() {
unary_expression();
while (lookahead == '*' || lookahead == '/') {
match(lookahead);
unary_expression();
}
}
void unary_expression() {
postfix_expression();
if (lookahead == '+' || lookahead == '-') {
match(lookahead);
unary_expression();
} else if (lookahead == '!') {
match('!');
unary_expression();
}
}
void postfix_expression() {
primary_expression();
if (lookahead == '[') {
match('[');
expression();
match(']');
} else if (lookahead == '(') {
match('(');
match(')');
} else if (lookahead == '.') {
match('.');
identifier();
} else if (lookahead == '-') {
match('-');
match('>');
identifier();
} else if (lookahead == '+') {
match('+');
match('+');
} else if (lookahead == '-') {
match('-');
match('-');
}
}
void primary_expression() {
if (isalpha(lookahead)) {
identifier();
} else if (isdigit(lookahead)) {
constant();
} else if (lookahead == '(') {
match('(');
expression();
match(')');
} else {
fprintf(stderr, "syntax error\n");
exit(1);
}
}
void constant() {
if (isdigit(lookahead)) {
match(lookahead);
} else {
fprintf(stderr, "syntax error\n");
exit(1);
}
}
int main() {
lookahead = getchar();
program();
return 0;
}
```
这个程序使用 getchar() 函数从标准输入读取一个字符,并使用递归下降分析方法来解析 C 语言程序。它可以识别程序中的声明、语句、表达式和常量,并在语法错误时输出错误信息。