gnu-flex

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...

Using $x to grab string from rule

I'm trying to do something like this in Bison... loop_for: FOR var_name COLONEQUALS expression TO {printf("%s<=", $2);} expression STEP {printf("%s+=", $2);} expression {printf(")\n");} Code ENDFOR What I'm trying to do is convert a for statement from the fake language's syntax to C's. However, th...

Typedef issue with bison and gnu flex

In my parser, I have %union { SYMTABLE *p_entry ; QUAD *p_quad ; } ; Now, SYMTABLE is the typedef for a struct. The struct and typedef are in an included file. There are no issues with this. QUAD is the typedef for a struct (typedef struct quad QUAD). The struct and typedef are in an included file. There is no problem doing: b...

Calling lex/yacc from a separate program

I've been reading up on lex/yacc. The books and examples are not hard to follow. In fact, the whole concept is clear as a bell. With one exception. Lex/yacc seem to generate standalone programs. What if I wanted to call them as a parser function? Yacc seems to generate a main function, so how would I call from my own without conflicts. ...

Space issue with Flex/Bison

Im currently working on an HTML parser/template library using Flex/Bison. I am having some issues with my if statement. The expression parses fine ( if you > me ) but when it comes to the statement between the beginning and ending if tags, it is only getting the first word and dying on the space between them. Just wondering how I can en...

Define comment and quote in flex and bison and lexical errors.

I have to create a lexical and syntax analyzer for a c-like language. In this language we define as comment "everything that exists after the symbol % until the end of line". Are the following declarations correct? Flex ... [%][^\n]*[\n] { return T_COMMENT; } [\n] { return T_NEWLINE; } Bison ... comment:com text newline; text: |name...

request for member in something not a structure or union

Hi! I've been working for hours and I can't figure out how to print the token from the bison file, the bison file is this: (it's a short simple file) This is the modified version: the solution to the problem: %{ #include <stdio.h> #include <stdlib.h> void yyerror(const char *); int yylex(void); int id; %} %union { int d; } %error-v...

When to free memory?

I'm working on a lexer / parser combination with Bison and Flex, and am slightly worried about memory leaks. The grammar is quite a simple one, and (so far) I have been able to get away with the following definition: #define YYSTYPE char const * In Flex, when I read a string, I allocate enough memory for that string and then copy it ...

Returning "non-ints" from yylex

I have a generated scanner from flex which the output from is not consumed by yacc or bison. yylex() needs to returns a pointer to a token-like structure memory instead of an int indicating the token-type. // example definition from foo.l [A-Za-z_][A-Za-z0-9_]* { return scanner_token(T_IDENTIFIER); } // example implementation of scanne...

Optimizing Bison Grammar

I have this grammar of a C# like language, and I want to make a parser for it, but when I put the grammar it tells me about Shift/Reduce conflicts. I tried to fix some but I can't seem to find another way to improve this grammar. Any help would be greatly appreciated :D Here's the grammar: Program: Decl | Program Decl ; D...