flex1.l文件:
%{
#include "bison1.tab.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int n=1;
%}
digit [0-9]
letter [a-zA-Z]
id {letter}({letter}|{digit})*
%%
";" {printf("semicolon\n"); return SEMICOLON;}
"=" {printf("assigh\n"); return ASSIGN;}
"+" {printf("plus\n"); return PLUS;}
"-" {printf("minus\n"); return MINUS;}
"(" {printf("parenthesi1\n"); return PARENTHESI1;}
")" {printf("parenthesi2\n"); return PARENTHESI2;}
"*" {printf("pollaplasiasmos\n"); return POLLAPL;}
"/" {printf("diairesi\n"); return DIAIRESI;}
"%" {printf("mod\n"); return MOD;}
"==" {printf("isotita\n"); return ISOTITA;}
"!=" {printf("diafora\n"); return DIAFORA;}
"<" {printf("lessthan\n"); return LESSTHAN;}
">" {printf("greaterthan\n"); return GREATERTHAN;}
"<=" {printf("lessiso\n"); return LESSISO;}
">=" {printf("greateriso\n"); return GREATERISO;}
"!" {printf("thaumastiko\n"); return THAUMASTIKO;}
"&" {printf("and1\n"); return AND1;}
"&&" {printf("and2\n"); return AND2;}
"||" {printf("or\n"); return OR;}
"," {printf("komma\n"); return KOMMA;}
"{" {printf("aggili1\n"); return AGGILI1;}
"}" {printf("aggili2\n"); return AGGILI2;}
"\n" {n++; return NEWLINE;}
return {printf("return\n"); return RETURN1;}
true {printf("true\n"); return TRUE;}
false {printf("false\n"); return FALSE;}
extern {printf("extern\n"); return EXTERN;}
void {printf("void\n"); return VOID;}
integer {printf("integer\n"); return INTEGER;}
boolean {printf("boolean\n"); return BOOLEAN;}
string {printf("strign\n"); return STRING;}
begin {printf("begin\n"); return BEGIN1;}
end {printf("end\n"); return END1;}
if {printf("if\n"); return IF1;}
else {printf("else\n"); return ELSE1;}
while {printf("while\n"); return WHILE1;}
for {printf("for\n"); return FOR1;}
{id} {printf("id\n"); return ID1;}
{digit}+ {printf("digit\n"); return DIGIT;}
[a-zA-Z0-9]+ {return STRING1;}
%%
bison1.y档案:
%{
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "bison1.tab.h"
void yyerror(char const *);
extern FILE *yyin;
extern FILE *input;
extern FILE *output;
extern int yylval;
extern int yyparse(void);
extern int n;
int errNum = 0;
int forNum = 0;
%}
%left '+' '-'
%left '*' '/' '%'
%nonassoc ISOTITA DIAFORA LESSTHAN GREATERTHAN LESSISO GREATERISO
%token SEMICOLON ASSIGN THAUMASTIKO AND1 KOMMA
%token PLUS MINUS
%token PARENTHESI1 PARENTHESI2 AGGILI1 AGGILI2
%token POLLAPL DIAIRESI MOD
%token ISOTITA DIAFORA LESSTHAN GREATERTHAN LESSISO GREATERISO
%token AND2 OR
%token TRUE FALSE
%token INTEGER BOOLEAN STRING VOID EXTERN
%token BEGIN1 END1 IF1 ELSE1 WHILE1 FOR1 RETURN1
%token ID1 DIGIT STRING1
%token NEWLINE
%start program
%%
program : externaldecl kefalidaprog tmimaorismon tmimaentolon
;
externaldecl : externaldecl externalprototype
|
;
externalprototype : EXTERN prototypefun
;
kefalidaprog : VOID ID1 PARENTHESI1 PARENTHESI2
;
tmimaorismon : tmimaorismon orismos
|
;
orismos : orismosvar
| orismosfun
| prototypefun
;
orismosvar : typedata listvar SEMICOLON
;
typedata : INTEGER
| BOOLEAN
| STRING
;
listvar : extralist ID1
|
;
extralist : KOMMA listvar
|
;
orismosfun : kefalidafun tmimaorismon tmimaentolon
;
prototypefun : kefalidafun SEMICOLON
;
kefalidafun : typefun ID1 PARENTHESI1 listtyppar PARENTHESI2
| typefun ID1 PARENTHESI1 PARENTHESI2
;
typefun : INTEGER
| BOOLEAN
| STRING
;
listtyppar : otherlistpar typpar
|
;
otherlistpar : listtyppar KOMMA
|
;
typpar : typedata AND1 ID1
| typedata ID1
;
tmimaentolon : BEGIN1 entoles END1
;
entoles : entoles entoli
|
;
entoli : simpleentoli SEMICOLON
| domimenientoli
| sinthetientoli
;
sinthetientoli : AGGILI1 entoles AGGILI2
;
domimenientoli : entoliif
| entoliwhile
| entolifor
;
simpleentoli : anathesi
| klisifun
| entolireturn
| entolinull
;
entoliif : IF1 PARENTHESI1 genikiekfrasi PARENTHESI2 entoli elseclause
| IF1 PARENTHESI1 genikiekfrasi PARENTHESI2 entoli
;
elseclause : ELSE1 entoli
;
entoliwhile : WHILE1 PARENTHESI1 genikiekfrasi PARENTHESI2 sinthetientoli
;
entolifor : FOR1 PARENTHESI1 conditions PARENTHESI2 sinthetientoli
;
conditions : condition SEMICOLON condition SEMICOLON condition SEMICOLON
;
condition : genikiekfrasi
|
;
anathesi : ID1 ASSIGN genikiekfrasi
;
klisifun : ID1 PARENTHESI1 listrealpar PARENTHESI2
| ID1 PARENTHESI2 PARENTHESI2
;
listrealpar : realpar otherlistreal
;
otherlistreal : KOMMA realpar otherlistreal
|
;
realpar : genikiekfrasi
;
entolireturn : RETURN1 genikiekfrasi
;
entolinull :
;
genikiekfrasi : genikaterms genikosoros
;
genikaterms : genikiekfrasi OR
|
;
genikosoros : genikosparags genikosparag
;
genikosparags : genikosoros AND2
|
;
genikosparag : THAUMASTIKO genikosprotparag
| genikosprotparag
;
genikosprotparag : simpleekfrasi tmimasigrisis
| simpleekfrasi
;
tmimasigrisis : sigritikostelestis simpleekfrasi
;
sigritikostelestis : ISOTITA
| DIAFORA
| LESSTHAN
| GREATERTHAN
| LESSISO
| GREATERISO
;
simpleekfrasi : simpleoros ekfrasi
;
ekfrasi : PLUS simpleekfrasi
| MINUS simpleekfrasi
|
;
simpleoros : muloroi simpleparag
;
muloroi : simpleoros POLLAPL
| simpleoros DIAIRESI
| simpleoros MOD
|
;
simpleparag : PLUS simpleprotoros
| MINUS simpleprotoros
| simpleprotoros
;
simpleprotoros : ID1
| stathera
| klisifun
| PARENTHESI1 genikiekfrasi PARENTHESI2
;
stathera : DIGIT
| STRING1
| TRUE
| FALSE
;
%%
void yyerror(char const *msg)
{
fprint(stderr, "%s\n", msg);
}
int main(int argc, char **argv)
{
FILE *input;
FILE *output;
++argv;
--argc;
if(argc>0)
{input = fopen(argv[0], "r");}
else
{output = fopen("output.exe","w");}
int a = yyparse();
if(a==0)
{printf("Done parsing\n");}
else
{printf("Yparxei lathos sti grammi: %d\n", n);}
printf("Estimated number of errors: %d\n", errNum);
return 0;
}
我编译了.y
文件:
bison -d bison1.y
我编译了.l
文件:
flex flex1.l
我跑了:
gcc bison1.tab.c lex.yy.c -lfl
我收到以下消息:
/tmp/ccPPnaPa.o:bison1.tab.c :(。text + 0x93f):未定义的引用 'fprint'/tmp/ccPPnaPa.o:bison1.tab.c:(.text+0x93f):搬迁 截断以适合:R_X86_64_PC32对未定义的符号'fprint' collect2:
错误:ld返回1退出状态