我构建了一个扫描程序 - >解析器,用于捕获已修改的Java。在测试它时,我注意到codeBlock从不触发但是varDecls触发。我不完全确定为什么会这样。
这是我的解析器
%{
#include <stdio.h>
extern int yylex(void);
void yyerror(char *s) {
fprintf(stderr, "error: %s\n", s);
}
int yywrap(){ return 1;}
%}
%token INT FLOAT STRING
%union {
int ival;
float fval;
char *sval;
int bval;
}
%token <ival> NegInt
%token <ival> Int
%token <fval> Float
%token <bval> Bool
%token <sval> ifHeader
%token <sval> thenHeader
%token <sval> elseHeader
%token <sval> forHeader
%token <sval> whileHeader
%token <sval> ID
%token <sval> BinOperator
%token <sval> BoolOperator
%token <sval> Assignment
%token <sval> Quotation
%token <sval> LBracket
%token <sval> RBracket
%token <sval> LFBracket
%token <sval> RFBracket
%token <sval> Semi
%token <sval> LABracket
%token <sval> RABracket
%token <sval> Comma
%token <sval> String
%%
codeBlock: varDecls {printf("why doth this triggering?\n");}
| ifExprs {printf("codeBlock Statement \n");}
;
ifExprs: ifExprs ifStmt
| ifStmt
;
ifStmt: ifExpr | ifExprElse;
ifExprElse: ifExpr elseExpr;
ifExpr: ifHeader LBracket boolExpr RBracket thenExpr;
thenExpr: thenHeader LFBracket varDecls RFBracket;
elseExpr: elseHeader LFBracket varDecls RFBracket;
varDecls: varDecls varDecl
| varDecl
;
varDecl: ID Assignment numStmt Semi
| ID Assignment strExpr Semi
| ID Assignment boolExpr Semi
| ID Assignment Bool Semi {printf("why is this triggering?\n");}
;
boolExpr: ID BoolOperator ID
| ID BoolOperator numExpr
| ID BoolOperator Bool
;
strExpr: Quotation ID Quotation
;
numStmt: numStmt BinOperator numExpr
| numExpr
;
numExpr: LBracket numStmt RBracket
| Int
| Float
| NegInt
;
%%
int main(int argc, char* argv[]) {
yyparse();
}
这是我的扫描仪:
%{
#include "y.tab.h"
extern YYSTYPE yylval;
%}
%option yylineno
Digit [0-9]
Letter [a-zA-Z]
Word [a-z][a-zA-Z0-9_]*
%%
"-"{Digit}+ {
//printf("\n An assignment: %s \n", yytext);
yylval.ival = atoi(yytext);
return NegInt;
}
{Digit}+ {
//printf("\n An assignment: %s \n", yytext);
yylval.ival = atoi(yytext);
return Int;
}
{Digit}+"."{Digit}+ {
//printf("\n A float: %s (%f)\n", yytext, atof(yytext));
yylval.ival=atof(yytext);
return Float;
}
True|False {
//printf("\n A Boolean: %s \n", yytext);
yylval.bval = atoi(yytext);
return Bool;
}
if {
//printf("\n An assignment: %s \n", yytext);
yylval.sval = yytext;
return ifHeader;
}
then {
//printf("\n An assignment: %s \n", yytext);
yylval.sval = yytext;
return thenHeader;
}
else {
//printf("\n An assignment: %s \n", yytext);
yylval.sval = yytext;
return elseHeader;
}
for {
//printf("\n An assignment: %s \n", yytext);
yylval.sval = yytext;
return forHeader;
}
while {
//printf("\n An assignment: %s \n", yytext);
yylval.sval = yytext;
return whileHeader;
}
{Word} {
//printf("\n An Identifier: %s \n", yytext);
yylval.sval = yytext;
return ID;
}
"'" {
//printf("\n An Identifier: %s \n", yytext);
yylval.sval = yytext;
return Quotation;
}
"+"|"-"|"*"|"/" {
//printf("\n An Operator: %s \n", yytext);
yylval.sval = yytext;
return BinOperator;
}
"<"|">"|"!="|"<="|">="|"==" {
//printf("\n An comparison: %s \n", yytext);
yylval.sval = yytext;
return BoolOperator;
}
"=" {
//printf("\n An assignment: %s \n", yytext);
yylval.sval = yytext;
return Assignment;
}
"(" {
//printf("\n An assignment: %s \n", yytext);
yylval.sval = yytext;
return LBracket;
}
")" {
//printf("\n An assignment: %s \n", yytext);
yylval.sval = yytext;
return RBracket;
}
"{" {
//printf("\n An assignment: %s \n", yytext);
yylval.sval = yytext;
return LFBracket;
}
"}" {
//printf("\n An assignment: %s \n", yytext);
yylval.sval = yytext;
return RFBracket;
}
";" {
//printf("\n An assignment: %s \n", yytext);
yylval.sval = yytext;
return Semi;
}
"[" {
//printf("\n An assignment: %s \n", yytext);
yylval.sval = yytext;
return LABracket;
}
"]" {
//printf("\n An assignment: %s \n", yytext);
yylval.sval = yytext;
return RABracket;
}
"," {
//printf("\n An assignment: %s \n", yytext);
yylval.sval = yytext;
return Comma;
}
%
正如您在我的print语句中看到的那样,我试图触发我的解析器打印出“为什么会触发这个”,而是触发varDecls。
此外,varDecl也会触发。这应该发生吗?
非常感谢帮助。谢谢