LexYacc错误,谁能帮我看看?
今天第一次用LexYacc写了一个简单的parser,当链入VC++工程(6.0)时出现了如下错误,希望大伙能帮我看看,实在是没招了:
Build : warning : failed to (or don't know how to) build 'E:\程序\MyProjects\modal logic\ModalReader_vc\myparse.c'
Compiling...
《mylexer.c
.\mylexer.l(18) : error C2065: 'yylval' : undeclared identifier
.\mylexer.l(18) : error C2065: 'new' : undeclared identifier
.\mylexer.l(18) : error C2146: syntax error : missing ';' before identifier 'GFormula'
.\mylexer.l(18) : error C2065: 'GFormula' : undeclared identifier
.\mylexer.l(19) : error C2223: left of '->kind' must point to struct/union
.\mylexer.l(20) : error C2223: left of '->atom' must point to struct/union
.\mylexer.l(36) : warning C4028: formal parameter 1 different from declaration
Error executing cl.exe.
ModalReader_vc.exe - 6 error(s), 2 warning(s)
下面是原代码:
-----------------
《MainAPP.cpp》
#include "myparser.h"
#include "GeneralizedFormula.h"
GFormula *ggform;
void main()
{
}
----------------
《GeneralizedFormula.h》
#ifndef _GFormula_h_
#define _GFormula_h_
struct GFormula
{
int kind;
struct GFormula *part1;
struct GFormula *part2;
int atom;
};
#endif
---------------
《myparser.y》
%token ATOM
%token BOX
%token DIAMOND
%left BOX DIAMOND
%left '>'
%left '='
%left '|'
%left '&'
%nonassoc '!'
%{
#include "GeneralizedFormula.h"
#include "mylexer.h"
extern GFormula * ggform;
%}
// attribute type
%include {
#ifndef YYSTYPE
#define YYSTYPE GFormula *
#endif
}
%%
FILE:
FORM { ggform = $1; }
;
FORM:
ATOM
|'!' FORM { GFormula * f = new GFormula;
f->kind = -1;
f->part1 = $2;
$$ = f; }
|BOX FORM { GFormula * f = new GFormula;
f->kind = 3;
f->part1 = $2;
$$ = f; }
|DIAMOND FORM { GFormula * f = new GFormula;
f->kind = 4;
f->part1 = $2 ;
$$ = f; }
|'(' FORM ')' { $$ = $2; }
|FORM '|' FORM { GFormula * f = new GFormula;
f->kind = 2;
f->part1 = $1;
f->part2 = $3;
$$ = f; }
|FORM '&' FORM { GFormula * f = new GFormula;
f->kind = 1;
f->part1 = $1;
f->part2 = $3;
$$ = f; }
|FORM '>' FORM { GFormula * f = new GFormula;
f->kind = 5;
f->part1 = $1;
f->part2 = $3;
$$ = f; }
|FORM '=' FORM { GFormula * f = new GFormula;
f->kind = 6;
f->part1 = $1;
f->part2 = $3;
$$ = f; }
;
%%
--------------
《mylexer.l》
%{
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "GeneralizedFormula.h"
#include "myparser.h"
void yyerror( const char * );
int string2int( char * );
char global_atom[10000][100];
int max_pos;
int i;
%}
%%
[a-zA-Z][a-zA-Z0-9]* { yylval = new GFormula;
yylval->kind = 0;
yylval->atom = string2int( yytext ) + 1;
return ATOM; }
"<>" { return DIAMOND; }
"[]" { return BOX; }
[|&!()] { return *yytext; }
"->" { return '>'; }
"<->" { return '='; }
. { yyerror( "INVALID CHARACTER !! \n" ); }
%%
void yyerror( const char * str )
{
printf( "%s", str );
}
int string2int( const char * string )
{
strcpy( global_atom[max_pos], string );
for ( i = 0; i < max_pos; i++ ) {
if ( strcmp( global_atom[i], string ) == 0 ) return i;
}
strcpy( global_atom[max_pos], string );
max_pos++;
}