yacc

Removing shift/reduce conflict on optional else block

佐手、 提交于 2020-02-08 07:21:36
问题 I'm in the process of defining a grammar with Bison and I stumbled upon a shift/reduce conflict I'd like to eliminate. The conflict is caused by a rule that aims to match if/else statements: state 17 13 Stmt: IfBlock . OptionalElseBlock ELSE shift, and go to state 42 ELSE [reduce using rule 16 (OptionalElseBlock)] $default reduce using rule 16 (OptionalElseBlock) OptionalElseBlock go to state 43 The OptionalElseBlock was defined as follows: 16 OptionalElseBlock: /* empty */ 17 | ELSE Stmt

Using yacc precedence for rules with no terminals, only non-terminals

你。 提交于 2020-01-26 04:06:48
问题 could some one help me on this i have this rule : e : T_NUM { $$ = mk_int($1);} | T_POP e[l] { $$ = mk_app(mk_op(POP),$l);} | T_NEXT e[l] { $$ = mk_app(mk_op(NEXT),$l);} | "{" e[x] "," e[y] "}" { $$ = mk_point($x,$y);} | e T_PLUS e { $$ = mk_app(mk_app(mk_op(PLUS),$1),$3);} | e T_MINUS e { $$ = mk_app(mk_app(mk_op(MINUS),$1),$3);} | e T_DIV e { $$ = mk_app(mk_app(mk_op(DIV),$1),$3);} | e T_MULT e { $$ = mk_app(mk_app(mk_op(MULT),$1),$3);} | e T_LEQ e { $$ = mk_app(mk_app(mk_op(LEQ),$1),$3) ;}

Yacc/Bison: The pseudo-variables ($$, $1, $2,..) and how to print them using printf

