bison

How to resolve a shift/reduce conflict forcing a shift or a reduce?

别等时光非礼了梦想. 提交于 2019-12-06 01:08:39
When there is a shift/reduce conflict in Yacc/Bison, is it possible to force the conflict to be solved exactly as you want? In other words: is it possible explicitly force it to prioritize the shift or the reduce? For what I have read, if you are happy with the default resolution you can tell the generator to not complain about it . I really don't like this because it is obfuscating your rational choice. Another option is to rewrite the grammar to fix the issue. I don't know if this is always possible and often this makes it much harder to understand. Finally, I have read the precedence rules

Why is yylval null?

拈花ヽ惹草 提交于 2019-12-06 01:00:11
I'm trying to write my first parser with Flex & Bison. When parsing numbers, I'm trying to save their values into the yylval structure. The problem is, yylval is null when the lexer reaches a number, which causes a segmentation fault. (Related point of confusion: why is it that in most Flex examples (e.g. here ), yylval is a structure, rather than a pointer to a structure? I couldn't get yylval to be recognized in test.l without %option bison-bridge , and that option made yylval a pointer. Also, I tried initializing yylval in main of test.y, but yylval = malloc(...) gives a type mismatch-- as

Shift/reduce conflicts in bison

元气小坏坏 提交于 2019-12-05 17:14:23
问题 I'm new to Bison and I'm having trouble with shift/reduce conflicts... I'm trying to load from file to array data[] : struct _data { char name[50]; char surname[50]; int year; } data[1000]; Here is part of my bison code: %token ID NUM NL EOF %% File : List EOF ; List : Record | List Record ; Record : Name Surname Year NL { count++; } | NL { count++; } | /*empty*/ ; Name : ID { strcpy(data[count].name, yytext); } ; Surname: ID { strcpy(data[count].surname, yytext); } ; Year : NUM { data[count]

Is there a Sublime Text Syntax for Flex and Bison?

梦想与她 提交于 2019-12-05 15:09:26
问题 I'm looking for a syntax in Sublime Text that highlights my Flex and Bison files (or lex/yacc) in a way that makes them readable... Sublime Text automatically chooses Lisp for Flex files, but that doesn't do the trick all that well. Any suggestions to try another syntax? Or is there a plugin somewhere that's useful (haven't found anything so far)?. 回答1: I haven't found one built specifically for Sublime, but I've found one for TextMate, which Sublime is compatible with. Therefore, for Flex

Difficulties during the compilation (g++, bison, flex) with yyparse();

放肆的年华 提交于 2019-12-05 14:31:45
I have a problem with compilation of my code: Flex: %{ #include "lista4.tab.hpp" #include <stdlib.h> extern int yylex(); %} %% "=" {return EQ;} "!=" {return NE;} "<" {return LT;} ">" {return GT;} ":=" {return ASSIGN;} ";" {return SEMICOLON;} "IF" {return IF;} "THEN"{return THEN;} "END" {return END;} [_a-z]+ {yylval.text = strdup(yytext); return IDENTIFIER;} [ \t]+ [0-9]+ { yylval.var = atoi (yytext); return NUMBER; } [-+/^*'%'()] { return *yytext; } \n return RESULT; %% Bison: %{ extern "C" { int yyparse(); int yylex(void); void yyerror(char *s){} int yywrap(void){return 1;} } #include

Multiple flex/bison parsers

旧巷老猫 提交于 2019-12-05 06:44:35
What is the best way to handle multiple Flex/Bison parsers inside a project? I wrote a parser and now I need a second one in the same project. So far in the third section of parser1.y I inserted the main(..) method and called yyparse from there. What I want to obtain is having two different parsers ( parser1.y and parser2.y ) and be able to use them from an external function (let's assume main in main.cpp ). Which precautions should I use to export yyparse functions outside .y files and how should I handle two parsers? PS. I'm using g++ to compile but not the C++ versions of Flex and Bison and

Why does this simple grammar have a shift/reduce conflict?

别等时光非礼了梦想. 提交于 2019-12-05 06:14:42
问题 %token <token> PLUS MINUS INT %left PLUS MINUS THIS WORKS: exp : exp PLUS exp; exp : exp MINUS exp; exp : INT; THIS HAS 2 SHIFT/REDUCE CONFLICTS: exp : exp binaryop exp; exp : INT; binaryop: PLUS | MINUS ; WHY? 回答1: This is because the second is in fact ambiguous. So is the first grammar, but you resolved the ambiguity by adding %left . This %left does not work in the second grammar, because associativity and precedence are not inherited from rule to rule. I.e. the binaryop nonterminal does

Assigning multiple data types to a non-terminal in yacc

我怕爱的太早我们不能终老 提交于 2019-12-05 06:08:09
问题 I'm working on a project for class in which we have to build a parser. We're currently in the stage of building the parser in yacc. The thing currently confusing me is I've read that you need to assign a type to each nonterminal. In some cases though I'll have Something like: ... %union { Type dataType; int integerConstant; bool boolConstant; char *stringConstant; double doubleConstant; char identifier[MaxIdentLen+1]; // +1 for terminating null Decl *decl; List<Decl*> *declList; } %token

How to fix a missing ld library for -lfl while compiling?

你说的曾经没有我的故事 提交于 2019-12-05 02:10:30
I am trying to translate my .spl file into a C file (because there is no compiler). I have an example "Hello World" .spl file, and I have downloaded the Shakespeare Programming Language .tar and extracted it, but I have no idea what to do next. I can't seem to find instructions in any documentation. Can anyone help? Edit: When I type make -f "Makefile" , I get the following output: bison --verbose -d grammar.y gcc -O2 -Wall -c grammar.tab.c gcc -O2 -Wall -c makescanner.c gcc makescanner.o -O2 -Wall -o makescanner ./makescanner include > scanner.l flex -Cem -t scanner.l > scanner.c scanner.l

Why are multi-line comments in flex/bison so evasive?

假如想象 提交于 2019-12-04 23:33:57
I'm trying to parse C-style multi-line comments in my flex (.l) file: %s ML_COMMENT %% ... <INITIAL>"/*" BEGIN(ML_COMMENT); <ML_COMMENT>"*/" BEGIN(INITIAL); <ML_COMMENT>[.\n]+ { } I'm not returning any token and my grammar (.y) doesn't address comments in any way. When I run my executable, I get a parse error: $ ./a.out /* abc def Parse error: parse error $ echo "/* foo */" | ./a.out Parse error: parse error (My yyerror function does a printf("Parse error: %s\n"), which is where the first half of the redundant error message comes from). I can see why the second example fails since the entirety