bison

Resolve conflict in bison grammar with space separated expression lists + if/then/else

狂风中的少年 提交于 2019-11-29 18:07:10
I have the following yacc/bison/happy grammar: %token if TokenIf then TokenThen else TokenElse true TokenTrue false TokenFalse %left APP %right IF %% Hungry : NoHungry | Hungry NoHungry %prec APP | if Hungry then Hungry else Hungry %prec IF NoHungry : true | false bison -v tells me there are two conflicts in the following situation: State 12 2 Hungry: Hungry . NoHungry 3 | if Hungry then Hungry else Hungry . true shift, and go to state 2 false shift, and go to state 3 true [reduce using rule 3 (Hungry)] false [reduce using rule 3 (Hungry)] $default reduce using rule 3 (Hungry) NoHungry go to

how to escape flex keyword

本秂侑毒 提交于 2019-11-29 17:37:21
I am using Flex & bison on Linux. I have have the following set up: // tokens CREATE { return token::CREATE;} SCHEMA { return token::SCHEMA; } RECORD { return token::RECORD;} [_a-zA-Z0-9][_a-zA-Z0-9]* { yylval->strval = strdup(yytext); return TOKEN::NAME;} ... // rules CREATE SCHEMA NAME ... CREATE RECORD NAME ... ... Everything worked just fine. But if users enter: "create schema record ..." (where 'record' is the name of the schema to be created), Flex will report an error since it matches 'record' as a token and it is looking for the rule "CREATE SCHEMA RECORD". I understand that keywords

Shift/reduce conflict with C-like grammar under Bison

若如初见. 提交于 2019-11-29 15:02:56
I've been working on a C-like grammar for my personal amusement. However, I've been running into shift/reduce conflicts, and I'm quite sure they could be resolved. Right now my expressions look like this, in a simplified form, stripped of actions: %left '+' '-' %% expr : NUMBER | IDENTIFIER | expr '+' expr | expr '-' expr /* other operators like '*', '/', etc. */ | expr '(' expr ')' /* function call */ %% However, this results in shift/reduce conflicts: the parser is unsure about how to treat parentheses. From what -v tells me, it is unclear whether an expression like expr '+' expr '(' should

Yacc/Bison, minimize amount by grouping math ops

最后都变了- 提交于 2019-11-29 14:42:22
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, $1, $3); } | expr cmpOp expr { $$ = opr(cmpOp, 2, $1, $3); } NOTE: I am using bison. The problem with

parse bibtex with flex+bison: revisited

社会主义新天地 提交于 2019-11-29 12:42:57
For last few weeks, I am trying to write a parser for bibtex ( http://www.bibtex.org/Format/ ) file using flex and bison. $ cat raw.l %{ #include "raw.tab.h" %} value [\"\{][a-zA-Z0-9 .\t\{\} \"\\]*[\"\}] %% [a-zA-Z]* return(KEY); \" return(QUOTE); \{ return(OBRACE); \} return(EBRACE); ; return(SEMICOLON); [ \t]+ /* ignore whitespace */; {value} { yylval.sval = malloc(strlen(yytext)); strncpy(yylval.sval, yytext, strlen(yytext)); return(VALUE); } $ cat raw.y %{ #include <stdio.h> %} //Symbols. %union { char *sval; }; %token <sval> VALUE %token KEY %token OBRACE %token EBRACE %token QUOTE

Make bison start parsing with a rule other than the start rule

╄→尐↘猪︶ㄣ 提交于 2019-11-29 12:38:44
Currently I'm working on a source-to-source compiler and I have already written a bison parser that creates the AST for the input correctly. I need to do several transformations on the syntax tree now and therefore I need to insert many nodes to the tree. I could create all the structs/unions that I want to add to the syntax tree manually, but this seems to be very much work. It would be much easier for me to create a string and I want this string to be parsed by the parser I already have. The parser should then return the tree for this string, which I can insert in my original syntax tree.

freeing the string allocated in strdup() from flex/bison

℡╲_俬逩灬. 提交于 2019-11-29 07:29:21
I have flex code that copies a string lexeme using strdup() . %{ #include "json.tab.h" #define YY_DECL extern "C" int yylex() %} %option noyywrap %% [ \t\n]+ ; \"[a-zA-Z]+\" {yylval.sval = strdup(yytext); return STRING; } [0-9]+ {yylval.ival = atoi(yytext); return NUMBER; } . {return yytext[0];} ; %% strdup() allocates memory and copies the input string into it and return ( strdup() - what does it do in C? ), so I guess I need to free it up when I don't need it anymore. From this post: When is %destructor invoked in BISON? , I added %destructor { free($$); printf("free");} STRING in the yacc

How much time would it take to write a C++ compiler using flex/yacc?

人盡茶涼 提交于 2019-11-29 06:16:34
问题 How much time would it take to write a C++ compiler using lex/yacc? Where can I get started with it? 回答1: There are many parsing rules that cannot be parsed by a bison/yacc parser (for example, distinguishing between a declaration and a function call in some circumstances). Additionally sometimes the interpretation of tokens requires input from the parser, particularly in C++0x. The handling of the character sequence >> for example is crucially dependent on parsing context. Those two tools

bison end of file

大兔子大兔子 提交于 2019-11-29 04:03:38
If I forget to put an empty line at the end of any of my files my program gets a syntax error. The problem is my grammar expects a newline to end the current line. Since a newline doesn't exist bison generates a syntax error because it does not finish the rule. How do I solve this? I tried making <<EOF>> return MY_EOF BUT when I do that lex crashes a horrible death. I guess there's code in its default EOF that I am not calling. I have no idea what functions they may be. Using EOF create the error symbol EOF is used, but is not defined as a token and has no rules You could use a flex EOF rule

C grammar in GCC source code

青春壹個敷衍的年華 提交于 2019-11-29 01:21:20
问题 I'm looking for the C grammar in GCC source code, more specifically for the grammar in the yacc/bison form. 回答1: Found the C grammar in Yacc specification in the GCC version 3.3 in the file "c-parse.y" 回答2: You will not find a C grammar yacc/bison file within the current GCC source code. It was done in the past, before the egcs fork stuff. I cannot give you the exact version and location, but i can tell you that it should be in the 2.x release The current version of GCC has its own C parser