yacc

Grammar construction in ply - recursion allowed?

▼魔方 西西 提交于 2019-12-11 19:51:31
问题 This has maybe been asked before but I do not know what to search for, really. Suppose I have some string I'd like to build a parser with. I have strings like a OR b , b OR C but also a OR (b AND c) . Now the nested parentheses cause trouble for me and I don't know how to construct the appropriate p_* functions. Is recursion allowed? If so, how? This is what I have thus far: import ply.lex as lex import ply.yacc as yacc # List of token names. This is always required tokens = ( 'VARIABLE', 'OR

Lex/Yacc for Boolean Calculation

五迷三道 提交于 2019-12-11 14:57:04
问题 I'm trying to write a program for calculating a NAND boolean expression using Lex/Yacc. For example, if input is "true nand (false nand true)," my program should print "false" This is what I have so far,, but I am really stuck What am I missing or doing wrong? boolean.l %{ #include "y.tab.h" %} %% "true"|"false" {return BOOL;} "nand" {return NAND;} [()] {return yytext[0];} %% boolean.y %token BOOL NAND %left NAND %% boolexp: boolexp NAND boolterm {$$ = !($1 && $3);} | boolterm {$$=$1;} ;

Yacc Problem: Make Data available in next Non Terminal

♀尐吖头ヾ 提交于 2019-12-11 14:09:17
问题 I want to make some variables I generate in b available in c: a : b c { ...some code...} A simple example: b : X { int result = 0; } | Y { int result = 1; } so I can, later on in c say: c : D { printf(result + 1); } | E { printf(result + 2); } Is there any chance to do that? Any help would really be appreciated! 回答1: result should be a global variable. You can do this by including %{ int result; %} at the top of your YACC file. Of course, you should also replace int result = 0 and int result

Bison Reduce/Reduce Conflict with Casting and Expression Parentheses

余生颓废 提交于 2019-12-11 10:25:13
问题 I'm building a grammar in bison, and I've narrowed down my last reduce/reduce error to the following test-case: %{ #include <stdio.h> #include <string.h> extern yydebug; void yyerror(const char *str) { fprintf(stderr, "Error: %s\n", str); } main() { yydebug = 1; yyparse(); } %} %right '=' %precedence CAST %left '(' %token AUTO BOOL BYTE DOUBLE FLOAT INT LONG SHORT SIGNED STRING UNSIGNED VOID %token IDENTIFIER %start file %debug %% file : %empty | statement file ; statement : expression ';' ;

Cant find Reduce/Reduce conflict in Grammar

最后都变了- 提交于 2019-12-11 09:12:21
问题 I wrote the following grammar and Bison is warning me about a reduce/reduce conflict. parser.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr] How can I detect which part of the grammar is generating the conflict? Is there a log generated by Bison where I can see the conflicts? And also, how could I solve this? Grammar: %left TK_OC_OR TK_OC_AND %left '<' '>' TK_OC_LE TK_OC_GE TK_OC_EQ TK_OC_NE %left '+' '-' %left '*' '/' %nonassoc LOWER_THAN_ELSE %nonassoc TK_PR_ELSE %start s %type<symbol

Parsing a sequence of expressions using yacc

坚强是说给别人听的谎言 提交于 2019-12-11 08:55:57
问题 I am trying to parse a sequence of expressions without delimiters in order to be able to parse ML/F# style function invocations: myfunc expr1 expr2 expr3 However, the sequence of expressions is giving me a list of shift/reduce conflicts. My guess is that the conflicts are caused by the recursive nature of my grammar, but I don't know how to fix these conflicts. My (simplified) precedence rules and grammar looks like this: /* Lowest precedence */ %left PLUS %left TIMES %left LPAR /* Highest

Lex - yacc program showing syntax error

一个人想着一个人 提交于 2019-12-11 08:48:58
问题 The following two codes have been written in order to perform arithmetic operations on input , but it gives me syntax error all the time Here is the lex program %{ #include "y.tab.h" #include <stdlib.h> %} %% [0-9]+ {yylval = atoi(yytext);return ID;} [*-+/()] {return yytext[0];} '\n' {return END;} . {return yytext[0];} %% Here is the yacc program : %{ #include <stdio.h> #include <stdlib.h> #include "y.tab.h" %} %token ID END %% S: expr END { printf("Answer is : %d\n",$$); exit(1); } expr: ID

Has anyone used the “selection preferences” provided by MKS Yacc?

自古美人都是妖i 提交于 2019-12-11 07:36:13
问题 Since I've gotten no answer at all to my question Is there an alternative to MKS Yacc that supports selection preference syntax or something very similar?, I'll ask the more basic question: Has anyone used the "selection preferences" provided by MKS Yacc? If you have, what did you use it for? Also, does it make any sense to use it in anything other than the last position in a rule? I have to look after a grammar which figures rules such as: TOKEN1 LPAREN non_terminal1 [^EQUAL] TOKEN2 non

Link error when using lex and yacc output in Visual Studio

故事扮演 提交于 2019-12-11 07:33:13
问题 I am using this windows version of flex (lex) and bison (yacc) to port my query compiler from linux to Windows. The output files, lex.yy.c , y.tab.c and y.tab.h are getting generated properly. However, I am getting the following link error. Error 29 error LNK2001: unresolved external symbol _yylval G:\Project\lex.yy.obj QC Error 30 error LNK1120: 1 unresolved externals G:\Project\QC.exe QC I tried copying the above files generated by the original linux versions of flex and yacc to Visual

Is there an alternative to MKS Yacc that supports “selection preference syntax” or something very similar? [closed]

旧街凉风 提交于 2019-12-11 06:59:59
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 4 years ago . MKS Yacc supports a notation which their web site calls "selection preference syntax". It isn't illustrated, but it consists of a token in square brackets, optionally with a caret, and it indicates that a particular token is required to follow, or is required not to follow, the rest of the rules: non_terminal: