繁体   English   中英

解析器的左联想语法

[英]Left associative grammar for parser

关于使用C语言编写的解析器,我有一个问题。它假设使用left-recursion(为运算符提供left关联性),然后以后缀表示法返回该表达式。 专门针对我的Expr和Term规则。 目前,我遇到了一个问题,我认为这没有发生。 这是我的语法:

extern int yylex(); /* The next token function. */
extern char *yytext; /* The matched token text. */
extern int yyleng; /* The token text length. */
void yyerror(char *s);
#define YYSTYPE long /* 64 bit so can hold pointer and int */
%}

// These token definitions populate y.tab.h
// Single character tokens are their own token numbers (e.g. scanner returns
// the value ';' for the semicolon token)
%token INT_TOK 1
%token CHR_TOK 2
%token ASSIGN_TOK 3
%token INTLIT_TOK 4
%token IDENT_TOK 5

%%
Prog    : IDENT_TOK '{' StmtSeq '}'    { $$ = (long) strdup(yytext); }  ;
StmtSeq : Stmt ';' StmtSeq                                                        ;
StmtSeq :                                                                         ;

Assign  : LHS ASSIGN_TOK Expr         { printf("%s =\n",(char *)$1); } ;
LHS     : IDENT_TOK                   { $$ = (long) strdup(yytext);  } ;

Stmt    : Decl;
Stmt    : Assign;
Decl    : Type IDLst;
Type    : INT_TOK;
Type    : CHR_TOK;
IDLst   : IDENT_TOK MLst;
MLst    : ',' IDLst;
MLst    : ;
Expr    : Term MExpr;
MExpr   : AddOp Term MExpr            { printf("%s ",(char *)$1); } ;
MExpr   : ;
Term    : Factor MTerm;
MTerm   : MultOp Factor MTerm         { printf("%s ",(char *)$1); } ;
MTerm   : ;
Factor  :  '(' Expr ')';
Factor  :  '-' Factor;
Factor  : INTLIT_TOK                  { printf("%s ",yytext); }
Factor  : IDENT_TOK                   { printf("%s ",yytext); }
AddOp   : '-'                                               { $$ = (long) strdup(yytext); }  ;
AddOp   : '+'                                               { $$ = (long) strdup(yytext); }  ;
MultOp  : '*'                                               { $$ = (long) strdup(yytext); }  ;
MultOp  : '/'                                               { $$ = (long) strdup(yytext); }  ;
%%

我正在使用的测试文件是这样的:

test1 {
  int amt, box;
  int x, y, w;
  x := 4 - 2 - 1;             // 4 2 - 1 - x =
  amt := 5 * y - 2;           // 5 y * 2 - amt =
  x := 5 * (y - 2);           // 5 y 2 - * x =
  box := 5 * x / amt + 3 * 4; // 5 x * amt / 3 4 * + box =
  y := z; w:= 1;              // z y =    1 w =
  }

带注释的表达式表示我应该得到的输出。 因此我的语法应该回来,

  1. x := 4 - 2 - 1; should produce 4 2 - 1 - x =
  2. amt := 5 * y - 2; should produce 5 y * 2 - amt =
  3. x := 5 * (y - 2); should produce 5 y 2 - * x =
  4.box := 5 * x / amt + 3 * 4; should produce 5 x * amt / 3 4 * + box =
  5. y := z; w:= 1;   should produce z y =    1 w =

我的语法回来了

1. 4 2 1 - - x =
2. I get the correct output
3. I get the correct output
4. I get the correct output
5. 5 x amt / * 3 4 * + box =

从我的理解来看,我的操作员似乎没有任何联系。 有人知道为什么会这样吗?

左递归生产是其中生产的非终端是右侧的第一个(最左侧)符号的生产。 例如,

MTerm: MTerm MultOp Factor

右递归产生是指产生的非终结符是右侧的最后一个(最右边)符号。 例如,

MTerm: MultOp Factor MTerm

您的语法没有左递归规则,而有许多右递归规则。 因此,它不会产生左联想性就不足为奇了。

该语法似乎是尝试删除左递归以生成LL语法的结果,尽管您似乎正在使用yacc / bison,并且该分配假设可以进行左递归,但两者都表明您需要LR语法。 LR语法不需要左因子分解或左递归消除。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM