bison

Trouble linking against LLVM with project including Flex and Bison

十年热恋 提交于 2019-12-01 05:43:17
I've been working through a tutorial on writing compilers with Flex, Bison, and LLVM (http://gnuu.org/2009/09/18/writing-your-own-toy-compiler/), and attempting to compile the final binary fails with many of the following "undefined reference" errors: g++ -o parser `llvm-config --libs core jit native --cxxflags --ldflags` *.cpp /tmp/ccl0CSyi.o: In function `NBinaryOperator::codeGen(CodeGenContext&)': codegen.cpp:(.text+0x2ce): undefined reference to `llvm::BinaryOperator::Create(llvm::Instruction::BinaryOps, llvm::Value*, llvm::Value*, llvm::Twine const&, llvm::BasicBlock*)' /tmp/ccl0CSyi.o:

How to get the AST from YACC?

*爱你&永不变心* 提交于 2019-12-01 04:45:50
I know how to make YACC generate an AST, but how do you actaully get it? I mean, how do you actaully get the value of the root node from YACC? Yacc only gives you back one node at a time, and it's always something that you just gave yacc at some earlier time, i.e., whatever you wanted to return from a reduced production or whatever you wanted to return from a terminal symbol. (Sorry, you said you knew that, but some people reading this might not.) So, take whatever you woud have returned from the root or top rule, and save it (in your attached C reduction code) any way you like. What Yacc

How to get the AST from YACC?

杀马特。学长 韩版系。学妹 提交于 2019-12-01 03:28:37
问题 I know how to make YACC generate an AST, but how do you actaully get it? I mean, how do you actaully get the value of the root node from YACC? 回答1: Yacc only gives you back one node at a time, and it's always something that you just gave yacc at some earlier time, i.e., whatever you wanted to return from a reduced production or whatever you wanted to return from a terminal symbol. (Sorry, you said you knew that, but some people reading this might not.) So, take whatever you woud have returned

When is %destructor invoked in BISON?

谁都会走 提交于 2019-12-01 03:08:10
问题 When is %destructor invoked in BISON ? I have the following bison code: %union{ char * sval; Variable * vval; } %token VARIABLE %token Literal %type <vval> Expression VARIABLE %type <sval> Literal %destructor { delete $$; } <vval> %destructor { delete $$; } Literal where Variable is a class. I thought that after processing a line, all the Variable objects will be freed, but I can see no destructor invoked. And that will lead straightly to memory leak... Edit: To be clear; I allocate a new

Flex / Lex Encoding Strings with Escaped Characters

眉间皱痕 提交于 2019-12-01 00:06:22
I'll refer to this question for some of the background: Regular expression for a string literal in flex/lex The problem I am having is handling the input with escaped characters in my lexer and I think it may be an issue to do with the encoding of the string, but I'm not sure. Here's is how I am handling string literals in my lexer: \"(\\.|[^\\"])*\" { char* text1 = strndup(yytext + 1, strlen(yytext) - 2); char* text2 = "text\n"; printf("value = <%s> <%x>\n", text1, text1); printf("value = <%s> <%x>\n", text2, text2); } This outputs the following: value = <text\n"> <15a1bb0> value = <text >

How can I have an “implicit multiplication” rule with Bison?

十年热恋 提交于 2019-11-30 18:38:28
问题 I'm working on a Bison file for a mathematical expression parser. Up to now it's mostly fine, but I'm facing a problem with implicit multiplications. You see, I'd like to support expressions like 2x sin(4x) cos(4x) . It should parse like 2 * x * sin(4 * x) * cos(4 * x) . Nothing too bad here, but consider the following set of rules: expr : /* snip */ | '-' expr { /* negate expression */ } | expr '-' expr { /* subtract expressions */ } | expr expr { /* multiply expressions */ } Having that

GCC says “syntax error before numeric constant” in generated header file from bison

谁说胖子不能爱 提交于 2019-11-30 16:54:17
问题 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 honestly don't know why this is happening--I would really like to use the generated yylval and I need it from this header file. Even if I declared yytokentype like this in

How to build an Array with Bison/Yacc and a Recursive Rule

不问归期 提交于 2019-11-30 16:08:19
With Bison, I figured out how to get everything into one long string as follows: arg_list: WORD arg_list { strcat( $1, "IFS" ); $$ = strcat($1, $2); } | WORD ; and: WORD arg_list { printf("%s, %s\n", $1, $2); } But the problem is that I will then have to split up $2 in the second rule again to parse it. Is there a way to populate an array instead of just using concatenation? Am I going about this the wrong way? If I need to build something like a linked list that could make sense, just not sure what would be the proper way to bind to arg_list, and then clean up the memory. If you have an array

Why am I getting this error: “data definition has no type or storage class”?

℡╲_俬逩灬. 提交于 2019-11-30 13:58:47
#include <stdio.h> #include <stdlib.h> struct NODE { char* name; int val; struct NODE* next; }; typedef struct NODE Node; Node *head, *tail; head = (Node*) malloc( sizeof( Node ) ); //line 21 And I compiling like this: cc -g -c -o file.tab.o file.tab.c I'm getting this error message: file.y:21:1 warning: data definition has no type or storage class [enabled by default] Natan Streppel It looks like the line head = (Node*) malloc( sizeof( Node ) ); //line 21 is outside the main() function. You can't do that, because you can't execute code outside functions. The only thing you can do at global

How does flex support bison-location exactly?

試著忘記壹切 提交于 2019-11-30 11:28:39
问题 I'm trying to use flex and bison to create a filter, because I want get certain grammar elements from a complex language. My plan is to use flex + bison to recognise the grammar, and dump out the location of elements of interest. (Then use a script to grab text according the locations dumped.) I found flex can support a bison feature called bison-locations, but how it works in exactly. I tried the example in flex document, it seems the yylloc is not set automatically by flex, I always get (1