bison

Simple XML parser in bison/flex

I would like to create simple xml parser using bison/flex. I don't need validation, comments, arguments, only <tag>value</tag>, where value can be number, string or other <tag>value</tag>. So for example: <div> <mul> <num>20</num> <add> <num>1</num> <num>5</num> </add> </mul> <id>test</id> </div> If it h...

Is there a way to localize error messages from bison/flex?

Do bison and flex allow user to natively localize error messages? For example, I would like to translate following message: syntax error, unexpected NUMBER, expecting $end to other language and replace NUMBER/$end with something more human-readable. ...

Creating diagram of grammar (bison)

Hey, Do you know guys how to create a diagram like following: http://www.svgopen.org/2009/papers/58-Interactive_Documentation_using_JavaScript_and_SVG/oracle_graph.png from bison grammar? ...

Building Lisp/Scheme-like parse tree with flex/bison

Hello, I was trying to parse simple Lisp/scheme-like code E.g. (func a (b c d) ) and build a tree from it, I could do the parsing in C without using bison (i.e, using only flex to return tokens and building the tree with recursion). But, with bison grammar, I am not sure where to add the code to build the list (i.e, which rule to...

c++ what is the advantage of lex and bison to a selfmade tokenizer / parser

Hi, I would like to do some parsing and tokenizing in c++ for learning purposes. Now I often times came across bison/yacc and lex when reading about this subject online. Would there be any mayor benefit of using those over for instance a tokenizer/parser written using STL or boost::regex or maybe even just C? ...

Where are the shift/reduce conflicts in this Bison code coming from?

I'm trying to parse this syntax: 34 + 1 − 8, 32 * 87 + 6 / 4, 34 / 8 I'm expecting to ground it like this: (, (- (+ 34 1) 8) (/ (+ (* 32 87) 6) 4) (/ 34 8)) This is the code for BISON: %token NUMBER %token COMMA %token OPERATOR %left OPERATOR %left COMMA %% term: NUMBER | term op term ; op: OPERATOR | COMMA; %% There is a probl...

flex/bison fixing memory leaks with unexpected tokens

I have a flex bison application. For a few of my tokens, I copy out the yytext from flex using strdup. This works great except when there is an error of an unexpected token. simple example flex.l: ... [a-zA-Z0-9]+ { lval.string = strdup(yytext); return IDENT }; [\{\}] { return yytext[0] }; ... and parse.y ... %destructor { ...

Undefined reference to yyparse (flex & bison)

I'm attempting to learn some flex/bison, and I'm reading Flex & Bison by John Levine (O'Reilly). There is an example that I need to get running, however I can't get it to run as I get the following error: /tmp/ccKZcRYB.o: In function `yylex': fb3-1.lex.c:(.text+0x2bd): undefined reference to `yylval' /tmp/cclBqnOk.o: In function `main'...

segmentation fault with bison and flex

Hi, I was trying learn lex and yacc using the oreilly book. I tried following example from book, but it gives segmentation fault. %{ /** * A lexer for the basic grammar to use for recognizing English sentences. */ #include <stdio.h> extern FILE *yyin; %} %token NOUN PRONOUN VERB ADVERB ADJECTIVE PREPOSITION CONJUNCTION %% ...

Conflict with ++ in bison, how to write post / pre?

I get a conflict with ++ and -- in bison. I wrote these two lines for post and pre increment | rval PLUSPLUS | PLUSPLUS rval I get a conflict. It only happens when both are included. I thought it may be involved with syntax like var+++var2 confusing with + pre or post +. However the conflict remained removing removing + in my syntax...

How do i allow postfix characters on a literal?

Using Bison/Flex i want to make this valid a = 9u I wrote a rule allowing 'u' to be after literals but realize flex isnt picking it up because letters return VAR instead of their ascii value. How do i allow postfix character on a literal? I am thinking write it in the lex rule but i dont know if a nasty surprise will happen. Where is...

Bison Shift/Reduce conflict for simple grammar

I'm building a parser for a language I've designed, in which type names start with an upper case letter and variable names start with a lower case letter, such that the lexer can tell the difference and provide different tokens. Also, the string 'this' is recognised by the lexer (it's an OOP language) and passed as a separate token. Fina...

How long to implement language after rules are done in bison?

I am writing my own language and i pretty much finished the rules in bison. Now what is left is implementing the right side. There are about 650 lines. The language is less complex then C but still fairly complex. I havent tried implementing the rules except for a test app to learn it. I had many segfaults but got the hang of it. Each r...

Syntax error, unexpected TRUE, expecting '{'

I'm writing a parser in Bison for a basic compiler (and then expand it to contain subroutines and dynamic memory allocation). The grammar is defined in Appendix A of the dragon book. My Flex scanner works--I ran my test files through it and it printed out all the correct tokens it found. Sorry about the strange formatting below. My bison...

Strange problem with context free grammar

I begin with an otherwise well formed (and well working) grammar for a language. Variables, binary operators, function calls, lists, loops, conditionals, etc. To this grammar I'd like to add what I'm calling the object construct: object : object_name ARROW more_objects ; more_objects : object_name | object_name ARROW more_ob...

GCC says "syntax error before numeric constant" in generated header file from bison.

When I compile my .y file with bison parser.y -d -t and then include the parser.tab.h file in my flex file, gcc says "error: syntax error before numeric constant." It's referencing line 32, which is the first line in the enum of yytokentype. enum yytokentype { BREAK = 258, ... } The error is about the line "BREAK = 258." I hone...

Emacs modes for flex and bison, or removing auto indent for these modes?

Emacs has poor handling of auto-indentation in Flex and Bison. In fact, it seems to have no support for flex mode. So, how does an emacs user cope with these? I like VIm but I would prefer not to switch because I am much faster and more comfortable in Emacs. I had a third party elisp module for Bison a few months ago but when its inden...

Bison grammar for collecting arguments

I have a bison grammar for collecting arguments of a function. This is what it is so far: args: arg {$$ = intArray($1);} //pseudo-code function | args arg {$$ = $1 + $2;} //pseudo-code array addition arg : NUM {$$ = $1;} | exp {$$ = $1;} How can I create an array of inte...

Adding indentation

I'm trying to make a parser for a made-up programming language. I'm now at the part of the exercise where we're required to make sure the parser's output is a conversion in C of the input. So things like... STARTMAIN a=b+2; return a ENDMAIN ...must become... int main () { a=b+2; return a; } So far so good, almost. The exercise als...

Trouble with printf in Bison Rule

I have tried something like this in my Bison file... ReturnS: RETURN expression {printf(";")} ...but the semicolon gets printed AFTER the next token, past this rule, instead of right after the expression. This rule was made as we're required to convert the input file to a c-like form and the original language doesn't require a semicol...