使用lex和yacc编写一个C语言的语法分析器
时间: 2023-12-12 12:03:50 浏览: 154
基于lex和yacc的词法分析器+语法分析器(C语言编译器)【100012624】
5星 · 资源好评率100%
1. 创建lex文件,命名为c_lex.l,输入以下内容:
%{
#include "c_yacc.tab.h"
%}
%%
"int" { return INT; }
"float" { return FLOAT; }
"char" { return CHAR; }
"if" { return IF; }
"else" { return ELSE; }
"while" { return WHILE; }
"for" { return FOR; }
"return" { return RETURN; }
[0-9]+ { yylval.num = atoi(yytext); return NUM; }
[a-zA-Z_][a-zA-Z0-9_]* { yylval.id = strdup(yytext); return ID; }
";" { return SEMICOLON; }
"," { return COMMA; }
"(" { return LEFTPAREN; }
")" { return RIGHTPAREN; }
"{" { return LEFTBRACE; }
"}" { return RIGHTBRACE; }
"==" { return EQ; }
">" { return GT; }
">=" { return GE; }
"<" { return LT; }
"<=" { return LE; }
"!=" { return NE; }
"=" { return ASSIGN; }
"//" { while(yyinput() != '\n'); }
"/*" { while(yyinput() != '*' || yyinput() != '/') yyinput(); }
[ \t\n] { }
. { printf("Error: unknown character %c\n", *yytext); }
%%
int yyinput(void)
{
return fgetc(yyin);
}
2. 创建yacc文件,命名为c_yacc.y,输入以下内容:
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int yylex();
void yyerror(char *error);
int yyparse();
extern FILE *yyin;
extern int yylineno;
extern char *yytext;
%}
%token INT FLOAT CHAR IF ELSE WHILE FOR RETURN
%token NUM ID
%token SEMICOLON COMMA LEFTPAREN RIGHTPAREN LEFTBRACE RIGHTBRACE
%token EQ GT GE LT LE NE ASSIGN
%start program
%%
program: statement
;
statement: declaration
| assignment
| if_statement
| while_statement
| for_statement
| return_statement
| SEMICOLON
;
declaration: type ID SEMICOLON
;
assignment: ID ASSIGN expression SEMICOLON
;
if_statement: IF LEFTPAREN expression RIGHTPAREN LEFTBRACE statement RIGHTBRACE
| IF LEFTPAREN expression RIGHTPAREN LEFTBRACE statement RIGHTBRACE ELSE LEFTBRACE statement RIGHTBRACE
;
while_statement: WHILE LEFTPAREN expression RIGHTPAREN LEFTBRACE statement RIGHTBRACE
;
for_statement: FOR LEFTPAREN expression SEMICOLON expression SEMICOLON expression RIGHTPAREN LEFTBRACE statement RIGHTBRACE
;
return_statement: RETURN expression SEMICOLON
;
type: INT
| FLOAT
| CHAR
;
expression: expression EQ expression
| expression GT expression
| expression GE expression
| expression LT expression
| expression LE expression
| expression NE expression
| expression '+' expression
| expression '-' expression
| expression '*' expression
| expression '/' expression
| LEFTPAREN expression RIGHTPAREN
| ID
| NUM
;
%%
void yyerror(char *error)
{
printf("Error: %s at line %d\n", error, yylineno);
}
int main(int argc, char *argv[])
{
if(argc < 2)
{
printf("Usage: %s <filename>\n", argv[0]);
return 1;
}
FILE *fp = fopen(argv[1], "r");
if(!fp)
{
printf("Error: cannot open file %s\n", argv[1]);
return 1;
}
yyin = fp;
yyparse();
fclose(fp);
return 0;
}
3. 编译生成可执行文件,输入以下命令:
lex c_lex.l
yacc -d c_yacc.y
gcc lex.yy.c y.tab.c -o c_parser
4. 执行可执行文件,并输入要分析的C语言代码文件名,例如:
./c_parser test.c
其中test.c为要分析的C语言代码文件。
阅读全文