/*lex.l*/
%{
#include "y.tab.h"
%}
%%
"typedef" { return TYPEDEF; }
"struct" { return STRUCT; }
"char" { return CHAR; }
"short" { return SHORT; }
"int" { return INT; }
"float" { return FLOAT; }
"double" { return DOUBLE; }
[a-zA-Z_][a-zA-Z0-9_]* { yylval.str = strdup(yytext); return IDENTIFIER; }
";" { return SEMICOLON; }
"{" { return LBRACE; }
"}" { return RBRACE; }
"[" { return LBRACKET; }
"]" { return RBRACKET; }
[ \t\n] {}
. {}
%%
/*yacc.y*/
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "lex.yy.c"
extern int yylex();
extern FILE* yyin;
void yyerror(const char *s);
char* typeDefName = NULL;
int yywrap() {
return 1;
}
%}
%union {
char* str;
}
%token TYPEDEF STRUCT CHAR SHORT INT FLOAT DOUBLE IDENTIFIER SEMICOLON LBRACE RBRACE LBRACKET RBRACKET
%type <str> structDeclaration fieldDeclaration
%%
program:
structDeclaration {
printf("public class %s {\n", $1);
}
|
;
struct_declaration:
TYPEDEF STRUCT IDENTIFIER '\n' LBRACE field_declaration RBRACE IDENTIFIER SEMICOLON {
printf("}\n");)
}
|
;
field_declaration:
INT IDENTIFIER SEMICOLON {
printf(" private int %s;\n", $2);
}
| CHAR IDENTIFIER LBRACKET INT RBRACKET SEMICOLON {
printf(" private String %s;\n", $2);
}
| SHORT IDENTIFIER SEMICOLON {
printf(" private short %s;\n", $2);
}
| FLOAT IDENTIFIER SEMICOLON {
printf(" private float %s;\n", $2);
}
| DOUBLE IDENTIFIER SEMICOLON {
printf(" private double %s;\n", $2);
}
;
%%
void yyerror(const char *s) {
fprintf(stderr, "Error: %s\n", s);
}
int main(int argc, char *argv[]) {
if (argc != 2) {
fprintf(stderr, "Usage: %s <input_file>\n", argv[0]);
return 1;
}
yyin = fopen(argv[1], "r");
if (!yyin) {
perror("fopen");
return 1;
}
yyparse();
fclose(yyin);
free(typeDefName);
return 0;
}
在执行的时候,lex文件可以执行,但是yacc yac.y时会报错, $2 (IDENTIFIER) is untyped, 这里卡住了,不明白什么意思、希望各位佬解答