女生的网名这么多〃 提交于 2020-01-23 02:16:49
问题 I have a lexical analyser written in flex that passes tokens to my parser written in bison. The following is a small part of my lexer: ID [a-z][a-z0-9]* %% rule { printf("A rule: %s\n", yytext); return RULE; } {ID} { printf( "An identifier: %s\n", yytext ); return ID; } "(" return LEFT; ")" return RIGHT; There are other bits for parsing whitespace etc too. Then part of the parser looks like this: %{ #include <stdio.h> #include <stdlib.h> #define YYSTYPE char* %} %token ID RULE %token LEFT

LEX和YACC的使用三

我是研究僧i 提交于 2020-01-21 03:27:52
2.4.3 yacc解决二义性和冲突的方法 在2.3.8中已涉及到二义性和冲突的问题,这里再集中介绍一下,这在写Yacc源程序时会经常碰到。二义性会带来冲突。在2.3.8中我们介绍了yacc可以用为算符确定优先级和结合规则解决由二义性造成的冲突,但是有一些由二义性造成的冲突不易通过优先级方法解决, 如有名的例子: stat:IF bexp THEN stat |IF bexp THEN stat ELSE stat ; 对于这样的二义性造成的冲突和一些不是由二义性造成的冲突,Yacc提供了下面两条消除二义性的规则: A1.出现移进/归约冲突时,进行移进; A2. 出现归约/归约冲突时,按照产生式在yacc源程序中出现的次序,用先出现的产生式归约。 我们可以看出用这两条规则解决上面的IF语句二义性问题是合乎我们需要的。所以用户不必将上述文法改造成无二义性的。当Yacc用上述两条规则消除了二义性,它将给出相应信息。 下面再稍微严格地介绍一下Yacc如何利用优先级和结合性来解决冲突的。 Yacc源程序中的产生式也有一个优先级和结合性.这个优先级和结合性就是该产生式右部最后一个终结符或文字字符的优先级和结合性,当使用了%Prec子句时,该产生式的优先级和结合性由%Prec子句决定。当然如果产生式右部最后一个终结符或文字字符没有优先级或结合性,则该产生式也没有优先级或结合性。 根据终结符

Yacc/Bison, minimize amount by grouping math ops

落爺英雄遲暮 提交于 2020-01-20 08:16:21
问题 I am looking at the calc source here http://epaperpress.com/lexandyacc/ I see theses lines in calc.y | expr '+' expr { $$ = opr('+', 2, $1, $3); } | expr '-' expr { $$ = opr('-', 2, $1, $3); } | expr '*' expr { $$ = opr('*', 2, $1, $3); } | expr '/' expr { $$ = opr('/', 2, $1, $3); } | expr '<' expr { $$ = opr('<', 2, $1, $3); } | expr '>' expr { $$ = opr('>', 2, $1, $3); } Is there a way to group them? so i can write something like the below instead? | expr mathOp expr { $$ = opr(mathOp, 2,

Yacc/Bison, minimize amount by grouping math ops

北城余情 提交于 2020-01-20 08:14:26
问题 I am looking at the calc source here http://epaperpress.com/lexandyacc/ I see theses lines in calc.y | expr '+' expr { $$ = opr('+', 2, $1, $3); } | expr '-' expr { $$ = opr('-', 2, $1, $3); } | expr '*' expr { $$ = opr('*', 2, $1, $3); } | expr '/' expr { $$ = opr('/', 2, $1, $3); } | expr '<' expr { $$ = opr('<', 2, $1, $3); } | expr '>' expr { $$ = opr('>', 2, $1, $3); } Is there a way to group them? so i can write something like the below instead? | expr mathOp expr { $$ = opr(mathOp, 2,

yacc - How to do a if condition

你离开我真会死。 提交于 2020-01-16 02:04:06
问题 I am trying to do a simple if condition from an input file. i will have something like if(color = black) No matter what i do i keep getting 1 shift / reduce I am very new to lex and yacc Do YACC grammars often have shift-reduce conflicts? and should i not worry about them? My lex file will return every character in the file correctly so i wont show you the lex file However, here is my yacc file: %{ #include <ctype.h> #include <stdio.h> %} |IF LPAREN COLOR EQ BLACK RPAREN {$$ = $1; printf(

How Compiler distinguishes minus and negative number during parser process

亡梦爱人 提交于 2020-01-15 03:58:06
问题 Hey I'm recently involved in a compiler developer, I've encountered a problem with minus sign(-) and negative number(-1). Suppose now I have 5--3, 5+-3, how to write a grammar rule such that during the abstract syntax tree construction, yacc will produce a correct abstract syntax tree? My grammar is like: expr : constant {} | id {} | exec_expr {} exec_expr : expr PLUS expr {} | expr MINUS expr {} | expr MUL expr {} | expr DIV expr {} My thought for now is to have a UMINUS symbol with highest

Unclear how a yacc/bison production spec can cause a stack overflow

吃可爱长大的小学妹 提交于 2020-01-07 02:11:07
问题 This is not homework, but it is from a book. I'm given the following grammar: %{ #include <stdio.h> #include <ctype.h> int yylex(); int yyerror(); %} %% command : exp '\n' { printf("%d\n", $1); exit(0); } | error '\n' { yyerrok; printf("reenter expression: "); } command ; exp : exp '+' term { $$ = $1 + $3; } | exp '-' term { $$ = $1 - $3; } | term { $$ = $1; } ; term : term '*' factor { $$ = $1 * $3; } | factor { $$ = $1; } ; factor : NUMBER { $$ = $1; } | '(' exp ')' { $$ = $2; } ; %% int

no error while parsing empty file yacc/lex

核能气质少年 提交于 2020-01-06 02:58:06
问题 I have a parser with me generated from yacc/lex. It is working fine for all the rules I have set except one case. If file is empty which this parser is parsing it gives error. I want to add rule so that it does not give error when file is empty. I have not added any checks for that in either of my .l/.y file. How can this be done with YACC/LEX? Thanks in advance !! 回答1: The lexer should recognize the end of input and return a token accordingly (i.e. EOF ). Your grammar's start rule could look