基于flex/bison对SysY语⾔进行语法分析 代码
时间: 2023-07-28 19:06:58 浏览: 377
基于C语言使用flex和bison对Cminus进行词法分析和语法分析(编译原理大作业)【100012265】
5星 · 资源好评率100%
下面是一个基于Flex/Bison对SysY语言进行词法分析和语法分析的示例代码:
Flex文件(sysy.l):
```c
%{
#include "sysy.tab.h"
%}
%%
"int" { return INT; }
"if" { return IF; }
"else" { return ELSE; }
"while" { return WHILE; }
"return" { return RETURN; }
"or" { return OR_OP; }
"and" { return AND_OP; }
"==" { return EQ_OP; }
"!=" { return NE_OP; }
"<=" { return LE_OP; }
">=" { return GE_OP; }
"+" { return '+'; }
"-" { return '-'; }
"*" { return '*'; }
"/" { return '/'; }
"%" { return '%'; }
"!" { return '!'; }
"=" { return '='; }
"<" { return '<'; }
">" { return '>';}
";" { return ';'; }
"," { return ','; }
"(" { return '('; }
")" { return ')'; }
"{" { return '{'; }
"}" { return '}'; }
[0-9]+ { yylval.num = atoi(yytext); return NUM; }
[a-zA-Z][a-zA-Z0-9]* { yylval.id = strdup(yytext); return ID; }
[ \t\n] { /* ignore whitespace */ }
. { fprintf(stderr, "Unknown character: %s\n", yytext); }
%%
int yywrap() {
return 1;
}
```
Bison文件(sysy.y):
```c
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define YYSTYPE_IS_DECLARED
#define YYSTYPE int
#include "sysy.tab.h"
extern int yylex();
extern int yylineno;
extern char* yytext;
void yyerror(const char *msg);
%}
%token INT ID IF ELSE WHILE RETURN
%token <num> NUM
%left OR_OP
%left AND_OP
%left EQ_OP NE_OP
%left '<' '>' LE_OP GE_OP
%left '+' '-'
%left '*' '/' '%'
%right '!'
%right '='
%left UMINUS
%%
program:
function_list
;
function_list:
function
| function_list function
;
function:
type_specifier ID '(' parameter_list ')' compound_stmt
;
type_specifier:
INT
;
parameter_list:
parameter_decl
| parameter_list ',' parameter_decl
;
parameter_decl:
type_specifier ID
;
compound_stmt:
'{' local_declarations statement_list '}'
;
local_declarations:
/* empty */
| local_declarations var_declaration
;
var_declaration:
type_specifier ID ';'
;
statement_list:
/* empty */
| statement_list statement
;
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:
assignment_expression
;
assignment_expression:
logical_or_expression
| unary_expression '=' assignment_expression
;
logical_or_expression:
logical_and_expression
| logical_or_expression OR_OP logical_and_expression
;
logical_and_expression:
equality_expression
| logical_and_expression AND_OP equality_expression
;
equality_expression:
relational_expression
| equality_expression EQ_OP relational_expression
| equality_expression NE_OP relational_expression
;
relational_expression:
additive_expression
| relational_expression '<' additive_expression
| relational_expression '>' additive_expression
| relational_expression LE_OP additive_expression
| relational_expression GE_OP 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
| multiplicative_expression '%' unary_expression
;
unary_expression:
postfix_expression
| '-' unary_expression %prec UMINUS
| '!' unary_expression
;
postfix_expression:
primary_expression
| ID '(' argument_list ')'
;
primary_expression:
ID
| NUM
| '(' expression ')'
;
argument_list:
/* empty */
| expression
| argument_list ',' expression
;
%%
int main(int argc, char **argv) {
yyparse();
return 0;
}
void yyerror(const char *msg) {
fprintf(stderr, "Syntax error at line %d: %s\n", yylineno, msg);
exit(1);
}
```
在main函数中调用yyparse函数,就可以启动语法分析器进行语法分析。当语法分析器遇到错误时,会通过yyerror函数输出错误信息并退出程序。
阅读全文